diff --git a/components/swap/SwapForm.tsx b/components/swap/SwapForm.tsx index 64e0b9f3..57304d9b 100644 --- a/components/swap/SwapForm.tsx +++ b/components/swap/SwapForm.tsx @@ -327,7 +327,7 @@ const SwapForm = () => { /> { -// const normalizedSearchValue = searchValue.toLowerCase() -// const values = `${item.symbol} ${item.name}`.toLowerCase() +const generateSearchTerm = (item: Token, searchValue: string) => { + const normalizedSearchValue = searchValue.toLowerCase() + const values = `${item.symbol} ${item.name}`.toLowerCase() -// const isMatchingWithSymbol = -// item.symbol.toLowerCase().indexOf(normalizedSearchValue) >= 0 -// const matchingSymbolPercent = isMatchingWithSymbol -// ? normalizedSearchValue.length / item.symbol.length -// : 0 + const isMatchingWithSymbol = + item.symbol.toLowerCase().indexOf(normalizedSearchValue) >= 0 + const matchingSymbolPercent = isMatchingWithSymbol + ? normalizedSearchValue.length / item.symbol.length + : 0 -// return { -// token: item, -// matchingIdx: values.indexOf(normalizedSearchValue), -// matchingSymbolPercent, -// } -// } + return { + token: item, + matchingIdx: values.indexOf(normalizedSearchValue), + matchingSymbolPercent, + } +} -// const startSearch = (items: Token[], searchValue: string) => { -// return items -// .map((item) => generateSearchTerm(item, searchValue)) -// .filter((item) => item.matchingIdx >= 0) -// .sort((i1, i2) => i1.matchingIdx - i2.matchingIdx) -// .sort((i1, i2) => i2.matchingSymbolPercent - i1.matchingSymbolPercent) -// .map((item) => item.token) -// } +const startSearch = (items: Token[], searchValue: string) => { + return items + .map((item) => generateSearchTerm(item, searchValue)) + .filter((item) => item.matchingIdx >= 0) + .sort((i1, i2) => i1.matchingIdx - i2.matchingIdx) + .sort((i1, i2) => i2.matchingSymbolPercent - i1.matchingSymbolPercent) + .map((item) => item.token) +} const TokenItem = ({ token, @@ -114,8 +115,6 @@ const TokenItem = ({ ) } -// const popularTokenSymbols = ['USDC', 'SOL', 'USDT', 'MNGO', 'BTC'] - interface TokenInfoWithAmounts extends Token { amount?: Decimal amountWithBorrow?: Decimal @@ -132,22 +131,14 @@ const SwapFormTokenList = ({ type: 'input' | 'output' | undefined useMargin: boolean }) => { - const { t } = useTranslation(['common', 'swap']) - // const [search, setSearch] = useState('') + const { t } = useTranslation(['common', 'search', 'swap']) + const [search, setSearch] = useState('') const { mangoTokens } = useJupiterMints() const inputBank = mangoStore((s) => s.swap.inputBank) const outputBank = mangoStore((s) => s.swap.outputBank) const { group } = useMangoGroup() const { mangoAccount } = useMangoAccount() - const focusRef = useRef(null) - - // const popularTokens = useMemo(() => { - // return tokens.filter((token) => { - // return !token?.name || !token?.symbol - // ? false - // : popularTokenSymbols.includes(token.symbol) - // }) - // }, [tokens]) + const focusRef = useRef(null) useEffect(() => { function onEscape(e: KeyboardEvent) { @@ -202,12 +193,11 @@ const SwapFormTokenList = ({ } }, [mangoTokens, inputBank, outputBank, mangoAccount, group, useMargin, type]) - // const handleUpdateSearch = (e: ChangeEvent) => { - // setSearch(e.target.value) - // } + const handleUpdateSearch = (e: ChangeEvent) => { + setSearch(e.target.value) + } - // const sortedTokens = search ? startSearch(tokenInfos, search) : tokenInfos - const sortedTokens = tokenInfos + const sortedTokens = search ? startSearch(tokenInfos, search) : tokenInfos useEffect(() => { if (focusRef?.current) { @@ -228,71 +218,43 @@ const SwapFormTokenList = ({ className="absolute top-2 right-2 text-th-fgd-3 hover:text-th-fgd-2" onClick={onClose} hideBg - ref={focusRef} > - {/* No need for search/popular tokens until we have more tokens */} - - {/*
+
} autoFocus value={search} onChange={handleUpdateSearch} + ref={focusRef} /> +
- {popularTokens.length ? ( -
- {popularTokens.map((token) => { - let logoURI - if (mangoTokens.length) { - logoURI = mangoTokens.find( - (t) => t.address === token.address - )!.logoURI - } - const disabled = - (type === 'input' && token.symbol === outputBank?.name) || - (type === 'output' && token.symbol === inputBank?.name) - return ( - - ) - })} -
- ) : null} */} - {/*
*/} -
+

{t('token')}

{type === 'input' ? (

{t('max')}

) : null}
-
- {sortedTokens.map((token) => ( - - ))} +
+ {sortedTokens?.length ? ( + sortedTokens.map((token) => ( + + )) + ) : ( +
+

{t('search:no-results')}

+
+ )}
)