From 1e8d1e4b5225e7bb378f10e64efb6443b3351444 Mon Sep 17 00:00:00 2001 From: MananTank Date: Tue, 11 Mar 2025 17:36:20 +0000 Subject: [PATCH] [NEB-113] Save context chains in local storage (#6452) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## PR-Codex overview This PR focuses on enhancing the `ChatPageContent` component by implementing functionality to save and retrieve the last used chain IDs from local storage, ensuring that the user's selection persists across sessions. ### Detailed summary - Added `saveLastUsedChainIds` function to store chain IDs in local storage. - Implemented `getLastUsedChainIds` function to retrieve chain IDs from local storage. - Updated `updatedContextFilters` to use last used chain IDs if available. - Ensured wallet address is set only from a connected wallet. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .../(app)/components/ChatPageContent.tsx | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/apps/dashboard/src/app/nebula-app/(app)/components/ChatPageContent.tsx b/apps/dashboard/src/app/nebula-app/(app)/components/ChatPageContent.tsx index bcff408a0ad..76a5247bd08 100644 --- a/apps/dashboard/src/app/nebula-app/(app)/components/ChatPageContent.tsx +++ b/apps/dashboard/src/app/nebula-app/(app)/components/ChatPageContent.tsx @@ -96,6 +96,7 @@ export function ChatPageContent(props: { const setContextFilters = useCallback((v: NebulaContext | undefined) => { _setContextFilters(v); setHasUserUpdatedContextFilters(true); + saveLastUsedChainIds(v?.chainIds || undefined); }, []); const isNewSession = !props.session; @@ -118,7 +119,21 @@ export function ChatPageContent(props: { walletAddress: null, }; + // Only set wallet address from connected wallet updatedContextFilters.walletAddress = address || null; + + // if we have last used chains in storage, continue using them + try { + const lastUsedChainIds = getLastUsedChainIds(); + if (lastUsedChainIds) { + updatedContextFilters.chainIds = lastUsedChainIds; + return updatedContextFilters; + } + } catch { + // ignore local storage errors + } + + // else - use the active chain updatedContextFilters.chainIds = activeChain ? [activeChain.id.toString()] : []; @@ -493,3 +508,31 @@ function WalletDisconnectedDialog(props: { ); } + +const NEBULA_LAST_USED_CHAIN_IDS_KEY = "nebula-last-used-chain-ids"; + +function saveLastUsedChainIds(chainIds: string[] | undefined) { + try { + if (chainIds && chainIds.length > 0) { + localStorage.setItem( + NEBULA_LAST_USED_CHAIN_IDS_KEY, + JSON.stringify(chainIds), + ); + } else { + localStorage.removeItem(NEBULA_LAST_USED_CHAIN_IDS_KEY); + } + } catch { + // ignore local storage errors + } +} + +function getLastUsedChainIds(): string[] | null { + try { + const lastUsedChainIdsStr = localStorage.getItem( + NEBULA_LAST_USED_CHAIN_IDS_KEY, + ); + return lastUsedChainIdsStr ? JSON.parse(lastUsedChainIdsStr) : null; + } catch { + return null; + } +}