mango-ui-v3/components/UserInfo.tsx

127 lines
4.1 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'
import FloatingElement from './FloatingElement'
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-03 05:11:21 -07:00
import Select from './Select'
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',
2021-07-19 10:13:03 -07:00
'Open Orders',
2021-07-24 11:12:52 -07:00
'Perp Positions',
// 'Fees',
2021-07-01 07:12:42 -07:00
'Trade History',
]
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={`pb-4 sm:hidden`}>
<label htmlFor="tabs" className={`sr-only`}>
2021-04-05 14:38:59 -07:00
Select a tab
</label>
2021-09-03 05:11:21 -07:00
<Select onChange={(t) => handleTabChange(t)} value={activeTab}>
2021-04-05 14:38:59 -07:00
{TABS.map((tabName) => (
2021-09-03 05:11:21 -07:00
<Select.Option key={tabName} value={tabName}>
2021-04-05 14:38:59 -07:00
{tabName}
2021-09-03 05:11:21 -07:00
</Select.Option>
2021-04-05 14:38:59 -07:00
))}
2021-09-03 05:11:21 -07:00
</Select>
2021-04-05 14:38:59 -07:00
</div>
<div className={`hidden sm:block`}>
2021-09-09 17:23:02 -07:00
<div className={`border-b border-th-fgd-4 mb-3 flex justify-between`}>
<nav className={`-mb-px flex space-x-6`} aria-label="Tabs">
2021-04-05 14:38:59 -07:00
{TABS.map((tabName) => (
<a
key={tabName}
onClick={() => handleTabChange(tabName)}
2021-08-22 05:45:10 -07:00
className={`whitespace-nowrap pt-2 pb-4 px-1 border-b-2 font-semibold cursor-pointer default-transition relative 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`
}
`}
2021-04-05 14:38:59 -07:00
>
2021-08-22 05:45:10 -07:00
{tabName}{' '}
{tabName === 'Open Orders' && openOrders?.length > 0 ? (
<Count count={openOrders?.length} />
) : null}
{tabName === 'Perp Positions' && perpPositions?.length > 0 ? (
2021-08-22 05:45:10 -07:00
<Count count={perpPositions?.length} />
) : null}
2021-04-05 14:38:59 -07:00
</a>
))}
</nav>
2021-09-09 17:23:02 -07:00
<div>
{connected && mangoAccount ? (
<ManualRefresh className="pl-2" />
) : null}
</div>
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-20">
<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) {
2021-07-19 10:13:03 -07:00
case 'Open Orders':
return <OpenOrdersTable />
2021-06-22 20:32:10 -07:00
case 'Balances':
return <BalancesTable />
case 'Trade History':
return <TradeHistoryTable numTrades={100} />
2021-07-24 11:12:52 -07:00
case 'Perp 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 (
2021-08-22 05:45:10 -07:00
<FloatingElement showConnect>
<div className={!connected ? 'filter blur-sm' : null}>
<UserInfoTabs activeTab={activeTab} setActiveTab={setActiveTab} />
<TabContent activeTab={activeTab} />
</div>
</FloatingElement>
)
}
export default UserInfo