diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 2c34c60faf..cadcc937ac 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -28,6 +28,7 @@ sidebarWidth } from '$lib/stores'; import { + allChatsLoaded, loadNextChatListPage, refreshChatList, registerFolderRefreshHandler, @@ -116,7 +117,6 @@ // Pagination variables let chatListLoading = false; let chatListReady = false; - let allChatsLoaded = false; let showCreateFolderModal = false; @@ -376,7 +376,6 @@ const initChatList = async () => { // Reset pagination variables console.log('initChatList'); - allChatsLoaded = false; chatListReady = false; await Promise.all([ @@ -419,20 +418,18 @@ if (result.accepted) { await initFolders(); await Promise.all(Object.values(folderRegistry).map((folder) => folder?.setFolderItems?.())); - allChatsLoaded = result.allLoaded; chatListReady = true; } }; const loadMoreChats = async () => { - if (chatListLoading || allChatsLoaded || !$showSidebar) { + if (chatListLoading || $allChatsLoaded || !$showSidebar) { return; } chatListLoading = true; - const result = await loadNextChatListPage(localStorage.token); - allChatsLoaded = result.allLoaded; + await loadNextChatListPage(localStorage.token); chatListLoading = false; }; @@ -1680,7 +1677,7 @@ /> {/each} - {#if chatListReady && !allChatsLoaded} + {#if chatListReady && !$allChatsLoaded} { if (!chatListLoading) { diff --git a/src/lib/stores/chatList.ts b/src/lib/stores/chatList.ts index 070909076a..d92fb39352 100644 --- a/src/lib/stores/chatList.ts +++ b/src/lib/stores/chatList.ts @@ -8,14 +8,15 @@ type ChatListItem = { const chatsStore = writable(null); const pinnedChatsStore = writable([]); +const allChatsLoadedStore = writable(false); export const chats = readonly(chatsStore); export const pinnedChats = readonly(pinnedChatsStore); +export const allChatsLoaded = readonly(allChatsLoadedStore); let currentPage = 1; let paginationReady = false; let requestGeneration = 0; -let allLoaded = false; let loadingNextPage = false; type RefreshChatListOptions = { @@ -25,7 +26,6 @@ type RefreshChatListOptions = { type ChatListResult = { accepted: boolean; - allLoaded: boolean; }; export const refreshChatList = async ( @@ -44,12 +44,12 @@ export const refreshChatList = async ( ]); if (generation !== requestGeneration) { - return { accepted: false, allLoaded }; + return { accepted: false }; } chatsStore.set(nextChats); currentPage = 1; - allLoaded = nextChats.length === 0; + allChatsLoadedStore.set(nextChats.length === 0); if (options.clearPinned) { pinnedChatsStore.set([]); @@ -58,7 +58,7 @@ export const refreshChatList = async ( } paginationReady = true; - return { accepted: true, allLoaded }; + return { accepted: true }; }; // The sidebar owns folder state. This bridge lets other components refresh it. @@ -88,8 +88,8 @@ export const refreshSidebar = async (token: string = '') => { }; export const loadNextChatListPage = async (token: string = ''): Promise => { - if (!paginationReady || allLoaded || loadingNextPage) { - return { accepted: false, allLoaded }; + if (!paginationReady || get(allChatsLoadedStore) || loadingNextPage) { + return { accepted: false }; } const generation = requestGeneration; @@ -100,17 +100,17 @@ export const loadNextChatListPage = async (token: string = ''): Promise chat.id)); const uniqueChats = nextChats.filter((chat) => !existingIds.has(chat.id)); chatsStore.set([...(get(chatsStore) ?? []), ...uniqueChats]); - return { accepted: true, allLoaded }; + return { accepted: true }; } finally { loadingNextPage = false; } @@ -157,7 +157,7 @@ export const resetChatListState = () => { requestGeneration += 1; currentPage = 1; paginationReady = false; - allLoaded = false; + allChatsLoadedStore.set(false); loadingNextPage = false; chatsStore.set(null); pinnedChatsStore.set([]);