From 135cb1107c01cea692df7a6bba035f99dca9137d Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 1 Mar 2026 21:59:08 -0800 Subject: cache chats in localstorage --- src/routes/(app)/messages/+page.svelte | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/routes/(app)/messages/+page.svelte b/src/routes/(app)/messages/+page.svelte index 6eb5a399..87198cb6 100644 --- a/src/routes/(app)/messages/+page.svelte +++ b/src/routes/(app)/messages/+page.svelte @@ -85,10 +85,14 @@ return latest?.content || ""; }; - // Initialize from cache immediately + // Restore from localStorage on hard refresh, or in-memory cache on SPA nav + const storageKey = `chatList:${data.user?.pubkey}`; const cachedList = getCachedChatList(data.user?.pubkey); - let chats: any[] = $state(cachedList?.chats ?? []); - let allRumours: any[] = $state(cachedList?.rumours ?? []); + let stored: any = null; + try { stored = JSON.parse(localStorage.getItem(storageKey) || "null"); } catch {} + let chats: any[] = $state(cachedList?.chats ?? stored?.chats ?? []); + let allRumours: any[] = $state(cachedList?.rumours ?? stored?.rumours ?? []); + let loaded = $state(chats.length > 0); const updateSendersRecipients = async (rumours: any[]) => { allRumours = rumours; @@ -114,9 +118,10 @@ (mostRecentCommunication(rumours, a.pubkey) ?? 0), ); setCachedChatList(user.pubkey, chats, allRumours); + try { localStorage.setItem(storageKey, JSON.stringify({ chats, rumours })); } catch {} }; - (() => getMessageRumours(data.user).then(updateSendersRecipients))(); + (() => getMessageRumours(data.user).then(updateSendersRecipients).then(() => (loaded = true)))(); let closeSub: (() => void) | undefined; @@ -134,7 +139,9 @@
- {#if chats.length === 0} + {#if !loaded} +
+ {:else if chats.length === 0}

{$t("dm.noChats")}

{:else} {#each chats as c} -- cgit v1.2.3