Explorar el Código

refactor: uses of `chats.set(...)` support pagi sidebar

Aryan Kothari hace 9 meses
padre
commit
2c4bc7a2b2

+ 23 - 8
src/lib/components/chat/Chat.svelte

@@ -25,7 +25,9 @@
 		user,
 		socket,
 		showCallOverlay,
-		tools
+		tools,
+		pageSkip,
+		pageLimit
 	} from '$lib/stores';
 	import {
 		convertMessagesToHistory,
@@ -418,7 +420,9 @@
 					params: params,
 					files: chatFiles
 				});
-				await chats.set(await getChatList(localStorage.token));
+				await chats.set(
+					await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit)
+				);
 			}
 		}
 	};
@@ -464,7 +468,9 @@
 					params: params,
 					files: chatFiles
 				});
-				await chats.set(await getChatList(localStorage.token));
+				await chats.set(
+					await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit)
+				);
 			}
 		}
 	};
@@ -624,7 +630,9 @@
 					tags: [],
 					timestamp: Date.now()
 				});
-				await chats.set(await getChatList(localStorage.token));
+				await chats.set(
+					await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit)
+				);
 				await chatId.set(chat.id);
 			} else {
 				await chatId.set('local');
@@ -700,7 +708,8 @@
 			})
 		);
 
-		await chats.set(await getChatList(localStorage.token));
+		await chats.set(await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit));
+
 		return _responses;
 	};
 
@@ -947,7 +956,9 @@
 						params: params,
 						files: chatFiles
 					});
-					await chats.set(await getChatList(localStorage.token));
+					await chats.set(
+						await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit)
+					);
 				}
 			}
 		} else {
@@ -1220,7 +1231,9 @@
 							params: params,
 							files: chatFiles
 						});
-						await chats.set(await getChatList(localStorage.token));
+						await chats.set(
+							await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit)
+						);
 					}
 				}
 			} else {
@@ -1385,7 +1398,9 @@
 
 		if ($settings.saveChatHistory ?? true) {
 			chat = await updateChatById(localStorage.token, _chatId, { title: _title });
-			await chats.set(await getChatList(localStorage.token));
+			await chats.set(
+				await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit)
+			);
 		}
 	};
 

+ 2 - 2
src/lib/components/chat/Messages.svelte

@@ -1,6 +1,6 @@
 <script lang="ts">
 	import { v4 as uuidv4 } from 'uuid';
-	import { chats, config, settings, user as _user, mobile } from '$lib/stores';
+	import { chats, config, settings, user as _user, mobile, pageSkip, pageLimit } from '$lib/stores';
 	import { tick, getContext, onMount } from 'svelte';
 
 	import { toast } from 'svelte-sonner';
@@ -90,7 +90,7 @@
 			history: history
 		});
 
-		await chats.set(await getChatList(localStorage.token));
+		await chats.set(await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit));
 	};
 
 	const confirmEditResponseMessage = async (messageId, content) => {

+ 19 - 2
src/lib/components/chat/Settings/Chats.svelte

@@ -2,7 +2,7 @@
 	import fileSaver from 'file-saver';
 	const { saveAs } = fileSaver;
 
-	import { chats, user, settings } from '$lib/stores';
+	import { chats, user, settings, scrollPaginationEnabled, pageSkip, pageLimit } from '$lib/stores';
 
 	import {
 		archiveAllChats,
@@ -61,7 +61,12 @@
 				await createNewChat(localStorage.token, chat);
 			}
 		}
-
+		// 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));
 	};
 
@@ -77,6 +82,12 @@
 		await archiveAllChats(localStorage.token).catch((error) => {
 			toast.error(error);
 		});
+		// 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));
 	};
 
@@ -85,6 +96,12 @@
 		await deleteAllChats(localStorage.token).catch((error) => {
 			toast.error(error);
 		});
+		// 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));
 	};
 

+ 10 - 3
src/lib/components/chat/Tags.svelte

@@ -8,7 +8,7 @@
 		getTagsById,
 		updateChatById
 	} from '$lib/apis/chats';
-	import { tags as _tags, chats, pinnedChats } from '$lib/stores';
+	import { tags as _tags, chats, pinnedChats, pageSkip, pageLimit } from '$lib/stores';
 	import { createEventDispatcher, onMount } from 'svelte';
 
 	const dispatch = createEventDispatcher();
@@ -47,22 +47,29 @@
 		});
 
 		console.log($_tags);
+
 		await _tags.set(await getAllChatTags(localStorage.token));
 
+		console.log($_tags);
+		console.log('this run !!!!!');
 		console.log($_tags);
 
 		if ($_tags.map((t) => t.name).includes(tagName)) {
 			if (tagName === 'pinned') {
 				await pinnedChats.set(await getChatListByTagName(localStorage.token, 'pinned'));
 			} else {
-				await chats.set(await getChatListByTagName(localStorage.token, tagName));
+				await chats.set(
+					await getChatList(localStorage.token, 0, $pageSkip * $pageLimit || $pageLimit)
+				);
 			}
 
 			if ($chats.find((chat) => chat.id === chatId)) {
 				dispatch('close');
 			}
 		} else {
-			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'));
 		}
 	};

+ 16 - 4
src/lib/components/layout/Sidebar/ChatItem.svelte

@@ -14,7 +14,15 @@
 		getChatListByTagName,
 		updateChatById
 	} from '$lib/apis/chats';
-	import { chatId, chats, mobile, pinnedChats, showSidebar } from '$lib/stores';
+	import {
+		chatId,
+		chats,
+		mobile,
+		pageSkip,
+		pageLimit,
+		pinnedChats,
+		showSidebar
+	} from '$lib/stores';
 
 	import ChatMenu from './ChatMenu.svelte';
 	import ShareChatModal from '$lib/components/chat/ShareChatModal.svelte';
@@ -40,7 +48,9 @@
 			await updateChatById(localStorage.token, id, {
 				title: _title
 			});
-			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'));
 		}
 	};
@@ -53,14 +63,16 @@
 
 		if (res) {
 			goto(`/c/${res.id}`);
-			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'));
 		}
 	};
 
 	const archiveChatHandler = async (id) => {
 		await archiveChatById(localStorage.token, id);
-		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'));
 	};