diff --git a/src/components/left/main/ChatList.tsx b/src/components/left/main/ChatList.tsx index 99e68f40..2540ea03 100644 --- a/src/components/left/main/ChatList.tsx +++ b/src/components/left/main/ChatList.tsx @@ -51,11 +51,11 @@ const ChatList: FC = ({ openNextChat, } = getDispatch(); - const virtualFolderId = ( + const resolvedFolderId = ( folderType === 'all' ? ALL_FOLDER_ID : folderType === 'archived' ? ARCHIVED_FOLDER_ID : folderId! ); - const orderedIds = useFolderManagerForOrderedIds(virtualFolderId); + const orderedIds = useFolderManagerForOrderedIds(resolvedFolderId); const orderById = useMemo(() => { if (!orderedIds) { @@ -136,7 +136,7 @@ const ChatList: FC = ({ function renderChats() { const viewportOffset = orderedIds!.indexOf(viewportIds![0]); - const pinnedCount = getPinnedChatsCount(virtualFolderId) || 0; + const pinnedCount = getPinnedChatsCount(resolvedFolderId) || 0; return viewportIds!.map((id, i) => ( ; foldersById: Record; usersById: Record; @@ -177,6 +179,8 @@ function updateFolderManager(global: GlobalState) { global.chats.listIds.archived && isMainFolderChanged(ARCHIVED_FOLDER_ID, global.chats.listIds.archived, global.chats.orderedPinnedIds.archived), ); + const isAllFullyLoadedChanged = global.chats.isFullyLoaded.active !== prevGlobal.isAllFolderFullyLoaded; + const isArchivedFullyLoadedChanged = global.chats.isFullyLoaded.archived !== prevGlobal.isArchivedFolderFullyLoaded; const areFoldersChanged = global.chatFolders.byId !== prevGlobal.foldersById; const areChatsChanged = global.chats.byId !== prevGlobal.chatsById; @@ -184,11 +188,23 @@ function updateFolderManager(global: GlobalState) { const areNotifySettingsChanged = selectNotifySettings(global) !== prevGlobal.notifySettings; const areNotifyExceptionsChanged = selectNotifyExceptions(global) !== prevGlobal.notifyExceptions; + let affectedFolderIds: number[] = []; + + if (isAllFullyLoadedChanged || isArchivedFullyLoadedChanged) { + affectedFolderIds = affectedFolderIds.concat( + updateFullyLoaded(global, isArchivedFullyLoadedChanged), + ); + } + if (!( isAllFolderChanged || isArchivedFolderChanged || areFoldersChanged || areChatsChanged || areUsersChanged || areNotifySettingsChanged || areNotifyExceptionsChanged ) ) { + if (affectedFolderIds.length) { + updateResults(affectedFolderIds); + } + return; } @@ -197,12 +213,12 @@ function updateFolderManager(global: GlobalState) { updateFolders(global, isAllFolderChanged, isArchivedFolderChanged, areFoldersChanged); - const affectedFolderIds = updateChats( + affectedFolderIds = affectedFolderIds.concat(updateChats( global, areFoldersChanged, areNotifySettingsChanged, areNotifyExceptionsChanged, prevAllFolderListIds, prevArchivedFolderListIds, - ); + )); - updateResults(affectedFolderIds); + updateResults(unique(affectedFolderIds)); if (DEBUG) { const duration = performance.now() - DEBUG_startedAt!; @@ -224,6 +240,33 @@ function isMainFolderChanged(folderId: number, newListIds?: string[], newPinnedI return currentListIds !== newListIds || currentPinnedIds !== newPinnedIds; } +function updateFullyLoaded( + global: GlobalState, + isArchivedFullyLoadedChanged = false, +) { + let affectedFolderIds = []; + + if (isArchivedFullyLoadedChanged) { + affectedFolderIds.push(ARCHIVED_FOLDER_ID); + } + + const isAllFolderFullyLoaded = global.chats.isFullyLoaded.active; + const isArchivedFolderFullyLoaded = global.chats.isFullyLoaded.archived; + + if (isAllFolderFullyLoaded && isArchivedFolderFullyLoaded) { + const emptyFolderIds = Object.keys(prepared.folderSummariesById) + .filter((folderId) => !results.orderedIdsByFolderId[folderId]) + .map(Number); + + affectedFolderIds = affectedFolderIds.concat(emptyFolderIds); + } + + prevGlobal.isAllFolderFullyLoaded = isAllFolderFullyLoaded; + prevGlobal.isArchivedFolderFullyLoaded = isArchivedFolderFullyLoaded; + + return affectedFolderIds; +} + function updateFolders( global: GlobalState, isAllFolderChanged: boolean, isArchivedFolderChanged: boolean, areFoldersChanged: boolean, ) { @@ -576,7 +619,7 @@ function buildFolderOrderedIds(folderId: number) { orderedIdsByFolderId: { [folderId]: prevOrderedIds }, } = results; - const allListIds = prevOrderedIds || Array.from(chatIds); + const allListIds = prevOrderedIds || (chatIds && Array.from(chatIds)) || []; const notPinnedIds = pinnedChatIds ? allListIds.filter((id) => !pinnedChatIds.has(id)) : allListIds; const sortedNotPinnedIds = notPinnedIds.sort((chatId1: string, chatId2: string) => { return chatSummariesById.get(chatId2)!.order - chatSummariesById.get(chatId1)!.order;