Chat List: Fix deleted chat being stuck at the bottom (#3127)

This commit is contained in:
Alexander Zinchuk 2023-05-02 15:23:48 +04:00
parent 43eb7e7020
commit ec8e99127b
6 changed files with 52 additions and 17 deletions

View File

@ -242,7 +242,7 @@ const SettingsFoldersEdit: FC<OwnProps & StateProps> = ({
const isExpanded = mode === 'included' ? isIncludedChatsListExpanded : isExcludedChatsListExpanded; const isExpanded = mode === 'included' ? isIncludedChatsListExpanded : isExcludedChatsListExpanded;
const allChatIds = mode === 'included' ? includedChatIds : excludedChatIds; const allChatIds = mode === 'included' ? includedChatIds : excludedChatIds;
const leftChatsCount = allChatIds.length - selectedChatTypes.length - visibleChatIds.length; const leftChatsCount = allChatIds.length - visibleChatIds.length;
const clickHandler = mode === 'included' const clickHandler = mode === 'included'
? () => setIsIncludedChatsListExpanded(true) ? () => setIsIncludedChatsListExpanded(true)
: () => setIsExcludedChatsListExpanded(true); : () => setIsExcludedChatsListExpanded(true);

View File

@ -327,7 +327,10 @@ addActionHandler('loadFullChat', (global, actions, payload): ActionReturnType =>
}); });
addActionHandler('loadTopChats', (global): ActionReturnType => { addActionHandler('loadTopChats', (global): ActionReturnType => {
runThrottledForLoadTopChats(() => loadChats(global, 'active')); runThrottledForLoadTopChats(() => {
loadChats(global, 'active');
loadChats(global, 'archived');
});
}); });
addActionHandler('requestChatUpdate', (global, actions, payload): ActionReturnType => { addActionHandler('requestChatUpdate', (global, actions, payload): ActionReturnType => {

View File

@ -523,6 +523,26 @@ addActionHandler('deleteHistory', async (global, actions, payload): Promise<void
if (activeChat && activeChat.chatId === chatId) { if (activeChat && activeChat.chatId === chatId) {
actions.openChat({ id: undefined, tabId }); actions.openChat({ id: undefined, tabId });
} }
// Delete chat from folders
const folders = global.chatFolders.byId;
Object.values(folders).forEach((folder) => {
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<void> => { addActionHandler('reportMessages', async (global, actions, payload): Promise<void> => {

View File

@ -29,6 +29,7 @@ import {
deleteTopic, deleteTopic,
updateMessageTranslations, updateMessageTranslations,
clearMessageTranslation, clearMessageTranslation,
removeChatFromChatLists,
} from '../../reducers'; } from '../../reducers';
import { import {
selectChatMessage, selectChatMessage,
@ -133,7 +134,7 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
setGlobal(global); 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)) { if (!selectIsChatListed(global, chatId)) {
actions.loadTopChats(); actions.loadTopChats();
} }
@ -436,6 +437,10 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
actions.requestChatUpdate({ chatId }); actions.requestChatUpdate({ chatId });
} }
global = getGlobal();
global = removeChatFromChatLists(global, chatId);
setGlobal(global);
break; break;
} }

View File

@ -7,7 +7,7 @@ import { ARCHIVED_FOLDER_ID } from '../../config';
import { import {
areSortedArraysEqual, buildCollectionByKey, omit, unique, areSortedArraysEqual, buildCollectionByKey, omit, unique,
} from '../../util/iteratees'; } from '../../util/iteratees';
import { selectChat, selectChatFullInfo, selectChatListType } from '../selectors'; import { selectChat, selectChatFullInfo } from '../selectors';
import { updateThread, updateThreadInfo } from './messages'; import { updateThread, updateThreadInfo } from './messages';
import { areDeepEqual } from '../../util/areDeepEqual'; import { areDeepEqual } from '../../util/areDeepEqual';
@ -275,22 +275,22 @@ export function updateChatListSecondaryInfo<T extends GlobalState>(
} }
export function leaveChat<T extends GlobalState>(global: T, leftChatId: string): T { export function leaveChat<T extends GlobalState>(global: T, leftChatId: string): T {
const listType = selectChatListType(global, leftChatId); global = removeChatFromChatLists(global, leftChatId);
if (!listType) {
return global;
}
const { [listType]: listIds } = global.chats.listIds;
if (listIds) {
global = replaceChatListIds(global, listType, listIds.filter((listId) => listId !== leftChatId));
}
global = updateChat(global, leftChatId, { isNotJoined: true }); global = updateChat(global, leftChatId, { isNotJoined: true });
return global; return global;
} }
export function removeChatFromChatLists<T extends GlobalState>(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<T extends GlobalState>(global: T, chat: ApiChat, membersToAdd: ApiChatMember[]): T { export function addChatMembers<T extends GlobalState>(global: T, chat: ApiChat, membersToAdd: ApiChatMember[]): T {
const currentMembers = selectChatFullInfo(global, chat.id)?.members; const currentMembers = selectChatFullInfo(global, chat.id)?.members;
const newMemberIds = new Set(membersToAdd.map((m) => m.userId)); const newMemberIds = new Set(membersToAdd.map((m) => m.userId));

View File

@ -362,7 +362,9 @@ function updateChats(
const newAllFolderListIds = global.chats.listIds.active; const newAllFolderListIds = global.chats.listIds.active;
const newArchivedFolderListIds = global.chats.listIds.archived; const newArchivedFolderListIds = global.chats.listIds.archived;
let allIds = [...newAllFolderListIds || [], ...newArchivedFolderListIds || []];
const newAllIds = [...newAllFolderListIds || [], ...newArchivedFolderListIds || []];
let allIds = newAllIds;
if (newAllFolderListIds !== prevAllFolderListIds || newArchivedFolderListIds !== prevArchivedFolderListIds) { if (newAllFolderListIds !== prevAllFolderListIds || newArchivedFolderListIds !== prevArchivedFolderListIds) {
allIds = unique(allIds.concat(prevAllFolderListIds || [], prevArchivedFolderListIds || [])); allIds = unique(allIds.concat(prevAllFolderListIds || [], prevArchivedFolderListIds || []));
} }
@ -383,7 +385,11 @@ function updateChats(
let newFolderIds: number[]; let newFolderIds: number[];
if (chat) { if (chat) {
const currentSummary = prepared.chatSummariesById.get(chatId); 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)) { if (!areFoldersChanged && currentSummary && arePropsShallowEqual(newSummary, currentSummary)) {
return; return;
} }
@ -423,6 +429,7 @@ function buildChatSummary(
notifySettings: NotifySettings, notifySettings: NotifySettings,
notifyExceptions?: Record<number, NotifyException>, notifyExceptions?: Record<number, NotifyException>,
user?: ApiUser, user?: ApiUser,
isRemoved?: boolean,
): ChatSummary { ): ChatSummary {
const { const {
id, type, lastMessage, isRestricted, isNotJoined, migratedTo, folderId, id, type, lastMessage, isRestricted, isNotJoined, migratedTo, folderId,
@ -447,7 +454,7 @@ function buildChatSummary(
return { return {
id, id,
type, type,
isListed: Boolean(!isRestricted && !isNotJoined && !migratedTo && !shouldHideServiceChat), isListed: Boolean(!isRestricted && !isNotJoined && !migratedTo && !shouldHideServiceChat && !isRemoved),
isArchived: folderId === ARCHIVED_FOLDER_ID, isArchived: folderId === ARCHIVED_FOLDER_ID,
isMuted: selectIsChatMuted(chat, notifySettings, notifyExceptions), isMuted: selectIsChatMuted(chat, notifySettings, notifyExceptions),
isUnread: Boolean(unreadCount || unreadMentionsCount || hasUnreadMark), isUnread: Boolean(unreadCount || unreadMentionsCount || hasUnreadMark),