2021-07-17 06:49:52 -07:00
|
|
|
import { useEffect } from 'react'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import useMangoGroupConfig from '../../hooks/useMangoGroupConfig'
|
|
|
|
import useMangoStore from '../../stores/useMangoStore'
|
2021-08-18 07:23:55 -07:00
|
|
|
import {
|
|
|
|
getMarketByBaseSymbolAndKind,
|
|
|
|
getMarketIndexBySymbol,
|
|
|
|
} from '@blockworks-foundation/mango-client'
|
2021-07-17 06:49:52 -07:00
|
|
|
import TopBar from '../../components/TopBar'
|
|
|
|
import TradePageGrid from '../../components/TradePageGrid'
|
|
|
|
import MarketSelect from '../../components/MarketSelect'
|
2021-08-29 21:56:27 -07:00
|
|
|
import useLocalStorageState from '../../hooks/useLocalStorageState'
|
2021-08-30 11:50:37 -07:00
|
|
|
import AlphaModal, { ALPHA_MODAL_KEY } from '../../components/AlphaModal'
|
2021-09-19 17:36:02 -07:00
|
|
|
import { PageBodyWrapper } from '../../components/styles'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
|
|
|
|
|
|
|
|
export async function getServerSideProps({ locale }) {
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
...(await serverSideTranslations(locale, ['common'])),
|
|
|
|
// Will be passed to the page component as props
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 06:49:52 -07:00
|
|
|
|
|
|
|
const PerpMarket = () => {
|
2021-08-30 11:50:37 -07:00
|
|
|
const [alphaAccepted] = useLocalStorageState(ALPHA_MODAL_KEY, false)
|
2021-07-17 06:49:52 -07:00
|
|
|
const groupConfig = useMangoGroupConfig()
|
|
|
|
const setMangoStore = useMangoStore((s) => s.set)
|
2021-07-21 10:40:42 -07:00
|
|
|
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
|
2021-08-18 07:23:55 -07:00
|
|
|
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
|
2021-08-14 13:05:31 -07:00
|
|
|
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
2021-07-17 06:49:52 -07:00
|
|
|
const router = useRouter()
|
|
|
|
const { market } = router.query
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-07-21 10:40:42 -07:00
|
|
|
if (market && mangoGroup) {
|
2021-07-17 06:49:52 -07:00
|
|
|
const newMarket = getMarketByBaseSymbolAndKind(
|
|
|
|
groupConfig,
|
|
|
|
market.toString().toUpperCase(),
|
|
|
|
'perp'
|
|
|
|
)
|
2021-08-18 07:23:55 -07:00
|
|
|
const marketIndex = getMarketIndexBySymbol(
|
|
|
|
groupConfig,
|
|
|
|
market.toString().toUpperCase()
|
|
|
|
)
|
2021-08-14 13:05:31 -07:00
|
|
|
|
2021-07-17 06:49:52 -07:00
|
|
|
setMangoStore((state) => {
|
2021-08-18 07:23:55 -07:00
|
|
|
state.selectedMarket.kind = 'perp'
|
2021-08-14 13:05:31 -07:00
|
|
|
if (newMarket.name !== marketConfig.name) {
|
2021-08-23 11:04:18 -07:00
|
|
|
state.selectedMarket.current = null
|
2021-08-14 13:05:31 -07:00
|
|
|
state.selectedMarket.config = newMarket
|
2021-08-18 13:15:17 -07:00
|
|
|
state.tradeForm.price =
|
|
|
|
state.tradeForm.tradeType === 'Limit'
|
|
|
|
? mangoGroup.getPrice(marketIndex, mangoCache).toFixed(2)
|
|
|
|
: ''
|
2021-08-14 13:05:31 -07:00
|
|
|
}
|
2021-07-17 06:49:52 -07:00
|
|
|
})
|
|
|
|
}
|
2021-07-21 10:40:42 -07:00
|
|
|
}, [market, mangoGroup])
|
2021-07-17 06:49:52 -07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={`bg-th-bkg-1 text-th-fgd-1 transition-all `}>
|
|
|
|
<TopBar />
|
|
|
|
<MarketSelect />
|
2021-09-19 17:36:02 -07:00
|
|
|
<PageBodyWrapper className="p-1 sm:px-2 sm:py-1 md:px-2 md:py-1">
|
2021-07-17 06:49:52 -07:00
|
|
|
<TradePageGrid />
|
2021-09-19 17:36:02 -07:00
|
|
|
</PageBodyWrapper>
|
2021-08-29 21:56:27 -07:00
|
|
|
{!alphaAccepted && (
|
|
|
|
<AlphaModal isOpen={!alphaAccepted} onClose={() => {}} />
|
|
|
|
)}
|
2021-07-17 06:49:52 -07:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PerpMarket
|