From ec8e99127b6690d9acc7c4186f9fa75c286e4ce7 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 2 May 2023 15:23:48 +0400 Subject: [PATCH] Chat List: Fix deleted chat being stuck at the bottom (#3127) --- .../settings/folders/SettingsFoldersEdit.tsx | 2 +- src/global/actions/api/chats.ts | 5 ++++- src/global/actions/api/messages.ts | 20 +++++++++++++++++ src/global/actions/apiUpdaters/messages.ts | 7 +++++- src/global/reducers/chats.ts | 22 +++++++++---------- src/util/folderManager.ts | 13 ++++++++--- 6 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/components/left/settings/folders/SettingsFoldersEdit.tsx b/src/components/left/settings/folders/SettingsFoldersEdit.tsx index 7b88df0ec..3bdce725e 100644 --- a/src/components/left/settings/folders/SettingsFoldersEdit.tsx +++ b/src/components/left/settings/folders/SettingsFoldersEdit.tsx @@ -242,7 +242,7 @@ const SettingsFoldersEdit: FC = ({ const isExpanded = mode === 'included' ? isIncludedChatsListExpanded : isExcludedChatsListExpanded; const allChatIds = mode === 'included' ? includedChatIds : excludedChatIds; - const leftChatsCount = allChatIds.length - selectedChatTypes.length - visibleChatIds.length; + const leftChatsCount = allChatIds.length - visibleChatIds.length; const clickHandler = mode === 'included' ? () => setIsIncludedChatsListExpanded(true) : () => setIsExcludedChatsListExpanded(true); diff --git a/src/global/actions/api/chats.ts b/src/global/actions/api/chats.ts index 10a151d42..98dc1c06d 100644 --- a/src/global/actions/api/chats.ts +++ b/src/global/actions/api/chats.ts @@ -327,7 +327,10 @@ addActionHandler('loadFullChat', (global, actions, payload): ActionReturnType => }); addActionHandler('loadTopChats', (global): ActionReturnType => { - runThrottledForLoadTopChats(() => loadChats(global, 'active')); + runThrottledForLoadTopChats(() => { + loadChats(global, 'active'); + loadChats(global, 'archived'); + }); }); addActionHandler('requestChatUpdate', (global, actions, payload): ActionReturnType => { diff --git a/src/global/actions/api/messages.ts b/src/global/actions/api/messages.ts index 7ff60db7a..9f6a0d92b 100644 --- a/src/global/actions/api/messages.ts +++ b/src/global/actions/api/messages.ts @@ -523,6 +523,26 @@ addActionHandler('deleteHistory', async (global, actions, payload): Promise { + if (folder.includedChatIds.includes(chatId) || folder.pinnedChatIds?.includes(chatId)) { + const newIncludedChatIds = folder.includedChatIds.filter((id) => id !== chatId); + const newPinnedChatIds = folder.pinnedChatIds?.filter((id) => id !== chatId); + + const updatedFolder = { + ...folder, + includedChatIds: newIncludedChatIds, + pinnedChatIds: newPinnedChatIds, + }; + + callApi('editChatFolder', { + id: folder.id, + folderUpdate: updatedFolder, + }); + } + }); }); addActionHandler('reportMessages', async (global, actions, payload): Promise => { diff --git a/src/global/actions/apiUpdaters/messages.ts b/src/global/actions/apiUpdaters/messages.ts index 415b5f115..bdcc5eb25 100644 --- a/src/global/actions/apiUpdaters/messages.ts +++ b/src/global/actions/apiUpdaters/messages.ts @@ -29,6 +29,7 @@ import { deleteTopic, updateMessageTranslations, clearMessageTranslation, + removeChatFromChatLists, } from '../../reducers'; import { selectChatMessage, @@ -133,7 +134,7 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => { setGlobal(global); - // Edge case: New message in an old (not loaded) chat. + // Reload dialogs if chat is not present in the list if (!selectIsChatListed(global, chatId)) { actions.loadTopChats(); } @@ -436,6 +437,10 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => { actions.requestChatUpdate({ chatId }); } + global = getGlobal(); + global = removeChatFromChatLists(global, chatId); + setGlobal(global); + break; } diff --git a/src/global/reducers/chats.ts b/src/global/reducers/chats.ts index 4afcad474..f5ba76d85 100644 --- a/src/global/reducers/chats.ts +++ b/src/global/reducers/chats.ts @@ -7,7 +7,7 @@ import { ARCHIVED_FOLDER_ID } from '../../config'; import { areSortedArraysEqual, buildCollectionByKey, omit, unique, } from '../../util/iteratees'; -import { selectChat, selectChatFullInfo, selectChatListType } from '../selectors'; +import { selectChat, selectChatFullInfo } from '../selectors'; import { updateThread, updateThreadInfo } from './messages'; import { areDeepEqual } from '../../util/areDeepEqual'; @@ -275,22 +275,22 @@ export function updateChatListSecondaryInfo( } export function leaveChat(global: T, leftChatId: string): T { - const listType = selectChatListType(global, leftChatId); - if (!listType) { - return global; - } - - const { [listType]: listIds } = global.chats.listIds; - - if (listIds) { - global = replaceChatListIds(global, listType, listIds.filter((listId) => listId !== leftChatId)); - } + global = removeChatFromChatLists(global, leftChatId); global = updateChat(global, leftChatId, { isNotJoined: true }); return global; } +export function removeChatFromChatLists(global: T, chatId: string): T { + const lists = global.chats.listIds; + Object.entries(lists).forEach(([listType, listIds]) => { + global = replaceChatListIds(global, listType as keyof typeof lists, listIds.filter((id) => id !== chatId)); + }); + + return global; +} + export function addChatMembers(global: T, chat: ApiChat, membersToAdd: ApiChatMember[]): T { const currentMembers = selectChatFullInfo(global, chat.id)?.members; const newMemberIds = new Set(membersToAdd.map((m) => m.userId)); diff --git a/src/util/folderManager.ts b/src/util/folderManager.ts index 89e741e13..8eb530a01 100644 --- a/src/util/folderManager.ts +++ b/src/util/folderManager.ts @@ -362,7 +362,9 @@ function updateChats( const newAllFolderListIds = global.chats.listIds.active; const newArchivedFolderListIds = global.chats.listIds.archived; - let allIds = [...newAllFolderListIds || [], ...newArchivedFolderListIds || []]; + + const newAllIds = [...newAllFolderListIds || [], ...newArchivedFolderListIds || []]; + let allIds = newAllIds; if (newAllFolderListIds !== prevAllFolderListIds || newArchivedFolderListIds !== prevArchivedFolderListIds) { allIds = unique(allIds.concat(prevAllFolderListIds || [], prevArchivedFolderListIds || [])); } @@ -383,7 +385,11 @@ function updateChats( let newFolderIds: number[]; if (chat) { const currentSummary = prepared.chatSummariesById.get(chatId); - const newSummary = buildChatSummary(chat, newNotifySettings, newNotifyExceptions, newUsersById[chatId]); + const isRemoved = !newAllIds.includes(chatId); + const newSummary = buildChatSummary( + chat, newNotifySettings, newNotifyExceptions, newUsersById[chatId], isRemoved, + ); + if (!areFoldersChanged && currentSummary && arePropsShallowEqual(newSummary, currentSummary)) { return; } @@ -423,6 +429,7 @@ function buildChatSummary( notifySettings: NotifySettings, notifyExceptions?: Record, user?: ApiUser, + isRemoved?: boolean, ): ChatSummary { const { id, type, lastMessage, isRestricted, isNotJoined, migratedTo, folderId, @@ -447,7 +454,7 @@ function buildChatSummary( return { id, type, - isListed: Boolean(!isRestricted && !isNotJoined && !migratedTo && !shouldHideServiceChat), + isListed: Boolean(!isRestricted && !isNotJoined && !migratedTo && !shouldHideServiceChat && !isRemoved), isArchived: folderId === ARCHIVED_FOLDER_ID, isMuted: selectIsChatMuted(chat, notifySettings, notifyExceptions), isUnread: Boolean(unreadCount || unreadMentionsCount || hasUnreadMark),