2021-07-22 05:50:50 -07:00
|
|
|
import { useEffect, useState } from 'react'
|
2021-06-23 06:16:37 -07:00
|
|
|
import styled from '@emotion/styled'
|
2021-08-31 05:46:47 -07:00
|
|
|
import { PlusCircleIcon } from '@heroicons/react/outline'
|
2021-06-18 15:38:09 -07:00
|
|
|
import useMangoGroupConfig from '../hooks/useMangoGroupConfig'
|
2021-08-31 05:46:47 -07:00
|
|
|
import useMangoStore from '../stores/useMangoStore'
|
2021-07-17 06:49:52 -07:00
|
|
|
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-04-06 15:11:42 -07:00
|
|
|
|
2021-07-23 07:07:05 -07:00
|
|
|
const StyledMarketSelectWrapper = styled.div`
|
|
|
|
-ms-overflow-style: none;
|
|
|
|
scrollbar-width: none;
|
|
|
|
|
|
|
|
::-webkit-scrollbar {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2021-06-23 06:16:37 -07:00
|
|
|
const StyledArrow = styled.div`
|
|
|
|
width: 0;
|
|
|
|
height: 0;
|
|
|
|
border-top: 20px solid transparent;
|
|
|
|
border-bottom: 20px solid transparent;
|
2021-07-17 06:49:52 -07:00
|
|
|
padding-right: 0.5rem;
|
2021-06-23 06:16:37 -07:00
|
|
|
`
|
|
|
|
|
2021-04-06 15:11:42 -07:00
|
|
|
const MarketSelect = () => {
|
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([])
|
2021-06-18 15:38:09 -07:00
|
|
|
const groupConfig = useMangoGroupConfig()
|
2021-08-31 05:46:47 -07:00
|
|
|
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
|
|
|
const baseSymbol = marketConfig.baseSymbol
|
|
|
|
const isPerpMarket = marketConfig.kind === 'perp'
|
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 (
|
2021-09-19 17:36:02 -07:00
|
|
|
<div className="hidden md:flex">
|
|
|
|
<StyledMarketSelectWrapper className="bg-th-bkg-3 flex h-10 w-full">
|
2021-07-29 06:19:32 -07:00
|
|
|
<div className="bg-th-bkg-4 flex items-center pl-6 md:pl-9 pr-1">
|
2021-08-31 05:46:47 -07:00
|
|
|
<ShowMarketsButton onClick={() => setShowMarketsModal(true)} />
|
2021-07-29 06:19:32 -07:00
|
|
|
</div>
|
|
|
|
<StyledArrow className="border-l-[20px] border-th-bkg-4" />
|
2021-07-18 07:17:52 -07:00
|
|
|
<div className="flex items-center justify-between w-full">
|
|
|
|
<div className="flex items-center">
|
|
|
|
{sortedMarkets
|
|
|
|
.filter((m) => !hiddenMarkets.includes(m.baseAsset))
|
|
|
|
.map((s) => (
|
|
|
|
<MarketMenuItem
|
|
|
|
key={s.baseAsset}
|
|
|
|
linksArray={s.markets}
|
|
|
|
menuTitle={s.baseAsset}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
2021-04-22 08:33:40 -07:00
|
|
|
</div>
|
2021-07-23 07:07:05 -07:00
|
|
|
</StyledMarketSelectWrapper>
|
2021-08-31 05:46:47 -07:00
|
|
|
<div className="bg-th-bkg-3 flex items-center justify-between py-3 px-6 sm:hidden">
|
|
|
|
<div className="flex items-center">
|
|
|
|
<img
|
|
|
|
alt=""
|
|
|
|
width="24"
|
|
|
|
height="24"
|
|
|
|
src={`/assets/icons/${baseSymbol.toLowerCase()}.svg`}
|
|
|
|
className={`mr-2.5`}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div className="font-semibold pr-0.5 text-xl">{baseSymbol}</div>
|
|
|
|
<span className="text-th-fgd-4 text-xl">
|
|
|
|
{isPerpMarket ? '-' : '/'}
|
|
|
|
</span>
|
|
|
|
<div className="font-semibold pl-0.5 text-xl">
|
|
|
|
{isPerpMarket ? 'PERP' : groupConfig.quoteSymbol}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<ShowMarketsButton onClick={() => setShowMarketsModal(true)} />
|
|
|
|
</div>
|
2021-07-18 07:17:52 -07:00
|
|
|
{showMarketsModal ? (
|
|
|
|
<MarketsModal
|
|
|
|
isOpen={showMarketsModal}
|
|
|
|
onClose={() => setShowMarketsModal(false)}
|
|
|
|
markets={sortedMarkets}
|
|
|
|
/>
|
|
|
|
) : null}
|
2021-09-19 17:36:02 -07:00
|
|
|
</div>
|
2021-04-06 15:11:42 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-31 05:46:47 -07:00
|
|
|
const ShowMarketsButton = ({ onClick }) => (
|
|
|
|
<LinkButton
|
|
|
|
className="font-normal flex items-center text-th-fgd-2 text-xs"
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
|
|
|
<PlusCircleIcon className="h-4 mr-1 w-4" />
|
|
|
|
MARKETS
|
|
|
|
</LinkButton>
|
|
|
|
)
|
|
|
|
|
2021-04-06 15:11:42 -07:00
|
|
|
export default MarketSelect
|