Skip to content

[NEB-113] Save context chains in local storage #6452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()]
: [];
Expand Down Expand Up @@ -493,3 +508,31 @@ function WalletDisconnectedDialog(props: {
</Dialog>
);
}

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;
}
}
Loading