From 234d077936e0d9d977323b68c8b799c2c3a904b3 Mon Sep 17 00:00:00 2001 From: Tommy Verrall Date: Fri, 12 Jun 2026 18:19:06 +0200 Subject: [PATCH] Fix network switcher to sandbox --- nym-wallet/src/context/main.tsx | 20 ++++++++++++++++++- .../src/utils/networkSwitchPolicy.test.ts | 11 ++++++++++ nym-wallet/src/utils/networkSwitchPolicy.ts | 4 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 nym-wallet/src/utils/networkSwitchPolicy.test.ts create mode 100644 nym-wallet/src/utils/networkSwitchPolicy.ts diff --git a/nym-wallet/src/context/main.tsx b/nym-wallet/src/context/main.tsx index 73ebb3c8e0..75eca8acaa 100644 --- a/nym-wallet/src/context/main.tsx +++ b/nym-wallet/src/context/main.tsx @@ -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); diff --git a/nym-wallet/src/utils/networkSwitchPolicy.test.ts b/nym-wallet/src/utils/networkSwitchPolicy.test.ts new file mode 100644 index 0000000000..1508ccf7b7 --- /dev/null +++ b/nym-wallet/src/utils/networkSwitchPolicy.test.ts @@ -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); + }); +}); diff --git a/nym-wallet/src/utils/networkSwitchPolicy.ts b/nym-wallet/src/utils/networkSwitchPolicy.ts new file mode 100644 index 0000000000..70dec2ae37 --- /dev/null +++ b/nym-wallet/src/utils/networkSwitchPolicy.ts @@ -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; +}