import { Fragment, useCallback, useMemo, useRef, useState } from 'react' import useMangoGroupConfig from '../hooks/useMangoGroupConfig' import { Popover, Transition } from '@headlessui/react' import { SearchIcon } from '@heroicons/react/outline' import { ChevronDownIcon } from '@heroicons/react/solid' import Input from './Input' import { useTranslation } from 'next-i18next' import MarketNavItem from './MarketNavItem' import useMangoStore from '../stores/useMangoStore' const SwitchMarketDropdown = () => { const groupConfig = useMangoGroupConfig() const marketConfig = useMangoStore((s) => s.selectedMarket.config) const baseSymbol = marketConfig.baseSymbol const isPerpMarket = marketConfig.kind === 'perp' const marketsInfo = useMangoStore((s) => s.marketsInfo) const perpMarketsInfo = useMemo( () => marketsInfo .filter((mkt) => mkt?.name.includes('PERP')) .sort((a, b) => b.volumeUsd24h - a.volumeUsd24h), [marketsInfo] ) const spotMarketsInfo = useMemo( () => marketsInfo .filter((mkt) => mkt?.name.includes('USDC')) .sort((a, b) => b.volumeUsd24h - a.volumeUsd24h), [marketsInfo] ) const [suggestions, setSuggestions] = useState([]) const [searchString, setSearchString] = useState('') const buttonRef = useRef(null) const { t } = useTranslation('common') const filteredMarkets = marketsInfo .filter((m) => m.name.toLowerCase().includes(searchString.toLowerCase())) .sort((a, b) => a.name.localeCompare(b.name)) const onSearch = (searchString) => { if (searchString.length > 0) { const newSuggestions = suggestions.filter((v) => v.name.toLowerCase().includes(searchString.toLowerCase()) ) setSuggestions(newSuggestions) } setSearchString(searchString) } const callbackRef = useCallback((inputElement) => { if (inputElement) { const timer = setTimeout(() => inputElement.focus(), 200) return () => clearTimeout(timer) } }, []) return ( {({ open }) => (
{baseSymbol}
{isPerpMarket ? '-' : '/'}
{isPerpMarket ? 'PERP' : groupConfig.quoteSymbol}
onSearch(e.target.value)} prefix={} ref={callbackRef} type="text" value={searchString} />
{searchString.length > 0 ? (
{filteredMarkets.length > 0 ? ( filteredMarkets.map((mkt) => ( setSearchString('')} market={mkt} key={mkt.name} /> )) ) : (

{t('no-markets')}

)}
) : (

{t('futures')}

{t('favorite')}

{perpMarketsInfo.map((mkt) => ( setSearchString('')} market={mkt} key={mkt.name} /> ))}

{t('spot')}

{t('favorite')}

{spotMarketsInfo.map((mkt) => ( setSearchString('')} market={mkt} key={mkt.name} /> ))}
)}
)}
) } export default SwitchMarketDropdown