From 954ee5c5086f6a61e32a55b622a0ec8a7a30f562 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 11 May 2021 23:17:56 +0300 Subject: [PATCH] Comment Threads: Various fixes --- src/api/gramjs/methods/messages.ts | 1 + src/api/types/updates.ts | 1 + src/components/middle/MessageList.tsx | 8 ++++++-- src/components/middle/MiddleColumn.tsx | 6 +++++- src/modules/actions/api/messages.ts | 8 ++++++-- src/modules/actions/apiUpdaters/chats.ts | 12 ++++++++---- src/modules/selectors/messages.ts | 4 +--- 7 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/api/gramjs/methods/messages.ts b/src/api/gramjs/methods/messages.ts index 725ca7270..4e3da8e16 100644 --- a/src/api/gramjs/methods/messages.ts +++ b/src/api/gramjs/methods/messages.ts @@ -732,6 +732,7 @@ export async function requestThreadInfoUpdate({ '@type': 'updateChat', id: newChat.id, chat: newChat, + noTopChatsRequest: true, }); }); } diff --git a/src/api/types/updates.ts b/src/api/types/updates.ts index decd5cc92..62957b4ea 100644 --- a/src/api/types/updates.ts +++ b/src/api/types/updates.ts @@ -61,6 +61,7 @@ export type ApiUpdateChat = { id: number; chat: Partial; newProfilePhoto?: ApiPhoto; + noTopChatsRequest?: boolean; }; export type ApiUpdateChatJoin = { diff --git a/src/components/middle/MessageList.tsx b/src/components/middle/MessageList.tsx index 8df067e71..21e833578 100644 --- a/src/components/middle/MessageList.tsx +++ b/src/components/middle/MessageList.tsx @@ -149,8 +149,7 @@ const MessageList: FC = ({ const anchorIdRef = useRef(); const anchorTopRef = useRef(); const listItemElementsRef = useRef(); - // Updated when opening chat (to preserve divider even after messages are read) - const memoUnreadDividerBeforeIdRef = useRef(firstUnreadId); + const memoUnreadDividerBeforeIdRef = useRef(); // Updated every time (to be used from intersection callback closure) const memoFirstUnreadIdRef = useRef(); const memoFocusingIdRef = useRef(); @@ -172,6 +171,11 @@ const MessageList: FC = ({ useOnChange(() => { memoFirstUnreadIdRef.current = firstUnreadId; + + // Updated only once (to preserve divider even after messages are read) + if (!memoUnreadDividerBeforeIdRef.current) { + memoUnreadDividerBeforeIdRef.current = firstUnreadId; + } }, [firstUnreadId]); const { diff --git a/src/components/middle/MiddleColumn.tsx b/src/components/middle/MiddleColumn.tsx index 1595610f8..c1ad3d067 100644 --- a/src/components/middle/MiddleColumn.tsx +++ b/src/components/middle/MiddleColumn.tsx @@ -330,7 +330,11 @@ export default memo(withGlobal( canPost: !isPinnedMessageList && (!chat || canPost) && (!isBotNotStarted || IS_MOBILE_SCREEN), isPinnedMessageList, messageSendingRestrictionReason: chat && getMessageSendingRestrictionReason(chat), - hasPinnedOrAudioMessage: Boolean(pinnedIds && pinnedIds.length) || Boolean(audioChatId && audioMessageId), + hasPinnedOrAudioMessage: ( + threadId !== MAIN_THREAD_ID + || Boolean(pinnedIds && pinnedIds.length) + || Boolean(audioChatId && audioMessageId) + ), customBackground, patternColor, isCustomBackgroundColor, diff --git a/src/modules/actions/api/messages.ts b/src/modules/actions/api/messages.ts index 154b35c85..b54373fd6 100644 --- a/src/modules/actions/api/messages.ts +++ b/src/modules/actions/api/messages.ts @@ -50,11 +50,13 @@ import { selectScheduledMessage, selectNoWebPage, } from '../../selectors'; -import { rafPromise } from '../../../util/schedulers'; +import { rafPromise, throttle } from '../../../util/schedulers'; import { copyTextToClipboard } from '../../../util/clipboard'; const uploadProgressCallbacks = new Map(); +const runThrottledForMarkRead = throttle((cb) => cb(), 1000, true); + addReducer('loadViewportMessages', (global, actions, payload) => { const { direction = LoadMoreDirection.Around, @@ -409,7 +411,9 @@ addReducer('markMessageListRead', (global, actions, payload) => { const { maxId } = payload!; - void callApi('markMessageListRead', { chat, threadId, maxId }); + runThrottledForMarkRead(() => { + void callApi('markMessageListRead', { chat, threadId, maxId }); + }); }); addReducer('markMessagesRead', (global, actions, payload) => { diff --git a/src/modules/actions/apiUpdaters/chats.ts b/src/modules/actions/apiUpdaters/chats.ts index fc66667d1..151d03267 100644 --- a/src/modules/actions/apiUpdaters/chats.ts +++ b/src/modules/actions/apiUpdaters/chats.ts @@ -1,6 +1,6 @@ import { addReducer, getGlobal, setGlobal } from '../../../lib/teact/teactn'; -import { ApiUpdate } from '../../../api/types'; +import { ApiUpdate, MAIN_THREAD_ID } from '../../../api/types'; import { ARCHIVED_FOLDER_ID, MAX_ACTIVE_PINNED_CHATS } from '../../../config'; import { pick } from '../../../util/iteratees'; @@ -27,7 +27,7 @@ const CURRENT_CHAT_UNREAD_DELAY = 1000; addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { switch (update['@type']) { case 'updateChat': { - if (!selectIsChatListed(global, update.id)) { + if (!update.noTopChatsRequest && !selectIsChatListed(global, update.id)) { // Chat can appear in dialogs list. actions.loadTopChats(); } @@ -95,7 +95,7 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { case 'newMessage': { const { message } = update; - const { chatId: currentChatId } = selectCurrentMessageList(global) || {}; + const { chatId: currentChatId, threadId, type: messageListType } = selectCurrentMessageList(global) || {}; if (message.senderId === global.currentUserId && !message.isFromScheduled) { return; @@ -106,7 +106,11 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { return; } - const isActiveChat = update.chatId === currentChatId; + const isActiveChat = ( + messageListType === 'thread' + && threadId === MAIN_THREAD_ID + && update.chatId === currentChatId + ); if (isActiveChat) { setTimeout(() => { diff --git a/src/modules/selectors/messages.ts b/src/modules/selectors/messages.ts index 4ed966814..4090fbeec 100644 --- a/src/modules/selectors/messages.ts +++ b/src/modules/selectors/messages.ts @@ -494,9 +494,7 @@ export function selectRealLastReadId(global: GlobalState, chatId: number, thread } // Some previously read messages may be deleted - return threadInfo.lastMessageId - ? Math.min(threadInfo.lastReadInboxMessageId, threadInfo.lastMessageId) - : threadInfo.lastReadInboxMessageId; + return Math.min(threadInfo.lastReadInboxMessageId, threadInfo.lastMessageId || Infinity); } }