Skip to content

Commit 21b69e0

Browse files
committed
simplify logic
1 parent 2ca7b32 commit 21b69e0

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

src/components/connections/networks-helpers.js

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -191,60 +191,37 @@ export async function switchNetwork(chainId) {
191191
}
192192

193193
export function updateCurrentNetworkDisplay() {
194-
const currentChainId = globalContext.chainIdInt
195-
? globalContext.chainIdInt.toString(16)
196-
: globalContext.chainIdPadded;
197194
const currentNetworkName = document.getElementById('currentNetworkName');
198195

199-
if (currentChainId) {
200-
// Convert chainId to proper format for comparison
201-
let normalizedChainId;
202-
if (typeof currentChainId === 'string') {
203-
if (currentChainId.startsWith('0x')) {
204-
normalizedChainId = currentChainId;
205-
} else {
206-
normalizedChainId = `0x${currentChainId}`;
207-
}
208-
} else {
209-
normalizedChainId = `0x${currentChainId.toString(16)}`;
210-
}
211-
212-
const network = NETWORKS.find((n) => n.chainId === normalizedChainId);
196+
if (
197+
globalContext.chainIdInt !== undefined &&
198+
globalContext.chainIdInt !== null
199+
) {
200+
const network = NETWORKS.find((n) => {
201+
const networkChainId = parseInt(n.chainId, 16);
202+
return networkChainId === globalContext.chainIdInt;
203+
});
213204

214205
if (network) {
215206
currentNetworkName.textContent = `Current Network: ${network.name}`;
216207
} else {
217-
// Extract the actual chain ID from the padded version if needed
218-
let displayChainId = normalizedChainId;
219-
if (normalizedChainId.length > 10) {
220-
// If it's a padded chain ID, try to extract the actual chain ID
221-
const actualChainId = globalContext.chainIdInt
222-
? globalContext.chainIdInt.toString(16)
223-
: null;
224-
if (actualChainId) {
225-
displayChainId = `0x${actualChainId}`;
226-
}
227-
}
228-
currentNetworkName.textContent = `Current Network: Chain ID ${displayChainId}`;
208+
// Fallback to chain ID if network not found
209+
currentNetworkName.textContent = `Current Network: Chain ID 0x${globalContext.chainIdInt.toString(
210+
16,
211+
)}`;
229212
}
230213
} else {
231214
currentNetworkName.textContent = 'Current Network: Not Connected';
232215
}
233216
}
234217

235218
export function updateActiveNetworkInModal() {
236-
const currentChainId =
237-
globalContext.chainIdPadded ||
238-
(globalContext.chainIdInt ? globalContext.chainIdInt.toString(16) : null);
239219
const networkItems = document.querySelectorAll('.network-modal-item');
240220

241221
networkItems.forEach((item) => {
242222
const itemChainId = item.dataset.chainId;
243-
const isActive =
244-
currentChainId &&
245-
(itemChainId === currentChainId ||
246-
itemChainId === `0x${currentChainId}` ||
247-
itemChainId === parseInt(currentChainId, 16).toString(16));
223+
const itemChainIdInt = parseInt(itemChainId, 16);
224+
const isActive = itemChainIdInt === globalContext.chainIdInt;
248225

249226
if (isActive) {
250227
item.classList.add('active');

0 commit comments

Comments
 (0)