From a0ee6ca9184128bb6cdd0a0ce09325632d19491e Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 24 May 2021 00:28:14 +0300 Subject: [PATCH] Message List: Fix losing scroll position after many new messages in closed chats --- src/modules/actions/apiUpdaters/messages.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/modules/actions/apiUpdaters/messages.ts b/src/modules/actions/apiUpdaters/messages.ts index 406e1e099..ad2ba9ed3 100644 --- a/src/modules/actions/apiUpdaters/messages.ts +++ b/src/modules/actions/apiUpdaters/messages.ts @@ -32,7 +32,7 @@ import { selectScheduledMessages, isMessageInCurrentMessageList, selectScheduledIds, - selectCurrentMessageList, + selectCurrentMessageList, selectRealLastReadId, selectViewportIds, } from '../../selectors'; import { getMessageContent, isChatPrivate, isMessageLocal } from '../../helpers'; @@ -427,7 +427,14 @@ function updateListedAndViewportIds(global: GlobalState, message: ApiMessage) { global = updateListedIds(global, chatId, MAIN_THREAD_ID, [id]); if (selectIsViewportNewest(global, chatId, MAIN_THREAD_ID)) { - global = addViewportId(global, chatId, MAIN_THREAD_ID, id); + // Always keep at least one read message in the viewport list + const lastReadId = selectRealLastReadId(global, chatId, MAIN_THREAD_ID); + const newGlobal = addViewportId(global, chatId, MAIN_THREAD_ID, id); + const newViewportIds = selectViewportIds(newGlobal, chatId, MAIN_THREAD_ID); + + if (!lastReadId || newViewportIds!.includes(lastReadId)) { + global = newGlobal; + } } const { threadInfo, firstMessageId } = selectThreadByMessage(global, chatId, message) || {};