From 3fa7718fa1b822dafa3cf15c273463d7a6d1b765 Mon Sep 17 00:00:00 2001 From: Henry Holloway Date: Wed, 7 Feb 2024 16:39:20 -0800 Subject: [PATCH 1/2] full search --- src/lib/components/layout/Sidebar.svelte | 51 +++++++++++++++++------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index fbdd61b90..d7a622ad2 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -11,6 +11,7 @@ import { deleteChatById, getChatList, + getChatById, getChatListByTagName, updateChatById } from '$lib/apis/chats'; @@ -28,18 +29,34 @@ let showDropdown = false; onMount(async () => { - if (window.innerWidth > 1280) { - show = true; - } + if (window.innerWidth > 1280) { + show = true; + } - await chats.set(await getChatList(localStorage.token)); + const chatList = await getChatList(localStorage.token); + await enrichChatsWithContent(chatList); + }); + + tags.subscribe(async (value) => { + if (value.length === 0) { + const chatList = await getChatList(localStorage.token); + await enrichChatsWithContent(chatList); + } + }); + + // Helper function to fetch and add chat content to each chat + async function enrichChatsWithContent(chatList) { + const enrichedChats = await Promise.all(chatList.map(async (chat) => { + const chatDetails = await getChatById(localStorage.token, chat.id).catch(error => null); // Handle error or non-existent chat gracefully + if (chatDetails) { + chat.chat = chatDetails.chat; // Assuming chatDetails.chat contains the chat content + } + return chat; + })); + + await chats.set(enrichedChats); + } - tags.subscribe(async (value) => { - if (value.length === 0) { - await chats.set(await getChatList(localStorage.token)); - } - }); - }); const loadChat = async (id) => { goto(`/c/${id}`); @@ -323,13 +340,19 @@ let title = chat.title.toLowerCase(); const query = search.toLowerCase(); - if (title.includes(query)) { - return true; - } else { - return false; + let contentMatches = false; + // Access the messages within chat.chat.messages + if (chat.chat && chat.chat.messages && Array.isArray(chat.chat.messages)) { + contentMatches = chat.chat.messages.some(message => { + // Check if message.content exists and includes the search query + return message.content && message.content.toLowerCase().includes(query); + }); } + + return title.includes(query) || contentMatches; } }) as chat, i} +