This commit is contained in:
Timothy J. Baek 2024-08-04 17:31:41 +02:00
parent b2999ad590
commit e5dd7e65d4
5 changed files with 41 additions and 39 deletions

View File

@ -2,7 +2,7 @@
import fileSaver from 'file-saver'; import fileSaver from 'file-saver';
const { saveAs } = fileSaver; const { saveAs } = fileSaver;
import { chats, user, settings, scrollPaginationEnabled } from '$lib/stores'; import { chats, user, settings, scrollPaginationEnabled, currentChatPage } from '$lib/stores';
import { import {
archiveAllChats, archiveAllChats,
@ -12,12 +12,7 @@
getAllUserChats, getAllUserChats,
getChatList getChatList
} from '$lib/apis/chats'; } from '$lib/apis/chats';
import { import { getImportOrigin, convertOpenAIChats } from '$lib/utils';
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';
@ -66,7 +61,10 @@
await createNewChat(localStorage.token, chat); await createNewChat(localStorage.token, chat);
} }
} }
enablePagination();
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
scrollPaginationEnabled.set(true);
}; };
const exportChats = async () => { const exportChats = async () => {
@ -81,7 +79,10 @@
await archiveAllChats(localStorage.token).catch((error) => { await archiveAllChats(localStorage.token).catch((error) => {
toast.error(error); toast.error(error);
}); });
enablePagination();
currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
scrollPaginationEnabled.set(true);
}; };
const deleteAllChatsHandler = async () => { const deleteAllChatsHandler = async () => {
@ -90,7 +91,9 @@
toast.error(error); toast.error(error);
}); });
enablePagination(); currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage));
scrollPaginationEnabled.set(true);
}; };
const toggleSaveChatHistory = async () => { const toggleSaveChatHistory = async () => {

View File

@ -8,13 +8,18 @@
getTagsById, getTagsById,
updateChatById updateChatById
} from '$lib/apis/chats'; } from '$lib/apis/chats';
import { tags as _tags, chats, pinnedChats, currentChatPage } from '$lib/stores'; import {
tags as _tags,
chats,
pinnedChats,
currentChatPage,
scrollPaginationEnabled
} 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 = [];
@ -60,10 +65,10 @@
} }
} 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
enablePagination();
currentChatPage.set(1); currentChatPage.set(1);
await chats.set(await getChatList(localStorage.token, $currentChatPage)); await chats.set(await getChatList(localStorage.token, $currentChatPage));
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned')); await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await scrollPaginationEnabled.set(true);
} }
}; };

View File

@ -18,7 +18,6 @@
import { onMount, getContext, tick } from 'svelte'; import { onMount, getContext, tick } from 'svelte';
const i18n = getContext('i18n'); const i18n = getContext('i18n');
import { disablePagination, enablePagination } from '$lib/utils';
import { updateUserSettings } from '$lib/apis/users'; import { updateUserSettings } from '$lib/apis/users';
import { import {
@ -56,7 +55,7 @@
let filteredChatList = []; let filteredChatList = [];
// Pagination variables // Pagination variables
let chatListLoading = true; let chatListLoading = false;
let allChatsLoaded = false; let allChatsLoaded = false;
$: filteredChatList = $chats.filter((chat) => { $: filteredChatList = $chats.filter((chat) => {
@ -79,6 +78,16 @@
} }
}); });
const enablePagination = async () => {
// Reset pagination variables
currentChatPage.set(1);
allChatsLoaded = false;
await chats.set(await getChatList(localStorage.token, $currentChatPage));
// Enable pagination
scrollPaginationEnabled.set(true);
};
const loadMoreChats = async () => { const loadMoreChats = async () => {
chatListLoading = true; chatListLoading = true;
@ -104,10 +113,8 @@
}); });
showSidebar.set(window.innerWidth > BREAKPOINT); showSidebar.set(window.innerWidth > BREAKPOINT);
await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned')); await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
await chats.set(await getChatList(localStorage.token, $currentChatPage)); await enablePagination();
chatListLoading = false;
let touchstart; let touchstart;
let touchend; let touchend;
@ -439,7 +446,7 @@
bind:value={search} bind:value={search}
on:focus={async () => { on:focus={async () => {
// TODO: migrate backend for more scalable search mechanism // TODO: migrate backend for more scalable search mechanism
disablePagination(); scrollPaginationEnabled.set(false);
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);
}} }}
@ -452,9 +459,7 @@
<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 () => {
enablePagination(); await enablePagination();
allChatsLoaded = false;
await chats.set(await getChatList(localStorage.token, $currentChatPage));
}} }}
> >
{$i18n.t('all')} {$i18n.t('all')}
@ -463,17 +468,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(); scrollPaginationEnabled.set(false);
let chatIds = await getChatListByTagName(localStorage.token, tag.name); let chatIds = await getChatListByTagName(localStorage.token, tag.name);
if (chatIds.length === 0) { if (chatIds.length === 0) {
await tags.set(await getAllChatTags(localStorage.token)); await tags.set(await getAllChatTags(localStorage.token));
// 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
enablePagination(); await enablePagination();
allChatsLoaded = false;
chatIds = await getChatList(localStorage.token, $currentChatPage);
} }
await chats.set(chatIds); await chats.set(chatIds);
chatListLoading = false;
}} }}
> >
{tag.name} {tag.name}

View File

@ -42,7 +42,7 @@ export const showArchivedChats = writable(false);
export const showChangelog = writable(false); export const showChangelog = writable(false);
export const showCallOverlay = writable(false); export const showCallOverlay = writable(false);
export const scrollPaginationEnabled = writable(true); export const scrollPaginationEnabled = writable(false);
export const currentChatPage = writable(1); export const currentChatPage = writable(1);
export type Model = OpenAIModel | OllamaModel; export type Model = OpenAIModel | OllamaModel;

View File

@ -2,6 +2,7 @@ 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, chats, currentChatPage } from '$lib/stores'; import { scrollPaginationEnabled, chats, currentChatPage } from '$lib/stores';
import { getChatList } from '$lib/apis/chats';
////////////////////////// //////////////////////////
// Helper functions // Helper functions
@ -780,15 +781,3 @@ export const bestMatchingLanguage = (supportedLanguages, preferredLanguages, def
console.log(languages, preferredLanguages, match, defaultLocale); console.log(languages, preferredLanguages, match, defaultLocale);
return match || defaultLocale; return match || defaultLocale;
}; };
export const enablePagination = () => {
// Enable infinite scroll pagination
chats.set([]);
currentChatPage.set(1);
scrollPaginationEnabled.set(true);
};
export const disablePagination = () => {
// Disable infinite scroll pagination
scrollPaginationEnabled.set(false);
};