import { memo, useMemo, useState, PureComponent, useEffect } from 'react' import { SearchIcon } from '@heroicons/react/outline' import Modal from './Modal' import { FixedSizeList } from 'react-window' import { Token } from '../@types/types' 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 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) } type ItemRendererProps = { data: any index: number style: any } class ItemRenderer extends PureComponent { render() { // Access the items array using the "data" prop: const tokenInfo = this.props.data.items[this.props.index] return (
) } } const SwapTokenSelect = ({ isOpen, sortedTokenMints, onClose, onTokenSelect, }: { isOpen: boolean sortedTokenMints: Token[] onClose?: (x?) => void onTokenSelect?: (x?) => void }) => { const [search, setSearch] = useState('') useEffect(() => { function onEscape(e) { if (e.keyCode === 27) { onClose?.() } } window.addEventListener('keydown', onEscape) return () => window.removeEventListener('keydown', onEscape) }, []) const tokenInfos = useMemo(() => { return sortedTokenMints.filter((token) => { return !token?.name || !token?.symbol ? false : true }) }, [sortedTokenMints]) const handleUpdateSearch = (e) => { setSearch(e.target.value) } const sortedTokens = search ? startSearch(tokenInfos, search) : tokenInfos return (
{ItemRenderer}
) } export default memo(SwapTokenSelect)