diff --git a/src/global/reducers/messages.ts b/src/global/reducers/messages.ts index a99f7b1a1..fa47934c3 100644 --- a/src/global/reducers/messages.ts +++ b/src/global/reducers/messages.ts @@ -17,8 +17,7 @@ import { } from '../../util/iteratees'; import { isLocalMessageId, type MessageKey } from '../../util/keys/messageKey'; import { - hasMessageTtl, isMediaLoadableInViewer, - mergeIdRanges, orderHistoryIds, orderPinnedIds, + hasMessageTtl, isMediaLoadableInViewer, mergeIdRanges, orderHistoryIds, orderPinnedIds, } from '../helpers'; import { selectChatMessage, @@ -58,10 +57,18 @@ export function updateCurrentMessageList( if (shouldReplaceHistory || (IS_TEST && !IS_MOCKED_CLIENT)) { newMessageLists = chatId ? [{ chatId, threadId, type }] : []; } else if (chatId) { - const last = messageLists[messageLists.length - 1]; - if (!last || last.chatId !== chatId || last.threadId !== threadId || last.type !== type) { - if (last && (last.chatId === TMP_CHAT_ID || shouldReplaceLast)) { - newMessageLists = [...messageLists.slice(0, -1), { chatId, threadId, type }]; + const current = messageLists[messageLists.length - 1]; + if (current?.chatId === chatId && current.threadId === threadId && current.type === type) { + return global; + } + + if (current && (current.chatId === TMP_CHAT_ID || shouldReplaceLast)) { + newMessageLists = [...messageLists.slice(0, -1), { chatId, threadId, type }]; + } else { + const previous = messageLists[messageLists.length - 2]; + + if (previous?.chatId === chatId && previous.threadId === threadId && previous.type === type) { + newMessageLists = messageLists.slice(0, -1); } else { newMessageLists = [...messageLists, { chatId, threadId, type }]; } @@ -599,10 +606,12 @@ export function updateThreadInfos( global: T, updates: Partial[], ): T { updates.forEach((update) => { - global = updateThreadInfo(global, + global = updateThreadInfo( + global, update.isCommentsInfo ? update.originChannelId! : update.chatId!, update.isCommentsInfo ? update.originMessageId! : update.threadId!, - update); + update, + ); }); return global; @@ -645,26 +654,28 @@ export function updateQuickReplyMessages( }; } -export function updateFocusedMessage({ - global, - chatId, - messageId, - threadId = MAIN_THREAD_ID, - noHighlight = false, - isResizingContainer = false, - quote, - scrollTargetPosition, -}: { - global: T; - chatId?: string; - messageId?: number; - threadId?: ThreadId; - noHighlight?: boolean; - isResizingContainer?: boolean; - quote?: string; - scrollTargetPosition?: ScrollTargetPosition; -}, -...[tabId = getCurrentTabId()]: TabArgs): T { +export function updateFocusedMessage( + { + global, + chatId, + messageId, + threadId = MAIN_THREAD_ID, + noHighlight = false, + isResizingContainer = false, + quote, + scrollTargetPosition, + }: { + global: T; + chatId?: string; + messageId?: number; + threadId?: ThreadId; + noHighlight?: boolean; + isResizingContainer?: boolean; + quote?: string; + scrollTargetPosition?: ScrollTargetPosition; + }, + ...[tabId = getCurrentTabId()]: TabArgs +): T { return updateTabState(global, { focusedMessage: { ...selectTabState(global, tabId).focusedMessage,