mango-v4-ui/components/account/AccountTabs.tsx

77 lines
2.3 KiB
TypeScript
Raw Normal View History

import { useEffect, useMemo, useState } from 'react'
2022-09-12 08:53:57 -07:00
import mangoStore from '@store/mangoStore'
2022-08-10 04:17:10 -07:00
import TabButtons from '../shared/TabButtons'
import TokenList from '../TokenList'
2022-09-19 03:43:38 -07:00
import SwapHistoryTable from '../swap/SwapHistoryTable'
2022-09-26 23:22:51 -07:00
import ActivityFeed from './ActivityFeed'
2022-10-06 20:16:11 -07:00
import UnsettledTrades from '@components/trade/UnsettledTrades'
import { useUnsettledSpotBalances } from 'hooks/useUnsettledSpotBalances'
2022-11-18 09:09:39 -08:00
import useMangoAccount from 'hooks/useMangoAccount'
2022-11-29 02:58:50 -08:00
import { useViewport } from 'hooks/useViewport'
import { breakpoints } from 'utils/theme'
import useUnsettledPerpPositions from 'hooks/useUnsettledPerpPositions'
2022-08-10 04:17:10 -07:00
2022-10-06 20:16:11 -07:00
const TABS = [
'balances',
'activity:activity',
'swap:swap-history',
'trade:unsettled',
]
2022-08-10 04:17:10 -07:00
const AccountTabs = () => {
2022-09-04 23:32:10 -07:00
const [activeTab, setActiveTab] = useState('balances')
2022-08-12 23:06:09 -07:00
const actions = mangoStore((s) => s.actions)
2022-11-18 09:09:39 -08:00
const { mangoAccount } = useMangoAccount()
2022-11-29 02:58:50 -08:00
const { width } = useViewport()
const isMobile = width ? width < breakpoints.md : false
const tabsWithCount: [string, number][] = useMemo(() => {
return TABS.map((t) => [t, 0])
}, [])
2022-08-10 04:17:10 -07:00
useEffect(() => {
if (mangoAccount) {
2022-09-26 23:22:51 -07:00
actions.fetchSwapHistory(mangoAccount.publicKey.toString())
2022-08-10 04:17:10 -07:00
}
2022-08-13 22:07:53 -07:00
}, [actions, mangoAccount])
2022-08-10 04:17:10 -07:00
return (
<>
2022-09-19 03:43:38 -07:00
<TabButtons
activeValue={activeTab}
onChange={(v) => setActiveTab(v)}
values={tabsWithCount}
2022-09-19 03:43:38 -07:00
showBorders
2022-11-29 02:58:50 -08:00
fillWidth={isMobile}
2022-09-19 03:43:38 -07:00
/>
2022-09-26 23:22:51 -07:00
<TabContent activeTab={activeTab} />
2022-08-10 04:17:10 -07:00
</>
)
}
2022-09-26 23:22:51 -07:00
const TabContent = ({ activeTab }: { activeTab: string }) => {
const swapHistory = mangoStore((s) => s.mangoAccount.stats.swapHistory.data)
const loading = mangoStore((s) => s.mangoAccount.stats.swapHistory.loading)
2022-10-06 20:16:11 -07:00
const unsettledSpotBalances = useUnsettledSpotBalances()
const unsettledPerpPositions = useUnsettledPerpPositions()
2022-09-26 23:22:51 -07:00
switch (activeTab) {
case 'balances':
return <TokenList />
2022-09-28 06:04:58 -07:00
case 'activity:activity':
2022-09-26 23:22:51 -07:00
return <ActivityFeed />
case 'swap:swap-history':
return <SwapHistoryTable swapHistory={swapHistory} loading={loading} />
2022-10-06 20:16:11 -07:00
case 'trade:unsettled':
2022-11-20 18:29:51 -08:00
return (
<UnsettledTrades
unsettledSpotBalances={unsettledSpotBalances}
unsettledPerpPositions={unsettledPerpPositions}
2022-11-20 18:29:51 -08:00
/>
)
2022-09-26 23:22:51 -07:00
default:
return <TokenList />
}
}
2022-08-10 04:17:10 -07:00
export default AccountTabs