Fix network switcher to sandbox

This commit is contained in:
Tommy Verrall
2026-06-12 18:19:06 +02:00
parent b6eb391e85
commit 234d077936
3 changed files with 34 additions and 1 deletions
+19 -1
View File
@@ -22,6 +22,7 @@ import { createSignInWindow, getReactState, setReactState } from '../requests/ap
import { fetchNymPriceDeduped, getNetworkOverviewEndpoints, clearNymPriceCache } from '../api/networkOverview';
import { signInAndNavigateToBalance } from '../utils/signInAndNavigateToBalance';
import { dedupeInflightByKey } from '../utils/dedupeInflightByKey';
import { shouldRefreshAccountOnManualNetworkSwitch } from '../utils/networkSwitchPolicy';
import { toDisplay } from '../utils';
export const urls = (networkName?: Network) =>
@@ -362,7 +363,24 @@ export const AppProvider: FCWithChildren = ({ children }) => {
const handleShowAdmin = () => setShowAdmin((show) => !show);
const handleShowTerminal = () => setShowTerminal((show) => !show);
const switchNetwork = (_network: Network) => setNetwork(_network);
const switchNetwork = async (_network: Network) => {
if (_network === network) {
return;
}
setNetwork(_network);
if (!shouldRefreshAccountOnManualNetworkSwitch(Boolean(clientDetails))) {
return;
}
userBalance.clearAll();
setMixnodeDetails(null);
try {
await refreshAccount(_network);
await keepState();
} catch (e) {
enqueueSnackbar('Error switching network', { variant: 'error' });
Console.error(e as string);
}
};
const handleShowSendModal = () => setShowSendModal(true);
const handleShowReceiveModal = () => setShowReceiveModal(true);
const handleCloseSendModal = () => setShowSendModal(false);
@@ -0,0 +1,11 @@
import { shouldRefreshAccountOnManualNetworkSwitch } from './networkSwitchPolicy';
describe('networkSwitchPolicy', () => {
it('requires backend refresh when user switches network while logged in', () => {
expect(shouldRefreshAccountOnManualNetworkSwitch(true)).toBe(true);
});
it('defers account load to the network effect when there is no active session', () => {
expect(shouldRefreshAccountOnManualNetworkSwitch(false)).toBe(false);
});
});
@@ -0,0 +1,4 @@
/** Logged-in network changes must call Rust `switch_network`; the boot effect only loads when no session yet. */
export function shouldRefreshAccountOnManualNetworkSwitch(hasActiveSession: boolean): boolean {
return hasActiveSession;
}