diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 49a0e2ebb..d8434e6a2 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -11,7 +11,10 @@ showSidebar, mobile, showArchivedChats, - pinnedChats + pinnedChats, + pageSkip, + pageLimit, + scrollPaginationEnabled } from '$lib/stores'; import { onMount, getContext, tick } from 'svelte'; @@ -49,6 +52,12 @@ let showDropdown = false; let filteredChatList = []; + let paginationScrollThreashold = 0.6; + let nextPageLoading = false; + let tagView = false; + let chatPagniationComplete = false; + + pageLimit.set(20); $: filteredChatList = $chats.filter((chat) => { if (search === '') { @@ -84,7 +93,7 @@ showSidebar.set(window.innerWidth > BREAKPOINT); await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned')); - await chats.set(await getChatList(localStorage.token)); + await chats.set(await getChatList(localStorage.token, $pageSkip, $pageLimit)); let touchstart; let touchend; @@ -185,7 +194,9 @@ await tick(); goto('/'); } - await chats.set(await getChatList(localStorage.token)); + await chats.set( + await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit) + ); await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned')); } }; @@ -235,6 +246,9 @@ ? '' : 'invisible'}" > +

+ Chats loaded: {$chats.length} +

{ + on:focus={async () => { + // loading all chats. disable pagination on scrol. + scrollPaginationEnabled.set(false); + // subsequent queries will calculate page size to rehydrate the ui. + // since every chat is already loaded, the calculation should now load all chats. + pageSkip.set(0); + pageLimit.set(-1); + await chats.set(await getChatList(localStorage.token)); // when searching, load all chats + enrichChatsWithContent($chats); }} /> @@ -422,7 +444,13 @@
{/if} -
+
{ + if (!$scrollPaginationEnabled) return; + if (tagView) return; + if (nextPageLoading) return; + if (chatPagniationComplete) return; + + const maxScroll = e.target.scrollHeight - e.target.clientHeight; + const currentPos = e.target.scrollTop; + const ratio = currentPos / maxScroll; + if (ratio >= paginationScrollThreashold) { + nextPageLoading = true; + pageSkip.set($pageSkip + 1); + // extend existing chats + const nextPageChats = await getChatList( + localStorage.token, + $pageSkip * $pageLimit, + $pageLimit + ); + // once the bottom of the list has been reached (no results) there is no need to continue querying + chatPagniationComplete = nextPageChats.length === 0; + await chats.set([...$chats, ...nextPageChats]); + nextPageLoading = false; + } + }} + > {#each filteredChatList as chat, idx} {#if idx === 0 || (idx > 0 && chat.time_range !== filteredChatList[idx - 1].time_range)}