mango-ui-v3/components/MarketSelect.tsx

98 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-07-22 05:50:50 -07:00
import { useEffect, useState } from 'react'
2021-11-30 03:51:33 -08:00
import { MenuIcon, PlusCircleIcon } from '@heroicons/react/outline'
import useMangoGroupConfig from '../hooks/useMangoGroupConfig'
import MarketMenuItem from './MarketMenuItem'
2021-07-18 07:17:52 -07:00
import { LinkButton } from './Button'
import MarketsModal from './MarketsModal'
import useLocalStorageState from '../hooks/useLocalStorageState'
2021-11-30 03:51:33 -08:00
import { useViewport } from '../hooks/useViewport'
import { breakpoints } from './TradePageGrid'
import { useTranslation } from 'next-i18next'
2021-04-06 15:11:42 -07:00
const MarketSelect = () => {
const { t } = useTranslation('common')
2021-07-18 07:17:52 -07:00
const [showMarketsModal, setShowMarketsModal] = useState(false)
const [hiddenMarkets] = useLocalStorageState('hiddenMarkets', [])
2021-07-22 05:50:50 -07:00
const [sortedMarkets, setSortedMarkets] = useState([])
const groupConfig = useMangoGroupConfig()
2021-11-30 03:51:33 -08:00
const { width } = useViewport()
const isMobile = width ? width < breakpoints.md : false
2021-04-06 15:11:42 -07:00
2021-07-22 05:50:50 -07:00
useEffect(() => {
const markets = []
2021-08-18 05:55:04 -07:00
const allMarkets = [...groupConfig.spotMarkets, ...groupConfig.perpMarkets]
2021-07-22 05:50:50 -07:00
allMarkets.forEach((market) => {
const base = market.name.slice(0, -5)
const found = markets.find((b) => b.baseAsset === base)
if (!found) {
markets.push({ baseAsset: base, markets: [market] })
} else {
found.markets.push(market)
}
})
2021-08-16 11:00:04 -07:00
setSortedMarkets(markets)
}, [groupConfig])
2021-07-18 07:17:52 -07:00
2021-06-23 06:16:37 -07:00
return (
<div className="hidden md:flex">
2022-02-01 16:58:47 -08:00
<div className="bg-th-bkg-3 flex h-10 w-full">
2021-11-30 03:51:33 -08:00
<div className="bg-th-bkg-4 flex items-center pl-4 lg:pl-9 pr-1">
{isMobile ? (
<MenuIcon
className="cursor-pointer h-5 text-th-fgd-1 w-5 hover:text-th-primary"
2021-11-30 03:51:33 -08:00
onClick={() => setShowMarketsModal(true)}
/>
) : (
<ShowMarketsButton
onClick={() => setShowMarketsModal(true)}
t={t}
/>
)}
2021-07-29 06:19:32 -07:00
</div>
<div
style={{
width: '0',
height: '0',
borderTop: '20px solid transparent',
borderBottom: '20px solid transparent',
paddingRight: '0.5rem',
}}
className="border-l-[20px] border-th-bkg-4"
/>
2022-01-17 13:46:04 -08:00
<div className="flex items-center justify-between w-full">
<div className="flex items-center">
2021-07-18 07:17:52 -07:00
{sortedMarkets
.filter((m) => !hiddenMarkets.includes(m.baseAsset))
.map((s) => (
<MarketMenuItem
key={s.baseAsset}
linksArray={s.markets}
menuTitle={s.baseAsset}
/>
))}
</div>
</div>
</div>
2021-07-18 07:17:52 -07:00
{showMarketsModal ? (
<MarketsModal
isOpen={showMarketsModal}
onClose={() => setShowMarketsModal(false)}
markets={sortedMarkets}
/>
) : null}
</div>
2021-04-06 15:11:42 -07:00
)
}
const ShowMarketsButton = ({ onClick, t }) => (
2021-08-31 05:46:47 -07:00
<LinkButton
className="font-normal flex items-center text-th-fgd-2 text-xs whitespace-nowrap"
2021-08-31 05:46:47 -07:00
onClick={onClick}
>
<PlusCircleIcon className="h-4 mr-1 w-4" />
{t('markets').toUpperCase()}
2021-08-31 05:46:47 -07:00
</LinkButton>
)
2021-04-06 15:11:42 -07:00
export default MarketSelect