mango-ui-v3/pages/perp/[market].tsx

77 lines
2.5 KiB
TypeScript
Raw Normal View History

import { useEffect } from 'react'
import { useRouter } from 'next/router'
import useMangoGroupConfig from '../../hooks/useMangoGroupConfig'
import useMangoStore from '../../stores/useMangoStore'
import {
getMarketByBaseSymbolAndKind,
getMarketIndexBySymbol,
} from '@blockworks-foundation/mango-client'
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'
import { PageBodyWrapper } from '../../components/styles'
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
},
}
}
const PerpMarket = () => {
2021-08-30 11:50:37 -07:00
const [alphaAccepted] = useLocalStorageState(ALPHA_MODAL_KEY, false)
const groupConfig = useMangoGroupConfig()
const setMangoStore = useMangoStore((s) => s.set)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache)
2021-08-14 13:05:31 -07:00
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const router = useRouter()
const { market } = router.query
useEffect(() => {
if (market && mangoGroup) {
const newMarket = getMarketByBaseSymbolAndKind(
groupConfig,
market.toString().toUpperCase(),
'perp'
)
const marketIndex = getMarketIndexBySymbol(
groupConfig,
market.toString().toUpperCase()
)
2021-08-14 13:05:31 -07:00
setMangoStore((state) => {
state.selectedMarket.kind = 'perp'
2021-08-14 13:05:31 -07:00
if (newMarket.name !== marketConfig.name) {
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
}
})
}
}, [market, mangoGroup])
return (
<div className={`bg-th-bkg-1 text-th-fgd-1 transition-all `}>
<TopBar />
<MarketSelect />
<PageBodyWrapper className="p-1 sm:px-2 sm:py-1 md:px-2 md:py-1">
<TradePageGrid />
</PageBodyWrapper>
2021-08-29 21:56:27 -07:00
{!alphaAccepted && (
<AlphaModal isOpen={!alphaAccepted} onClose={() => {}} />
)}
</div>
)
}
export default PerpMarket