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

226 lines
7.2 KiB
TypeScript
Raw Normal View History

import { toUiDecimalsForQuote } from '@blockworks-foundation/mango-v4'
2022-09-01 10:33:29 -07:00
import { useTranslation } from 'next-i18next'
2023-07-11 06:14:44 -07:00
import { useCallback, useMemo, useState } from 'react'
2022-09-01 10:33:29 -07:00
import AccountActions from './AccountActions'
import AccountTabs from './AccountTabs'
import AccountChart from './AccountChart'
2022-11-18 09:09:39 -08:00
import useMangoAccount from '../../hooks/useMangoAccount'
2022-09-21 21:25:24 -07:00
import useLocalStorageState from 'hooks/useLocalStorageState'
2022-11-20 16:50:25 -08:00
import dayjs from 'dayjs'
2022-12-21 03:07:17 -08:00
import { useViewport } from 'hooks/useViewport'
import useMangoGroup from 'hooks/useMangoGroup'
2023-01-15 21:13:34 -08:00
import PnlHistoryModal from '@components/modals/PnlHistoryModal'
2023-05-01 19:01:14 -07:00
import AssetsLiabilities from './AssetsLiabilities'
2023-07-03 20:24:13 -07:00
import FundingChart from './FundingChart'
import VolumeChart from './VolumeChart'
import AccountHeroStats from './AccountHeroStats'
import AccountValue from './AccountValue'
import useAccountPerformanceData from 'hooks/useAccountPerformanceData'
2023-07-10 04:56:38 -07:00
import HealthContributions from './HealthContributions'
2023-07-11 05:00:23 -07:00
import { PerformanceDataItem } from 'types'
2023-07-11 06:14:44 -07:00
import { useRouter } from 'next/router'
2023-07-14 06:34:09 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
2023-05-01 19:01:14 -07:00
const TABS = ['account-value', 'account:assets-liabilities']
2023-07-11 05:00:23 -07:00
export type ViewToShow =
| ''
| 'account-value'
| 'cumulative-interest-value'
| 'pnl'
| 'hourly-funding'
2023-07-03 20:24:13 -07:00
| 'hourly-volume'
2023-07-10 04:56:38 -07:00
| 'health-contributions'
2022-09-01 10:33:29 -07:00
const AccountPage = () => {
2023-01-16 20:59:51 -08:00
const { t } = useTranslation(['common', 'account'])
const { group } = useMangoGroup()
const { mangoAccount } = useMangoAccount()
2023-01-15 21:13:34 -08:00
const [showPnlHistory, setShowPnlHistory] = useState<boolean>(false)
2023-08-31 20:22:50 -07:00
const { isTablet } = useViewport()
2023-02-05 18:37:31 -08:00
const [activeTab, setActiveTab] = useLocalStorageState(
'accountHeroKey-0.1',
2023-07-21 11:47:53 -07:00
'account-value',
2023-02-05 18:37:31 -08:00
)
const { performanceData, rollingDailyData } = useAccountPerformanceData()
2023-07-11 06:14:44 -07:00
const router = useRouter()
const { view } = router.query
const handleViewChange = useCallback(
(view: ViewToShow) => {
const query = { ...router.query, ['view']: view }
router.push({ pathname: router.pathname, query })
},
2023-07-21 11:47:53 -07:00
[router],
2023-07-11 06:14:44 -07:00
)
2022-09-01 10:33:29 -07:00
2023-01-16 02:07:46 -08:00
const handleCloseDailyPnlModal = () => {
setShowPnlHistory(false)
}
2023-06-05 22:29:04 -07:00
const [accountPnl, accountValue] = useMemo(() => {
if (!group || !mangoAccount) return [0, 0]
return [
toUiDecimalsForQuote(mangoAccount.getPnl(group).toNumber()),
toUiDecimalsForQuote(mangoAccount.getEquity(group).toNumber()),
]
2022-11-20 16:50:25 -08:00
}, [group, mangoAccount])
const pnlChangeToday = useMemo(() => {
if (!accountPnl || !rollingDailyData.length) return 0
const startHour = rollingDailyData.find((item) => {
const itemHour = new Date(item.time).getHours()
return itemHour === 0
})
const startDayPnl = startHour?.pnl
const pnlChangeToday = startDayPnl ? accountPnl - startDayPnl : 0
2022-09-01 10:33:29 -07:00
return pnlChangeToday
}, [accountPnl, rollingDailyData])
2022-10-03 15:45:46 -07:00
2022-11-20 16:50:25 -08:00
const latestAccountData = useMemo(() => {
2023-07-06 22:01:47 -07:00
if (!accountValue || !performanceData || !performanceData.length) return []
2023-02-11 04:40:23 -08:00
const latestDataItem = performanceData[performanceData.length - 1]
2022-11-20 16:50:25 -08:00
return [
{
account_equity: accountValue,
time: dayjs(Date.now()).toISOString(),
borrow_interest_cumulative_usd:
2023-02-11 04:40:23 -08:00
latestDataItem.borrow_interest_cumulative_usd,
2022-11-20 16:50:25 -08:00
deposit_interest_cumulative_usd:
2023-02-11 04:40:23 -08:00
latestDataItem.deposit_interest_cumulative_usd,
2023-06-05 22:29:04 -07:00
pnl: accountPnl,
2023-02-11 04:40:23 -08:00
spot_value: latestDataItem.spot_value,
transfer_balance: latestDataItem.transfer_balance,
2022-11-20 16:50:25 -08:00
},
]
2023-06-05 22:29:04 -07:00
}, [accountPnl, accountValue, performanceData])
2022-11-20 16:50:25 -08:00
2023-07-11 06:14:44 -07:00
return !view ? (
2022-09-01 10:33:29 -07:00
<>
2023-02-12 15:30:33 -08:00
<div className="flex flex-col border-b-0 border-th-bkg-3 px-6 py-4 lg:flex-row lg:items-center lg:justify-between lg:border-b">
<div>
<div className="hide-scroll flex justify-center space-x-2 md:justify-start">
{TABS.map((tab) => (
<button
className={`rounded-md px-2.5 py-1.5 text-sm font-medium focus-visible:bg-th-bkg-3 focus-visible:text-th-fgd-1 ${
2023-02-12 15:30:33 -08:00
activeTab === tab
2023-04-19 18:01:31 -07:00
? 'bg-th-bkg-3 text-th-active md:hover:text-th-active'
: 'text-th-fgd-3 md:hover:text-th-fgd-2'
2023-02-12 15:30:33 -08:00
}`}
onClick={() => setActiveTab(tab)}
key={tab}
2022-09-01 10:33:29 -07:00
>
2023-02-12 15:30:33 -08:00
{t(tab)}
2023-02-04 04:31:36 -08:00
</button>
2023-02-12 15:30:33 -08:00
))}
2023-02-04 04:31:36 -08:00
</div>
2023-02-12 15:30:33 -08:00
<div className="md:h-24">
{activeTab === 'account-value' ? (
<AccountValue
accountValue={accountValue}
latestAccountData={latestAccountData}
rollingDailyData={rollingDailyData}
2023-07-11 06:14:44 -07:00
handleViewChange={handleViewChange}
/>
2023-02-12 15:30:33 -08:00
) : null}
{activeTab === 'account:assets-liabilities' ? (
2023-08-31 20:22:50 -07:00
<AssetsLiabilities isMobile={isTablet} />
2023-02-05 18:37:31 -08:00
) : null}
</div>
2022-09-01 10:33:29 -07:00
</div>
<div className="mb-1 mt-6 lg:mt-0">
<AccountActions />
</div>
2022-09-01 10:33:29 -07:00
</div>
<AccountHeroStats
accountPnl={accountPnl}
accountValue={accountValue}
rollingDailyData={rollingDailyData}
setShowPnlHistory={setShowPnlHistory}
2023-07-11 06:14:44 -07:00
handleViewChange={handleViewChange}
/>
<AccountTabs />
2023-01-15 21:13:34 -08:00
{showPnlHistory ? (
<PnlHistoryModal
2023-07-06 23:00:17 -07:00
pnlChangeToday={pnlChangeToday}
2023-01-15 21:13:34 -08:00
isOpen={showPnlHistory}
2023-01-16 02:07:46 -08:00
onClose={handleCloseDailyPnlModal}
2023-01-15 21:13:34 -08:00
/>
) : null}
2022-09-01 10:33:29 -07:00
</>
) : (
2023-07-11 05:00:23 -07:00
<AccountView
2023-07-11 06:14:44 -07:00
view={view as ViewToShow}
2023-07-11 05:00:23 -07:00
latestAccountData={latestAccountData}
2023-07-11 06:14:44 -07:00
handleViewChange={handleViewChange}
2023-07-11 05:00:23 -07:00
/>
)
}
export default AccountPage
const AccountView = ({
view,
2023-07-11 06:14:44 -07:00
handleViewChange,
2023-07-11 05:00:23 -07:00
latestAccountData,
}: {
view: ViewToShow
latestAccountData: PerformanceDataItem[]
2023-07-11 06:14:44 -07:00
handleViewChange: (view: ViewToShow) => void
2023-07-11 05:00:23 -07:00
}) => {
2023-07-11 06:14:44 -07:00
const router = useRouter()
2023-07-14 06:34:09 -07:00
const { connected } = useWallet()
2023-07-11 06:14:44 -07:00
const { address } = router.query
2023-07-11 05:00:23 -07:00
const { performanceData } = useAccountPerformanceData()
2023-07-11 06:14:44 -07:00
const handleHideChart = useCallback(() => {
2023-07-14 06:34:09 -07:00
if (address && !connected) {
router.push(`/?address=${address}`, undefined, { shallow: true })
2023-07-11 06:14:44 -07:00
} else {
2023-07-14 06:34:09 -07:00
router.push('/', undefined, { shallow: true })
2023-07-11 06:14:44 -07:00
}
2023-07-14 06:34:09 -07:00
}, [address, router, connected])
2023-07-11 05:00:23 -07:00
switch (view) {
case 'account-value':
return (
2022-09-19 16:57:58 -07:00
<AccountChart
2023-07-11 05:00:23 -07:00
chartName="account-value"
2023-07-11 06:14:44 -07:00
handleViewChange={handleViewChange}
2023-07-06 22:01:47 -07:00
data={performanceData?.concat(latestAccountData)}
2022-09-19 16:57:58 -07:00
hideChart={handleHideChart}
yKey="account_equity"
/>
2023-07-11 05:00:23 -07:00
)
case 'pnl':
return (
2022-09-19 16:57:58 -07:00
<AccountChart
2023-07-11 05:00:23 -07:00
chartName="pnl"
2023-07-11 06:14:44 -07:00
handleViewChange={handleViewChange}
2023-07-06 22:01:47 -07:00
data={performanceData?.concat(latestAccountData)}
2022-09-19 16:57:58 -07:00
hideChart={handleHideChart}
yKey="pnl"
/>
2023-07-11 05:00:23 -07:00
)
case 'cumulative-interest-value':
return (
<AccountChart
chartName="cumulative-interest-value"
2023-07-11 06:14:44 -07:00
handleViewChange={handleViewChange}
2023-07-11 05:00:23 -07:00
data={performanceData?.concat(latestAccountData)}
hideChart={handleHideChart}
yKey="interest_value"
/>
)
case 'hourly-funding':
return <FundingChart hideChart={handleHideChart} />
case 'hourly-volume':
2023-07-11 06:14:44 -07:00
return <VolumeChart hideChart={handleHideChart} />
2023-07-11 05:00:23 -07:00
case 'health-contributions':
return <HealthContributions hideView={handleHideChart} />
default:
return null
}
2022-09-01 10:33:29 -07:00
}