mango-ui-v3/pages/index.tsx

151 lines
5.1 KiB
TypeScript
Raw Normal View History

import { useEffect } from 'react'
import { useRouter } from 'next/router'
import useMangoGroupConfig from '../hooks/useMangoGroupConfig'
2022-01-16 15:16:47 -08:00
import useMangoStore, { serumProgramId } from '../stores/useMangoStore'
import {
getMarketByBaseSymbolAndKind,
getMarketIndexBySymbol,
} from '@blockworks-foundation/mango-client'
import TopBar from '../components/TopBar'
import TradePageGrid from '../components/TradePageGrid'
import useLocalStorageState from '../hooks/useLocalStorageState'
import AlphaModal, { ALPHA_MODAL_KEY } from '../components/AlphaModal'
import { PageBodyWrapper } from '../components/styles'
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,
walletConnectedSelector,
} from '../stores/selectors'
2022-01-16 15:16:47 -08:00
import { PublicKey } from '@solana/web3.js'
2022-02-09 02:55:54 -08:00
import FavoritesShortcutBar from '../components/FavoritesShortcutBar'
export async function getStaticProps({ locale }) {
return {
props: {
...(await serverSideTranslations(locale, [
'common',
'tv-chart',
'alerts',
])),
// 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)
2021-10-29 05:05:55 -07:00
const [showTour] = useLocalStorageState(SHOW_TOUR_KEY, false)
const groupConfig = useMangoGroupConfig()
const setMangoStore = useMangoStore((s) => s.set)
const connected = useMangoStore(walletConnectedSelector)
2022-01-16 15:16:47 -08:00
const mangoAccount = useMangoStore(mangoAccountSelector)
const mangoClient = useMangoStore((s) => s.connection.client)
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-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-01-16 15:16:47 -08:00
useEffect(() => {
async function loadUnownedMangoAccount() {
try {
const unownedMangoAccountPubkey = new PublicKey(pubkey)
if (mangoGroup) {
const unOwnedMangoAccount = await mangoClient.getMangoAccount(
unownedMangoAccountPubkey,
serumProgramId
)
console.log('unOwnedMangoAccount: ', unOwnedMangoAccount)
setMangoStore((state) => {
state.selectedMangoAccount.current = unOwnedMangoAccount
state.selectedMangoAccount.initialLoad = false
state.wallet.connected = true
})
actions.fetchTradeHistory()
actions.reloadOrders()
// setResetOnLeave(true)
}
} catch (error) {
router.push('/account')
}
}
if (pubkey) {
loadUnownedMangoAccount()
}
}, [pubkey, mangoClient, mangoGroup])
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
if (name) {
marketQueryParam = name.toString().split(/-|\//)
marketBaseSymbol = marketQueryParam[0]
marketType = marketQueryParam[1] === '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()
)
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) {
2021-12-09 09:23:19 -08: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'
2022-02-10 09:49:59 -08:00
? parseFloat(
mangoGroup.getPrice(marketIndex, mangoCache).toFixed(2)
)
2021-08-18 13:15:17 -07:00
: ''
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 (
2021-12-14 14:42:02 -08:00
<div className={`bg-th-bkg-1 text-th-fgd-1 transition-all`}>
2021-12-17 04:23:58 -08:00
{showTour && !hideTips ? (
<IntroTips connected={connected} mangoAccount={mangoAccount} />
) : null}
<TopBar />
2022-02-09 02:55:54 -08:00
<FavoritesShortcutBar />
<PageBodyWrapper className="p-1 sm:px-2 sm:py-1 md:px-2 md:py-1 xl:px-4">
<TradePageGrid />
</PageBodyWrapper>
2021-08-29 21:56:27 -07:00
{!alphaAccepted && (
<AlphaModal isOpen={!alphaAccepted} onClose={() => {}} />
)}
</div>
)
}
export default PerpMarket