mango-ui-v3/pages/index.tsx

189 lines
6.0 KiB
TypeScript
Raw Normal View History

2022-05-23 21:39:40 -07:00
import React, { useCallback, useEffect, useState } from 'react'
import { useRouter } from 'next/router'
2022-01-16 15:16:47 -08:00
import useMangoStore, { serumProgramId } from '../stores/useMangoStore'
import {
getMarketByBaseSymbolAndKind,
getMarketIndexBySymbol,
} from '@blockworks-foundation/mango-client'
import TradePageGrid from '../components/TradePageGrid'
import useLocalStorageState from '../hooks/useLocalStorageState'
import AlphaModal, { ALPHA_MODAL_KEY } from '../components/AlphaModal'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
2021-11-23 15:08:14 -08:00
import IntroTips, { SHOW_TOUR_KEY } from '../components/IntroTips'
import { useViewport } from '../hooks/useViewport'
import { breakpoints } from '../components/TradePageGrid'
import {
2022-01-16 15:16:47 -08:00
actionsSelector,
mangoAccountSelector,
marketConfigSelector,
} from '../stores/selectors'
2022-01-16 15:16:47 -08:00
import { PublicKey } from '@solana/web3.js'
import { useWallet } from '@solana/wallet-adapter-react'
2022-05-23 21:39:40 -07:00
import AccountsModal from 'components/AccountsModal'
2022-06-11 16:26:02 -07:00
import dayjs from 'dayjs'
2022-06-30 20:44:04 -07:00
import { tokenPrecision } from 'utils'
2022-05-23 21:39:40 -07:00
const DISMISS_CREATE_ACCOUNT_KEY = 'show-create-account'
export async function getStaticProps({ locale }) {
return {
props: {
...(await serverSideTranslations(locale, [
'common',
'delegate',
'tv-chart',
'alerts',
2022-04-20 11:28:53 -07:00
'share-modal',
2022-05-10 20:29:35 -07:00
'profile',
])),
// Will be passed to the page component as props
},
}
}
const PerpMarket: React.FC = () => {
2021-08-30 11:50:37 -07:00
const [alphaAccepted] = useLocalStorageState(ALPHA_MODAL_KEY, false)
2021-10-29 05:05:55 -07:00
const [showTour] = useLocalStorageState(SHOW_TOUR_KEY, false)
2022-05-23 21:39:40 -07:00
const [dismissCreateAccount, setDismissCreateAccount] = useLocalStorageState(
DISMISS_CREATE_ACCOUNT_KEY,
false
)
const [showCreateAccount, setShowCreateAccount] = useState(false)
const { connected } = useWallet()
2022-03-31 07:58:01 -07:00
const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const setMangoStore = useMangoStore((s) => s.set)
2022-01-16 15:16:47 -08:00
const mangoAccount = useMangoStore(mangoAccountSelector)
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const marketConfig = useMangoStore(marketConfigSelector)
2022-01-16 15:16:47 -08:00
const actions = useMangoStore(actionsSelector)
const router = useRouter()
2022-06-11 16:26:02 -07:00
const [savedLanguage] = useLocalStorageState('language', '')
2022-01-16 15:16:47 -08:00
const { pubkey } = router.query
2021-10-31 04:21:22 -07:00
const { width } = useViewport()
const hideTips = width ? width < breakpoints.md : false
2022-06-11 16:26:02 -07:00
useEffect(() => {
dayjs.locale(savedLanguage == 'zh_tw' ? 'zh-tw' : savedLanguage)
})
2022-06-16 14:52:54 -07:00
2022-05-23 21:39:40 -07:00
useEffect(() => {
if (connected && !mangoAccount && !dismissCreateAccount) {
setShowCreateAccount(true)
}
}, [connected, mangoAccount])
const handleCloseCreateAccount = useCallback(() => {
setShowCreateAccount(false)
setDismissCreateAccount(true)
}, [])
2022-01-16 15:16:47 -08:00
useEffect(() => {
async function loadUnownedMangoAccount() {
2022-03-30 10:10:15 -07:00
if (!pubkey) return
2022-01-16 15:16:47 -08:00
try {
const unownedMangoAccountPubkey = new PublicKey(pubkey)
const mangoClient = useMangoStore.getState().connection.client
2022-01-16 15:16:47 -08:00
if (mangoGroup) {
const unOwnedMangoAccount = await mangoClient.getMangoAccount(
unownedMangoAccountPubkey,
serumProgramId
)
setMangoStore((state) => {
state.selectedMangoAccount.current = unOwnedMangoAccount
state.selectedMangoAccount.initialLoad = false
})
actions.fetchTradeHistory()
actions.reloadOrders()
// setResetOnLeave(true)
}
} catch (error) {
router.push('/account')
}
}
if (pubkey) {
loadUnownedMangoAccount()
}
}, [pubkey, mangoGroup])
2022-01-16 15:16:47 -08:00
useEffect(() => {
const name = decodeURIComponent(router.asPath).split('name=')[1]
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
2021-12-08 09:47:36 -08:00
let marketQueryParam, marketBaseSymbol, marketType, newMarket, marketIndex
2022-03-30 10:10:15 -07:00
if (name && groupConfig) {
2021-12-08 09:47:36 -08:00
marketQueryParam = name.toString().split(/-|\//)
marketBaseSymbol = marketQueryParam[0]
2022-04-15 13:55:39 -07:00
marketType = marketQueryParam[1]?.includes('PERP') ? 'perp' : 'spot'
2021-12-08 09:47:36 -08:00
newMarket = getMarketByBaseSymbolAndKind(
groupConfig,
marketBaseSymbol.toUpperCase(),
marketType
)
2021-12-08 09:47:36 -08:00
marketIndex = getMarketIndexBySymbol(
groupConfig,
marketBaseSymbol.toUpperCase()
)
2022-03-16 08:02:18 -07:00
if (!newMarket?.baseSymbol) {
router.push('/')
return
}
2021-12-08 09:47:36 -08:00
}
2022-03-20 01:07:03 -07:00
if (newMarket?.name === marketConfig?.name) return
2021-12-08 09:47:36 -08:00
if (name && mangoGroup) {
const mangoCache = useMangoStore.getState().selectedMangoGroup.cache
setMangoStore((state) => {
state.selectedMarket.kind = marketType
2021-08-14 13:05:31 -07:00
if (newMarket.name !== marketConfig.name) {
state.selectedMarket.config = newMarket
2022-06-30 20:44:04 -07:00
state.tradeForm.price = mangoCache
? parseFloat(
mangoGroup.getPrice(marketIndex, mangoCache).toFixed(2)
)
: ''
if (state.tradeForm.quoteSize) {
state.tradeForm.baseSize = Number(
(
state.tradeForm.quoteSize / Number(state.tradeForm.price)
).toFixed(tokenPrecision[newMarket.baseSymbol])
)
}
2021-08-14 13:05:31 -07:00
}
})
2021-12-08 09:47:36 -08:00
} else if (name && marketConfig) {
// if mangoGroup hasn't loaded yet, set the marketConfig to the query param if different
if (newMarket.name !== marketConfig.name) {
setMangoStore((state) => {
state.selectedMarket.kind = marketType
state.selectedMarket.config = newMarket
})
}
}
}, [router, marketConfig])
return (
<>
2022-06-13 17:13:25 -07:00
{showTour && !hideTips ? (
<IntroTips connected={connected} mangoAccount={mangoAccount} />
) : null}
<TradePageGrid />
{!alphaAccepted && (
<AlphaModal isOpen={!alphaAccepted} onClose={() => {}} />
)}
{showCreateAccount ? (
<AccountsModal
isOpen={showCreateAccount}
onClose={() => handleCloseCreateAccount()}
/>
) : null}
</>
)
}
export default PerpMarket