mango-ui-v3/components/MarketSelect.tsx

112 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-06-23 06:16:37 -07:00
import { useState } from 'react'
import styled from '@emotion/styled'
import { RadioGroup } from '@headlessui/react'
import useLocalStorageState from '../hooks/useLocalStorageState'
import useMangoGroupConfig from '../hooks/useMangoGroupConfig'
2021-04-06 15:11:42 -07:00
import useMangoStore from '../stores/useMangoStore'
import { getMarketByBaseSymbolAndKind } from '@blockworks-foundation/mango-client'
2021-04-06 15:11:42 -07:00
2021-06-23 06:16:37 -07:00
const StyledMarketTypeToggleWrapper = styled.div`
background: rgba(255, 255, 255, 0.12);
`
const StyledArrow = styled.div`
width: 0;
height: 0;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
border-left: 20px solid rgba(255, 255, 255, 0.12);
`
2021-04-06 15:11:42 -07:00
const MarketSelect = () => {
2021-06-23 06:16:37 -07:00
// const [lastViewedMarket, setLastViewedMarket] = useLocalStorageState(
// 'lastViewedMarket',
// { baseSymbol: 'BTC', kind: 'spot' }
// )
const [marketType, setMarketType] = useState('spot')
const groupConfig = useMangoGroupConfig()
const selectedMarket = useMangoStore((s) => s.selectedMarket.config)
2021-04-06 15:11:42 -07:00
const setMangoStore = useMangoStore((s) => s.set)
const handleChange = (symbol, kind) => {
const newMarket = getMarketByBaseSymbolAndKind(groupConfig, symbol, kind)
2021-04-06 15:11:42 -07:00
setMangoStore((state) => {
state.selectedMarket.current = null
state.selectedMarket.config = newMarket
2021-04-06 15:11:42 -07:00
})
2021-06-23 06:16:37 -07:00
// setLastViewedMarket({ baseSymbol: symbol, kind: kind })
2021-04-06 15:11:42 -07:00
}
2021-06-23 06:16:37 -07:00
const markets =
marketType === 'perp' ? groupConfig.perpMarkets : groupConfig.spotMarkets
2021-06-23 06:16:37 -07:00
return (
<div className="bg-th-bkg-3 flex h-10">
<StyledMarketTypeToggleWrapper className="flex items-center pl-6 md:pl-9 pr-1">
<RadioGroup
value={marketType}
onChange={(t) => setMarketType(t)}
className="flex font-bold rounded-md text-xs w-30"
>
<RadioGroup.Option value="spot" className="flex-1 focus:outline-none">
{({ checked }) => (
<button
className={`${
checked
? 'bg-th-primary text-th-bkg-1'
: 'bg-th-bkg-2 hover:bg-th-bkg-1'
} rounded-r-none text-th-fgd-3 text-center py-1 px-2.5 h-full w-full focus:outline-none`}
>
Spot
</button>
)}
</RadioGroup.Option>
<RadioGroup.Option value="perp" className="flex-1 focus:outline-none">
{({ checked }) => (
<button
className={`${
checked
? 'bg-th-primary text-th-bkg-1'
: 'bg-th-bkg-2 hover:bg-th-bkg-1'
} rounded-l-none text-th-fgd-3 text-center py-1 px-2.5 h-full w-full focus:outline-none`}
>
Perp
</button>
)}
</RadioGroup.Option>
</RadioGroup>
</StyledMarketTypeToggleWrapper>
<StyledArrow />
<div className="flex items-center justify-between w-full">
<div className="flex items-center">
{markets.map((s) => (
<div
2021-06-23 06:16:37 -07:00
className={`cursor-pointer default-transition flex font-bold px-4 text-xs hover:text-th-primary
${
selectedMarket.name === s.name
? `text-th-primary`
: `text-th-fgd-3`
}
`}
2021-06-23 06:16:37 -07:00
onClick={() => handleChange(s.baseSymbol, marketType)}
2021-06-18 15:57:13 -07:00
key={s.publicKey.toBase58()}
>
{s.name}
</div>
))}
</div>
<div className="mr-10 text-xs">
<a
2021-06-08 08:04:56 -07:00
href="https://usdt.mango.markets"
2021-06-08 07:59:39 -07:00
className="text-primary default-transition underline hover:text-th-primary hover:no-underline border-1"
>
2021-06-08 07:59:39 -07:00
Go to Mango V1
</a>
</div>
2021-04-12 09:20:17 -07:00
</div>
2021-04-06 15:11:42 -07:00
</div>
)
}
export default MarketSelect