[Perf] Chat: Prevent redundant updates

This commit is contained in:
Alexander Zinchuk 2021-08-03 19:03:14 +03:00
parent 8dff81f301
commit a9eaa1a6d4

View File

@ -5,7 +5,7 @@ import { withGlobal } from '../../../lib/teact/teactn';
import useLang, { LangFn } from '../../../hooks/useLang'; import useLang, { LangFn } from '../../../hooks/useLang';
import { GlobalActions, MessageListType } from '../../../global/types'; import { GlobalActions } from '../../../global/types';
import { import {
ApiChat, ApiUser, ApiMessage, ApiMessageOutgoingStatus, ApiFormattedText, MAIN_THREAD_ID, ApiChat, ApiUser, ApiMessage, ApiMessageOutgoingStatus, ApiFormattedText, MAIN_THREAD_ID,
} from '../../../api/types'; } from '../../../api/types';
@ -66,16 +66,16 @@ type StateProps = {
chat?: ApiChat; chat?: ApiChat;
isMuted?: boolean; isMuted?: boolean;
privateChatUser?: ApiUser; privateChatUser?: ApiUser;
usersById?: Record<number, ApiUser>;
actionTargetUserIds?: number[]; actionTargetUserIds?: number[];
usersById?: Record<number, ApiUser>;
actionTargetMessage?: ApiMessage; actionTargetMessage?: ApiMessage;
actionTargetChatId?: number; actionTargetChatId?: number;
lastMessageSender?: ApiUser; lastMessageSender?: ApiUser;
lastMessageOutgoingStatus?: ApiMessageOutgoingStatus; lastMessageOutgoingStatus?: ApiMessageOutgoingStatus;
draft?: ApiFormattedText; draft?: ApiFormattedText;
messageListType?: MessageListType;
animationLevel?: number; animationLevel?: number;
isSelected?: boolean; isSelected?: boolean;
canScrollDown?: boolean;
lastSyncTime?: number; lastSyncTime?: number;
}; };
@ -92,17 +92,17 @@ const Chat: FC<OwnProps & StateProps & DispatchProps> = ({
isPinned, isPinned,
chat, chat,
isMuted, isMuted,
usersById,
privateChatUser, privateChatUser,
actionTargetUserIds, actionTargetUserIds,
usersById,
lastMessageSender, lastMessageSender,
lastMessageOutgoingStatus, lastMessageOutgoingStatus,
actionTargetMessage, actionTargetMessage,
actionTargetChatId, actionTargetChatId,
draft, draft,
messageListType,
animationLevel, animationLevel,
isSelected, isSelected,
canScrollDown,
lastSyncTime, lastSyncTime,
openChat, openChat,
focusLastMessage, focusLastMessage,
@ -169,12 +169,12 @@ const Chat: FC<OwnProps & StateProps & DispatchProps> = ({
const handleClick = useCallback(() => { const handleClick = useCallback(() => {
openChat({ id: chatId, shouldReplaceHistory: true }); openChat({ id: chatId, shouldReplaceHistory: true });
if (isSelected && messageListType === 'thread') { if (isSelected && canScrollDown) {
focusLastMessage(); focusLastMessage();
} }
}, [ }, [
isSelected, isSelected,
messageListType, canScrollDown,
openChat, openChat,
chatId, chatId,
focusLastMessage, focusLastMessage,
@ -338,6 +338,7 @@ export default memo(withGlobal<OwnProps>(
threadId: currentThreadId, threadId: currentThreadId,
type: messageListType, type: messageListType,
} = selectCurrentMessageList(global) || {}; } = selectCurrentMessageList(global) || {};
const isSelected = chatId === currentChatId && currentThreadId === MAIN_THREAD_ID;
return { return {
chat, chat,
@ -345,14 +346,14 @@ export default memo(withGlobal<OwnProps>(
lastMessageSender, lastMessageSender,
...(isOutgoing && { lastMessageOutgoingStatus: selectOutgoingStatus(global, chat.lastMessage) }), ...(isOutgoing && { lastMessageOutgoingStatus: selectOutgoingStatus(global, chat.lastMessage) }),
...(privateChatUserId && { privateChatUser: selectUser(global, privateChatUserId) }), ...(privateChatUserId && { privateChatUser: selectUser(global, privateChatUserId) }),
usersById,
actionTargetUserIds, actionTargetUserIds,
...(actionTargetUserIds && { usersById }),
actionTargetChatId, actionTargetChatId,
actionTargetMessage, actionTargetMessage,
draft: selectDraft(global, chatId, MAIN_THREAD_ID), draft: selectDraft(global, chatId, MAIN_THREAD_ID),
messageListType,
animationLevel: global.settings.byKey.animationLevel, animationLevel: global.settings.byKey.animationLevel,
isSelected: chatId === currentChatId && currentThreadId === MAIN_THREAD_ID, isSelected,
canScrollDown: isSelected && messageListType === 'thread',
lastSyncTime: global.lastSyncTime, lastSyncTime: global.lastSyncTime,
}; };
}, },