import { useAllTokenBalances } from "../../hooks/apis"; import { Currency } from "../../types"; import { useContext, useEffect } from "react"; import { useState } from "react"; import { Check, ChevronDown } from "react-feather"; import { CustomizeContext } from "../../providers/CustomizeProvider"; import { Modal } from "../common/Modal"; import { useTransition } from "@react-spring/web"; import { SearchBar } from "./SearchBar"; interface Props { activeToken: Currency; updateToken: (token: Currency) => void; tokens: Currency[]; tokenToDisable?: Currency; } export const TokenSelect = (props: Props) => { const { activeToken, updateToken, tokens, tokenToDisable } = props; const [openTokenList, setOpenTokenList] = useState(false); const [filteredTokens, setFilteredTokens] = useState(null); const [displayTokens, setDisplayTokens] = useState(null); const customSettings = useContext(CustomizeContext); const { borderRadius } = customSettings.customization; const transitions = useTransition(openTokenList, { from: { y: "100%" }, enter: { y: "0" }, leave: { y: "100%" }, config: { duration: 200 }, onReset: () => setOpenTokenList(false), }); // Hook that gives you all the balances for a user on all chains. const { data: tokensWithBalances } = useAllTokenBalances(); function selectToken(token: Currency) { updateToken(token); setOpenTokenList(false); } function showBalance(token: Currency) { const _token = tokensWithBalances?.filter( (x) => x.address.toLowerCase() === token.address.toLowerCase() && x.chainId === token.chainId ); if (_token?.[0]) { return _token[0].amount.toFixed(5); } else return ""; } // compare tokens with tokensWithBalances and return the tokens with balance function getTokenWithBalance(token: Currency) { const filteredTokens = tokensWithBalances?.filter( (x) => x.address.toLowerCase() === token.address.toLowerCase() && x.chainId === token.chainId ); return filteredTokens?.length > 0; } useEffect(() => { // Filtering out tokens with balance const tokensWithBalance = tokens?.filter((token: Currency) => getTokenWithBalance(token) ); // Sorting the tokens as per amount const sortedTokens = tokensWithBalance?.sort((a, b) => { // corresponding tokens with balances const _tokenA = tokensWithBalances.filter( (x) => x.address === a.address )[0]; const _tokenB = tokensWithBalances.filter( (x) => x.address === b.address )[0]; return _tokenB?.amount - _tokenA?.amount; }); // Filtering out tokens without balance const restTokens = tokens?.filter((x) => !sortedTokens?.includes(x)); // Merging both the lists const _filteredTokens = sortedTokens && restTokens && [...sortedTokens, ...restTokens]; setFilteredTokens(_filteredTokens); setDisplayTokens(_filteredTokens); }, [tokens, tokensWithBalances]); const [searchInput, setSearchInput] = useState(""); function handleSearchInput(searchKeyword) { setSearchInput(searchKeyword); } useEffect(() => { const _filteredTokens = filteredTokens?.filter( (x: Currency) => x?.symbol?.toLowerCase()?.includes(searchInput.toLowerCase()) || x?.address?.toLowerCase() === searchInput.toLowerCase() ); setDisplayTokens(_filteredTokens); }, [searchInput]); return (
{activeToken ? ( ) : (
)} {transitions( (style, item) => item && ( { setOpenTokenList(false); handleSearchInput(""); }} style={style} >
handleSearchInput(e)} />
{displayTokens?.map((token: Currency) => { const isActiveToken = token?.address?.toLowerCase() === activeToken?.address?.toLowerCase(); return ( ); })}
) )}
); };