From 5beff6d5f78243db1a6c694ea978dbe69c9247bc Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 7 Feb 2024 17:06:07 -0800 Subject: [PATCH] refac: optimisation --- src/lib/components/layout/Sidebar.svelte | 50 +++++++++++------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index d7a622ad2..9f41e80ea 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/src/lib/components/layout/Sidebar.svelte @@ -29,34 +29,26 @@ 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); - } + // Helper function to fetch and add chat content to each chat + const enrichChatsWithContent = async (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); + }; const loadChat = async (id) => { goto(`/c/${id}`); @@ -288,6 +280,9 @@ class="w-full rounded-r py-1.5 pl-2.5 pr-4 text-sm text-gray-300 bg-gray-950 outline-none" placeholder="Search" bind:value={search} + on:focus={() => { + enrichChatsWithContent($chats); + }} />