mango-ui-v3/components/UserInfo.tsx

92 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-08-20 04:51:29 -07:00
import { useEffect, useState } from 'react'
2021-08-22 05:45:10 -07:00
import useMangoStore from '../stores/useMangoStore'
2021-11-09 02:40:07 -08:00
import usePerpPositions from '../hooks/usePerpPositions'
2021-04-05 14:38:59 -07:00
import OpenOrdersTable from './OpenOrdersTable'
2021-06-22 20:32:10 -07:00
import BalancesTable from './BalancesTable'
import PositionsTable from './PerpPositionsTable'
import TradeHistoryTable from './TradeHistoryTable'
2021-09-09 17:23:02 -07:00
import ManualRefresh from './ManualRefresh'
2021-09-20 05:24:42 -07:00
import Tabs from './Tabs'
2021-10-18 13:38:03 -07:00
import FeeDiscountsTable from './FeeDiscountsTable'
import useMangoAccount from '../hooks/useMangoAccount'
2021-12-06 12:14:29 -08:00
import { marketConfigSelector } from '../stores/selectors'
2021-04-05 14:38:59 -07:00
2021-07-01 07:12:42 -07:00
const TABS = [
'Balances',
'Orders',
'Positions',
2021-09-20 05:24:42 -07:00
'Trade History',
2021-10-18 13:38:03 -07:00
'Fee Discount',
2021-07-01 07:12:42 -07:00
]
2021-04-05 14:38:59 -07:00
const UserInfoTabs = ({ activeTab, setActiveTab }) => {
2022-01-27 08:57:36 -08:00
const totalOpenOrders = useMangoStore(
(s) => s.selectedMangoAccount.totalOpenOrders
)
2021-11-09 02:40:07 -08:00
const { openPositions } = usePerpPositions()
const { mangoAccount } = useMangoAccount()
2021-04-05 14:38:59 -07:00
const handleTabChange = (tabName) => {
setActiveTab(tabName)
}
return (
2021-12-07 20:00:02 -08:00
<div className="pb-2 relative">
2021-09-20 05:24:42 -07:00
<Tabs
activeTab={activeTab}
onChange={handleTabChange}
showCount={
2022-01-27 08:57:36 -08:00
totalOpenOrders > 0 && openPositions
2021-11-09 02:40:07 -08:00
? [
2022-01-27 08:57:36 -08:00
{ tabName: 'Orders', count: totalOpenOrders },
2021-11-09 02:40:07 -08:00
{ tabName: 'Positions', count: openPositions.length },
]
2021-09-20 05:24:42 -07:00
: null
}
tabs={TABS}
/>
2021-12-06 12:14:29 -08:00
{mangoAccount ? (
2021-09-20 05:24:42 -07:00
<div className="absolute right-0 top-0">
<ManualRefresh />
2021-04-05 14:38:59 -07:00
</div>
2021-09-20 05:24:42 -07:00
) : null}
2021-04-05 14:38:59 -07:00
</div>
)
}
const TabContent = ({ activeTab }) => {
switch (activeTab) {
case 'Orders':
return <OpenOrdersTable />
2021-06-22 20:32:10 -07:00
case 'Balances':
return <BalancesTable />
2021-09-20 06:15:44 -07:00
case 'Trade History':
return <TradeHistoryTable numTrades={100} />
case 'Positions':
return <PositionsTable />
2021-10-18 13:38:03 -07:00
case 'Fee Discount':
return <FeeDiscountsTable />
default:
2021-07-19 10:13:03 -07:00
return <BalancesTable />
}
}
const UserInfo = () => {
2021-12-05 17:13:23 -08:00
const marketConfig = useMangoStore(marketConfigSelector)
2021-08-23 07:14:03 -07:00
const isPerpMarket = marketConfig.kind === 'perp'
2021-08-20 04:51:29 -07:00
const [activeTab, setActiveTab] = useState('')
useEffect(() => {
2021-08-23 07:14:03 -07:00
isPerpMarket ? setActiveTab(TABS[2]) : setActiveTab(TABS[0])
}, [isPerpMarket])
return (
<div>
<UserInfoTabs activeTab={activeTab} setActiveTab={setActiveTab} />
<TabContent activeTab={activeTab} />
</div>
)
}
export default UserInfo