mango-ui-v3/components/UserInfo.tsx

109 lines
3.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'
import { useOpenOrders } from '../hooks/useOpenOrders'
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'
// import FeeDiscountsTable from './FeeDiscountsTable'
2021-09-09 17:23:02 -07:00
import ManualRefresh from './ManualRefresh'
2021-04-05 14:38:59 -07:00
2021-07-01 07:12:42 -07:00
const TABS = [
'Balances',
'Orders',
'Positions',
// 'Fees',
'History',
2021-07-01 07:12:42 -07:00
]
2021-04-05 14:38:59 -07:00
const UserInfoTabs = ({ activeTab, setActiveTab }) => {
2021-08-22 05:45:10 -07:00
const openOrders = useOpenOrders()
const perpPositions = usePerpPositions()
2021-09-09 17:23:02 -07:00
const connected = useMangoStore((s) => s.connection.current)
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
2021-04-05 14:38:59 -07:00
const handleTabChange = (tabName) => {
setActiveTab(tabName)
}
return (
<div>
<div className={`border-b border-th-fgd-4 mb-3 flex justify-between`}>
<nav className={`-mb-px flex space-x-3 sm:space-x-6`} aria-label="Tabs">
2021-04-05 14:38:59 -07:00
{TABS.map((tabName) => (
<a
key={tabName}
onClick={() => handleTabChange(tabName)}
className={`whitespace-nowrap pt-2 pb-4 px-1 border-b-2 font-semibold cursor-pointer default-transition relative text-xs sm:text-sm hover:opacity-100
${
activeTab === tabName
2021-04-16 04:50:56 -07:00
? `border-th-primary text-th-primary`
2021-04-16 07:08:33 -07:00
: `border-transparent text-th-fgd-4 hover:text-th-primary`
}
`}
>
{tabName}{' '}
{tabName === 'Orders' && openOrders?.length > 0 ? (
<Count count={openOrders?.length} />
) : null}
{tabName === 'Positions' && perpPositions?.length > 0 ? (
<Count count={perpPositions?.length} />
) : null}
</a>
))}
</nav>
<div>
{connected && mangoAccount ? (
<ManualRefresh className="pl-2" />
) : null}
2021-04-05 14:38:59 -07:00
</div>
</div>
</div>
)
}
2021-08-22 05:45:10 -07:00
const Count = ({ count }) => (
<div className="absolute -top-2 -right-2 z-10">
<span className="h-4 p-1 bg-th-bkg-4 inline-flex rounded-lg items-center justify-center text-th-fgd-2 text-xxs">
2021-08-22 05:45:10 -07:00
{count}
</span>
2021-08-22 05:45:10 -07:00
</div>
)
const TabContent = ({ activeTab }) => {
switch (activeTab) {
case 'Orders':
return <OpenOrdersTable />
2021-06-22 20:32:10 -07:00
case 'Balances':
return <BalancesTable />
case 'History':
return <TradeHistoryTable numTrades={100} />
case 'Positions':
return <PositionsTable />
// case 'Fees':
// return <FeeDiscountsTable /> // now displayed in trade form. may add back when MRSRM deposits are supported
default:
2021-07-19 10:13:03 -07:00
return <BalancesTable />
}
}
const UserInfo = () => {
2021-08-23 07:14:03 -07:00
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const isPerpMarket = marketConfig.kind === 'perp'
2021-08-22 05:45:10 -07:00
const connected = useMangoStore((s) => s.wallet.connected)
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 className={!connected ? 'filter blur-sm' : null}>
<UserInfoTabs activeTab={activeTab} setActiveTab={setActiveTab} />
<TabContent activeTab={activeTab} />
</div>
)
}
export default UserInfo