Fix deleting active folder

This commit is contained in:
Alexander Zinchuk 2023-04-26 21:15:13 +04:00
parent 70847a933e
commit b00f413055

View File

@ -1,10 +1,11 @@
import { addActionHandler, getGlobal, setGlobal } from '../../index'; import { addActionHandler, getGlobal, setGlobal } from '../../index';
import type { ApiUpdateChat } from '../../../api/types'; import type { ApiUpdateChat } from '../../../api/types';
import type { ActionReturnType } from '../../types';
import { MAIN_THREAD_ID } from '../../../api/types'; import { MAIN_THREAD_ID } from '../../../api/types';
import { ARCHIVED_FOLDER_ID, MAX_ACTIVE_PINNED_CHATS } from '../../../config'; import { ALL_FOLDER_ID, ARCHIVED_FOLDER_ID, MAX_ACTIVE_PINNED_CHATS } from '../../../config';
import { buildCollectionByKey, omit, pick } from '../../../util/iteratees'; import { buildCollectionByKey, omit } from '../../../util/iteratees';
import { closeMessageNotifications, notifyAboutMessage } from '../../../util/notifications'; import { closeMessageNotifications, notifyAboutMessage } from '../../../util/notifications';
import { import {
leaveChat, leaveChat,
@ -23,9 +24,10 @@ import {
selectCurrentMessageList, selectCurrentMessageList,
selectThreadParam, selectThreadParam,
selectChatFullInfo, selectChatFullInfo,
selectTabState,
} from '../../selectors'; } from '../../selectors';
import { updateUnreadReactions } from '../../reducers/reactions'; import { updateUnreadReactions } from '../../reducers/reactions';
import type { ActionReturnType } from '../../types'; import { updateTabState } from '../../reducers/tabs';
const TYPING_STATUS_CLEAR_DELAY = 6000; // 6 seconds const TYPING_STATUS_CLEAR_DELAY = 6000; // 6 seconds
@ -247,16 +249,21 @@ addActionHandler('apiUpdate', (global, actions, update): ActionReturnType => {
const { id, folder } = update; const { id, folder } = update;
const { byId: chatFoldersById, orderedIds } = global.chatFolders; const { byId: chatFoldersById, orderedIds } = global.chatFolders;
const newChatFoldersById = folder const isDeleted = folder === undefined;
? { ...chatFoldersById, [id]: folder }
: pick(
chatFoldersById,
Object.keys(chatFoldersById).map(Number).filter((folderId) => folderId !== id),
);
const newOrderedIds = folder Object.values(global.byTabId).forEach(({ id: tabId }) => {
? orderedIds && orderedIds.includes(id) ? orderedIds : [...(orderedIds || []), id] const tabState = selectTabState(global, tabId);
: orderedIds ? orderedIds.filter((orderedId) => orderedId !== id) : undefined; const isFolderActive = Object.values(chatFoldersById)[tabState.activeChatFolder - 1]?.id === id;
if (isFolderActive) {
global = updateTabState(global, { activeChatFolder: 0 }, tabId);
}
});
const newChatFoldersById = !isDeleted ? { ...chatFoldersById, [id]: folder } : omit(chatFoldersById, [id]);
const newOrderedIds = !isDeleted
? orderedIds?.includes(id) ? orderedIds : [...(orderedIds || []), id]
: orderedIds?.filter((orderedId) => orderedId !== id);
return { return {
...global, ...global,