mango-ui-v3/components/UserInfo.tsx

79 lines
2.2 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-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-12-06 12:14:29 -08:00
import { marketConfigSelector } from '../stores/selectors'
2021-04-05 14:38:59 -07:00
2022-03-24 16:56:32 -07:00
const TABS = ['Balances', 'Orders', 'Positions', 'Trade History']
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
)
2022-01-27 19:57:18 -08:00
const totalOpenPerpPositions = useMangoStore(
(s) => s.selectedMangoAccount.totalOpenPerpPositions
)
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
2021-04-05 14:38:59 -07:00
const handleTabChange = (tabName) => {
setActiveTab(tabName)
}
return (
<div className="relative pb-1">
2021-09-20 05:24:42 -07:00
<Tabs
activeTab={activeTab}
onChange={handleTabChange}
2022-01-27 19:57:18 -08:00
showCount={[
{ tabName: 'Orders', count: totalOpenOrders },
{ tabName: 'Positions', count: totalOpenPerpPositions },
]}
2021-09-20 05:24:42 -07:00
tabs={TABS}
/>
2021-12-06 12:14:29 -08:00
{mangoAccount ? (
<div className="absolute right-0 top-0 -mt-1">
2021-09-20 05:24:42 -07:00
<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 clickToPopulateTradeForm />
2021-09-20 06:15:44 -07:00
case 'Trade History':
return <TradeHistoryTable numTrades={100} />
case 'Positions':
return <PositionsTable />
default:
return <BalancesTable clickToPopulateTradeForm />
}
}
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