Skip to content

Commit 5ec5d6f

Browse files
committed
fix(frontend): use localStorage as the single source of truth for network toggle
The previous version wrote both a ?net= URL param and localStorage, which could desync (e.g. remove the param, reload, and the localStorage value still forced the other network). Drop the URL param entirely: read the active network from localStorage (fallback to the build's network) and persist the explicit choice on switch, then reload. Verified both directions survive a manual reload.
1 parent c1cb87e commit 5ec5d6f

1 file changed

Lines changed: 7 additions & 28 deletions

File tree

‎frontend/src/config/env.ts‎

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,8 @@ const getStoredNetwork = (): NetworkKey | null => {
7878
}
7979
};
8080

81-
// The URL param is the source of truth (survives navigation reliably, unlike
82-
// localStorage in some embedded contexts). localStorage is a secondary fallback.
83-
const getUrlNetwork = (): NetworkKey | null => {
84-
try {
85-
const v = new URLSearchParams(window.location.search).get('net');
86-
return v === 'mainnet' || v === 'hoodi' ? v : null;
87-
} catch {
88-
return null;
89-
}
90-
};
91-
92-
// Active network: ?net= param wins, then localStorage, then the build's network.
93-
const network = getUrlNetwork() ?? getStoredNetwork() ?? BUILD_NETWORK;
81+
// Single source of truth: localStorage. Falls back to the build's network.
82+
const network = getStoredNetwork() ?? BUILD_NETWORK;
9483
const profile = NETWORKS[network];
9584

9685
// VITE_* overrides are only honoured for the network the build targeted; when the
@@ -120,24 +109,14 @@ export type { NetworkKey };
120109

121110
export const ACTIVE_NETWORK: NetworkKey = network;
122111

123-
// Switch the active network: encode it in the URL (?net=) and navigate so all
124-
// providers re-init against it. The URL param survives the navigation reliably;
125-
// localStorage is updated too as a best-effort fallback.
112+
// Switch the active network: persist the explicit choice in localStorage and
113+
// reload so every provider re-inits against it.
126114
export const setActiveNetwork = (key: NetworkKey): void => {
115+
if (key === network) return;
127116
try {
128-
if (key === BUILD_NETWORK) {
129-
localStorage.removeItem(ACTIVE_NETWORK_KEY);
130-
} else {
131-
localStorage.setItem(ACTIVE_NETWORK_KEY, key);
132-
}
117+
localStorage.setItem(ACTIVE_NETWORK_KEY, key);
133118
} catch {
134119
// ignore storage errors
135120
}
136-
const url = new URL(window.location.href);
137-
if (key === BUILD_NETWORK) {
138-
url.searchParams.delete('net');
139-
} else {
140-
url.searchParams.set('net', key);
141-
}
142-
window.location.assign(url.toString());
121+
window.location.reload();
143122
};

0 commit comments

Comments
 (0)