Skip to content

Commit fab2809

Browse files
committed
memoize selection and active chain
1 parent bd90148 commit fab2809

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

packages/frontend/src/components/EVMFromTokenSelector.tsx

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useEffect } from 'react';
1+
import { useState, useEffect, useMemo } from 'react';
22
import { Loader2, ChevronDown, Check, Search } from 'lucide-react';
33
import { Button } from '@/components/ui/button';
44
import { Input } from '@/components/ui/input';
@@ -150,11 +150,11 @@ export function EVMFromTokenSelector({
150150
onChange(balance);
151151
};
152152

153-
// Get filtered and prioritized sections
154-
const getFilteredSections = () => {
153+
// Get filtered and prioritized sections (memoized to avoid recalculating on every render)
154+
const filteredSections = useMemo(() => {
155155
const term = searchTerm.toLowerCase().trim();
156156

157-
// Filter sections based on search term and mark same-chain tokens as disabled
157+
// Filter sections based on search term
158158
let filtered = CHAIN_SECTIONS.map(section => {
159159
// Filter tokens by symbol or name if search term exists
160160
const tokensToUse = term
@@ -172,30 +172,26 @@ export function EVMFromTokenSelector({
172172
return { ...section, tokens: tokensToUse };
173173
}).filter((section): section is ChainSection => section !== null);
174174

175-
// Prioritize active chain (move to top) - only when dropdown is closed
176-
if (!isOpen) {
177-
const activeChainName = chainId ? CHAIN_ID_TO_NAME[chainId] : null;
178-
if (activeChainName) {
179-
console.info(`🔗 [EVMFromTokenSelector] Active chain ID: ${chainId}, Chain name: ${activeChainName}`);
180-
const activeIndex = filtered.findIndex(s => s.label === activeChainName);
181-
if (activeIndex > 0) {
182-
console.info(`🔝 [EVMFromTokenSelector] Moving ${activeChainName} to top (was at index ${activeIndex})`);
183-
const [activeSection] = filtered.splice(activeIndex, 1);
184-
filtered.unshift(activeSection);
185-
} else if (activeIndex === 0) {
186-
console.info(`✅ [EVMFromTokenSelector] ${activeChainName} already at top`);
187-
} else {
188-
console.info(`⚠️ [EVMFromTokenSelector] Active chain ${activeChainName} not found in sections`);
189-
}
175+
// Prioritize active chain (move to top)
176+
const activeChainName = chainId ? CHAIN_ID_TO_NAME[chainId] : null;
177+
if (activeChainName) {
178+
console.info(`🔗 [EVMFromTokenSelector] Active chain ID: ${chainId}, Chain name: ${activeChainName}`);
179+
const activeIndex = filtered.findIndex(s => s.label === activeChainName);
180+
if (activeIndex > 0) {
181+
console.info(`🔝 [EVMFromTokenSelector] Moving ${activeChainName} to top (was at index ${activeIndex})`);
182+
const [activeSection] = filtered.splice(activeIndex, 1);
183+
filtered.unshift(activeSection);
184+
} else if (activeIndex === 0) {
185+
console.info(`✅ [EVMFromTokenSelector] ${activeChainName} already at top`);
190186
} else {
191-
console.info(`⚠️ [EVMFromTokenSelector] No active chain detected (chainId: ${chainId})`);
187+
console.info(`⚠️ [EVMFromTokenSelector] Active chain ${activeChainName} not found in sections`);
192188
}
189+
} else {
190+
console.info(`⚠️ [EVMFromTokenSelector] No active chain detected (chainId: ${chainId})`);
193191
}
194192

195193
return filtered;
196-
};
197-
198-
const filteredSections = getFilteredSections();
194+
}, [searchTerm, chainId]); // Only recalculate when search term or chain ID changes, not when dropdown opens/closes
199195

200196
return (
201197
<div className="space-y-2">

0 commit comments

Comments
 (0)