This commit is contained in:
Timothy J. Baek 2024-08-04 16:36:44 +02:00
parent 4441338574
commit a2f9f7c975
6 changed files with 87 additions and 72 deletions

View File

@ -12,7 +12,12 @@
getAllUserChats, getAllUserChats,
getChatList getChatList
} from '$lib/apis/chats'; } from '$lib/apis/chats';
import { getImportOrigin, convertOpenAIChats, disablePagination } from '$lib/utils'; import {
getImportOrigin,
convertOpenAIChats,
disablePagination,
enablePagination
} from '$lib/utils';
import { onMount, getContext } from 'svelte'; import { onMount, getContext } from 'svelte';
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { toast } from 'svelte-sonner'; import { toast } from 'svelte-sonner';
@ -61,8 +66,7 @@
await createNewChat(localStorage.token, chat); await createNewChat(localStorage.token, chat);
} }
} }
disablePagination(); enablePagination();
await chats.set(await getChatList(localStorage.token));
}; };
const exportChats = async () => { const exportChats = async () => {
@ -77,8 +81,7 @@
await archiveAllChats(localStorage.token).catch((error) => { await archiveAllChats(localStorage.token).catch((error) => {
toast.error(error); toast.error(error);
}); });
disablePagination(); enablePagination();
await chats.set(await getChatList(localStorage.token));
}; };
const deleteAllChatsHandler = async () => { const deleteAllChatsHandler = async () => {
@ -86,8 +89,8 @@
await deleteAllChats(localStorage.token).catch((error) => { await deleteAllChats(localStorage.token).catch((error) => {
toast.error(error); toast.error(error);
}); });
disablePagination();
await chats.set(await getChatList(localStorage.token)); enablePagination();
}; };
const toggleSaveChatHistory = async () => { const toggleSaveChatHistory = async () => {

View File

@ -8,12 +8,13 @@
getTagsById, getTagsById,
updateChatById updateChatById
} from '$lib/apis/chats'; } from '$lib/apis/chats';
import { tags as _tags, chats, pinnedChats, pageSkip, pageLimit, tagView } from '$lib/stores'; import { tags as _tags, chats, pinnedChats, pageSkip, pageLimit } from '$lib/stores';
import { createEventDispatcher, onMount } from 'svelte'; import { createEventDispatcher, onMount } from 'svelte';
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
import Tags from '../common/Tags.svelte'; import Tags from '../common/Tags.svelte';
import { enablePagination } from '$lib/utils';
export let chatId = ''; export let chatId = '';
let tags = []; let tags = [];
@ -59,7 +60,7 @@
} }
} else { } else {
// if the tag we deleted is no longer a valid tag, return to main chat list view // if the tag we deleted is no longer a valid tag, return to main chat list view
tagView.set(false); enablePagination();
await chats.set( await chats.set(
await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit) await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit)
); );

View File

@ -0,0 +1,30 @@
<script lang="ts">
import { createEventDispatcher, onMount } from 'svelte';
const dispatch = createEventDispatcher();
let loaderElement: HTMLElement;
onMount(() => {
const observer = new IntersectionObserver(
(entries, observer) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
dispatch('visible');
// observer.unobserve(loaderElement); // Stop observing until content is loaded
}
});
},
{
root: null, // viewport
rootMargin: '0px',
threshold: 1.0 // When 100% of the loader is visible
}
);
observer.observe(loaderElement);
});
</script>
<div bind:this={loaderElement}>
<slot />
</div>

View File

@ -14,13 +14,12 @@
pinnedChats, pinnedChats,
pageSkip, pageSkip,
pageLimit, pageLimit,
scrollPaginationEnabled, scrollPaginationEnabled
tagView
} from '$lib/stores'; } from '$lib/stores';
import { onMount, getContext, tick } from 'svelte'; import { onMount, getContext, tick } from 'svelte';
const i18n = getContext('i18n'); const i18n = getContext('i18n');
import { disablePagination } from '$lib/utils'; import { disablePagination, enablePagination } from '$lib/utils';
import { updateUserSettings } from '$lib/apis/users'; import { updateUserSettings } from '$lib/apis/users';
import { import {
@ -40,6 +39,7 @@
import ChatItem from './Sidebar/ChatItem.svelte'; import ChatItem from './Sidebar/ChatItem.svelte';
import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte'; import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
import Spinner from '../common/Spinner.svelte'; import Spinner from '../common/Spinner.svelte';
import Loader from '../common/Loader.svelte';
const BREAKPOINT = 768; const BREAKPOINT = 768;
@ -55,9 +55,10 @@
let showDropdown = false; let showDropdown = false;
let filteredChatList = []; let filteredChatList = [];
let paginationScrollThreashold = 0.6;
let nextPageLoading = false; // Pagination variables
let chatPagniationComplete = false; let chatListLoading = false;
let allChatsLoaded = false;
pageLimit.set(20); pageLimit.set(20);
@ -81,6 +82,18 @@
} }
}); });
const loadMoreChats = async () => {
chatListLoading = true;
pageSkip.set($pageSkip + 1);
const newChatList = 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
allChatsLoaded = newChatList.length === 0;
await chats.set([...$chats, ...newChatList]);
chatListLoading = false;
};
onMount(async () => { onMount(async () => {
mobile.subscribe((e) => { mobile.subscribe((e) => {
if ($showSidebar && e) { if ($showSidebar && e) {
@ -151,48 +164,6 @@
window.addEventListener('focus', onFocus); window.addEventListener('focus', onFocus);
window.addEventListener('blur', onBlur); 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 () => { return () => {
window.removeEventListener('keydown', onKeyDown); window.removeEventListener('keydown', onKeyDown);
window.removeEventListener('keyup', onKeyUp); window.removeEventListener('keyup', onKeyUp);
@ -466,8 +437,8 @@
placeholder={$i18n.t('Search')} placeholder={$i18n.t('Search')}
bind:value={search} bind:value={search}
on:focus={async () => { on:focus={async () => {
disablePagination();
// TODO: migrate backend for more scalable search mechanism // TODO: migrate backend for more scalable search mechanism
disablePagination();
await chats.set(await getChatList(localStorage.token)); // when searching, load all chats await chats.set(await getChatList(localStorage.token)); // when searching, load all chats
enrichChatsWithContent($chats); enrichChatsWithContent($chats);
}} }}
@ -480,8 +451,8 @@
<button <button
class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full" class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
on:click={async () => { on:click={async () => {
disablePagination(); enablePagination();
allChatsLoaded = false;
await chats.set( await chats.set(
await getChatList(localStorage.token, $pageSkip * $pageLimit, $pageLimit) await getChatList(localStorage.token, $pageSkip * $pageLimit, $pageLimit)
); );
@ -493,18 +464,17 @@
<button <button
class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full" class="px-2.5 text-xs font-medium bg-gray-50 dark:bg-gray-900 dark:hover:bg-gray-800 transition rounded-full"
on:click={async () => { on:click={async () => {
disablePagination();
let chatIds = await getChatListByTagName(localStorage.token, tag.name); let chatIds = await getChatListByTagName(localStorage.token, tag.name);
if (chatIds.length === 0) { if (chatIds.length === 0) {
// no chats found in the tag
await tags.set(await getAllChatTags(localStorage.token)); await tags.set(await getAllChatTags(localStorage.token));
disablePagination();
chatIds = await getChatList( chatIds = await getChatList(
localStorage.token, localStorage.token,
$pageSkip * $pageLimit, $pageSkip * $pageLimit,
$pageLimit $pageLimit
); );
} }
tagView.set(true);
await chats.set(chatIds); await chats.set(chatIds);
}} }}
> >
@ -597,14 +567,19 @@
/> />
{/each} {/each}
{#if !chatPagniationComplete} {#if $scrollPaginationEnabled && !allChatsLoaded}
<div <Loader
id="loader" on:visible={(e) => {
class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2" if (!chatListLoading) {
loadMoreChats();
}
}}
> >
<Spinner className=" size-4" /> <div class="w-full flex justify-center py-1 text-xs animate-pulse items-center gap-2">
<div class=" ">Loading...</div> <Spinner className=" size-4" />
</div> <div class=" ">Loading...</div>
</div>
</Loader>
{/if} {/if}
</div> </div>
</div> </div>

View File

@ -45,7 +45,6 @@ export const showCallOverlay = writable(false);
export const scrollPaginationEnabled = writable(true); export const scrollPaginationEnabled = writable(true);
export const pageSkip = writable(0); export const pageSkip = writable(0);
export const pageLimit = writable(-1); export const pageLimit = writable(-1);
export const tagView = writable(false);
export type Model = OpenAIModel | OllamaModel; export type Model = OpenAIModel | OllamaModel;

View File

@ -1,7 +1,7 @@
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import sha256 from 'js-sha256'; import sha256 from 'js-sha256';
import { WEBUI_BASE_URL } from '$lib/constants'; import { WEBUI_BASE_URL } from '$lib/constants';
import { scrollPaginationEnabled, pageLimit, pageSkip } from '$lib/stores'; import { scrollPaginationEnabled, pageLimit, pageSkip, chats } from '$lib/stores';
////////////////////////// //////////////////////////
// Helper functions // Helper functions
@ -781,6 +781,13 @@ export const bestMatchingLanguage = (supportedLanguages, preferredLanguages, def
return match || defaultLocale; return match || defaultLocale;
}; };
export const enablePagination = () => {
chats.set([]);
scrollPaginationEnabled.set(true);
pageLimit.set(20);
pageSkip.set(0);
};
export const disablePagination = () => { export const disablePagination = () => {
scrollPaginationEnabled.set(false); scrollPaginationEnabled.set(false);
pageLimit.set(-1); pageLimit.set(-1);