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