Skip to content

Commit ce01481

Browse files
committed
Fix hydration issues, update caching logic
1 parent f7d7bf9 commit ce01481

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ storybook-static
3131
.aider*
3232

3333
tsconfig.tsbuildinfo
34+

apps/dashboard/src/app/nebula-app/(app)/components/ChatPageContent.tsx

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import { type ChatMessage, Chats } from "./Chats";
2424
import ContextFiltersButton, { ContextFiltersForm } from "./ContextFilters";
2525
import { EmptyStateChatPageContent } from "./EmptyStateChatPageContent";
2626

27+
const NEBULA_LAST_USED_CHAIN_IDS_KEY = "nebula-last-used-chain-ids";
28+
2729
export function ChatPageContent(props: {
2830
session: SessionInfo | undefined;
2931
authToken: string;
@@ -85,32 +87,33 @@ export function ChatPageContent(props: {
8587
NebulaContext | undefined
8688
>(() => {
8789
const contextRes = props.session?.context;
88-
90+
8991
// If we have context from an existing session, use that
9092
if (contextRes) {
9193
return {
92-
chainIds: contextRes.chain_ids || null,
93-
walletAddress: contextRes.wallet_address || null,
94+
chainIds: contextRes.chain_ids,
95+
walletAddress: contextRes.wallet_address,
9496
};
9597
}
96-
97-
// For new sessions, try to get cached chains from localStorage
98-
const cachedChains = localStorage.getItem('nebula-cached-chains');
99-
const parsedCachedChains = cachedChains ? JSON.parse(cachedChains) : null;
100-
98+
10199
return {
102-
chainIds: parsedCachedChains || ['1'], // Default to Ethereum if no cache
100+
chainIds: null,
103101
walletAddress: null,
104102
};
105103
});
106104

107105
const setContextFilters = useCallback((v: NebulaContext | undefined) => {
108106
_setContextFilters(v);
109107
setHasUserUpdatedContextFilters(true);
110-
108+
111109
// Cache the chains when context is updated
112110
if (v?.chainIds) {
113-
localStorage.setItem('nebula-cached-chains', JSON.stringify(v.chainIds));
111+
localStorage.setItem(
112+
NEBULA_LAST_USED_CHAIN_IDS_KEY,
113+
JSON.stringify(v.chainIds),
114+
);
115+
} else {
116+
localStorage.removeItem(NEBULA_LAST_USED_CHAIN_IDS_KEY);
114117
}
115118
}, []);
116119

@@ -136,12 +139,30 @@ export function ChatPageContent(props: {
136139

137140
// Only set wallet address from connected wallet
138141
updatedContextFilters.walletAddress = address || null;
139-
140-
// Only set chain if we don't already have chains (either from cache or session)
141-
if (!updatedContextFilters.chainIds?.length) {
142-
updatedContextFilters.chainIds = activeChain ? [activeChain.id.toString()] : ['1'];
142+
143+
// if we have cached chains, use that
144+
try {
145+
const lastUsedChainIdsStr = localStorage.getItem(
146+
NEBULA_LAST_USED_CHAIN_IDS_KEY,
147+
);
148+
149+
if (lastUsedChainIdsStr) {
150+
const lastUsedChainIds = lastUsedChainIdsStr
151+
? JSON.parse(lastUsedChainIdsStr)
152+
: null;
153+
154+
updatedContextFilters.chainIds = lastUsedChainIds;
155+
return updatedContextFilters;
156+
}
157+
} catch {
158+
// ignore
143159
}
144160

161+
// if we don't have chains, use the active chain
162+
updatedContextFilters.chainIds = activeChain
163+
? [activeChain.id.toString()]
164+
: [];
165+
145166
return updatedContextFilters;
146167
});
147168
}, [address, isNewSession, hasUserUpdatedContextFilters, activeChain]);

pnpm-lock.yaml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)