Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
44413385
Commit
44413385
authored
Aug 04, 2024
by
Timothy J. Baek
Browse files
refac: onScroll -> IntersectionObserver for infinite scroll
parent
389d650e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
36 deletions
+56
-36
src/lib/components/layout/Sidebar.svelte
src/lib/components/layout/Sidebar.svelte
+55
-36
src/lib/stores/index.ts
src/lib/stores/index.ts
+1
-0
No files found.
src/lib/components/layout/Sidebar.svelte
View file @
44413385
...
...
@@ -39,7 +39,7 @@
import UserMenu from './Sidebar/UserMenu.svelte';
import ChatItem from './Sidebar/ChatItem.svelte';
import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
import Sp
arkles
from '../
i
co
ns/Sparkles
.svelte';
import Sp
inner
from '../co
mmon/Spinner
.svelte';
const BREAKPOINT = 768;
...
...
@@ -58,10 +58,8 @@
let paginationScrollThreashold = 0.6;
let nextPageLoading = false;
let chatPagniationComplete = false;
// number of chats per page depends on screen size.
// 35px is the height of each chat item.
// load 5 extra chats
pageLimit.set(Math.round(window.innerHeight / 35) + 5);
pageLimit.set(20);
$: filteredChatList = $chats.filter((chat) => {
if (search === '') {
...
...
@@ -153,6 +151,48 @@
window.addEventListener('focus', onFocus);
window.addEventListener('blur', onBlur);
// Infinite scroll
const loader = document.getElementById('loader');
const observer = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
loadMoreContent();
observer.unobserve(loader); // Stop observing until content is loaded
}
});
},
{
root: null, // viewport
rootMargin: '0px',
threshold: 1.0 // When 100% of the loader is visible
}
);
observer.observe(loader);
const loadMoreContent = async () => {
if (!$scrollPaginationEnabled) return;
if ($tagView) return;
if (nextPageLoading) return;
if (chatPagniationComplete) return;
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;
observer.observe(loader); // Start observing again after content is loaded
};
return () => {
window.removeEventListener('keydown', onKeyDown);
window.removeEventListener('keyup', onKeyUp);
...
...
@@ -427,8 +467,8 @@
bind:value={search}
on:focus={async () => {
disablePagination();
// TODO: migrate backend for more scalable search mechanism
await chats.set(await getChatList(localStorage.token)); // when searching, load all chats
enrichChatsWithContent($chats);
}}
/>
...
...
@@ -506,33 +546,7 @@
</div>
{/if}
<div
class="pl-2 my-2 flex-1 flex flex-col space-y-1 overflow-y-auto scrollbar-hidden"
on:scroll={async (e) => {
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;
}
}}
>
<div class="pl-2 my-2 flex-1 flex flex-col space-y-1 overflow-y-auto scrollbar-hidden">
{#each filteredChatList as chat, idx}
{#if idx === 0 || (idx > 0 && chat.time_range !== filteredChatList[idx - 1].time_range)}
<div
...
...
@@ -582,9 +596,14 @@
}}
/>
{/each}
{#if nextPageLoading}
<div class="w-full flex justify-center py-4 animate-pulse">
<Sparkles />
{#if !chatPagniationComplete}
<div
id="loader"
class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2"
>
<Spinner className=" size-4" />
<div class=" ">Loading...</div>
</div>
{/if}
</div>
...
...
src/lib/stores/index.ts
View file @
44413385
...
...
@@ -41,6 +41,7 @@ export const showSettings = writable(false);
export
const
showArchivedChats
=
writable
(
false
);
export
const
showChangelog
=
writable
(
false
);
export
const
showCallOverlay
=
writable
(
false
);
export
const
scrollPaginationEnabled
=
writable
(
true
);
export
const
pageSkip
=
writable
(
0
);
export
const
pageLimit
=
writable
(
-
1
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment