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

242 lines
8.0 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'
import { 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-12-20 01:13:24 -08:00
// import AccountOnboardingTour from '@components/tours/AccountOnboardingTour'
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 { breakpoints } from 'utils/theme'
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'
import { PerformanceDataItem } from 'types'
import { useQuery } from '@tanstack/react-query'
2023-07-03 20:24:13 -07:00
import FundingChart from './FundingChart'
import VolumeChart from './VolumeChart'
import { fetchAccountPerformance, fetchHourlyVolume } from 'utils/account'
import AccountHeroStats from './AccountHeroStats'
import AccountValue from './AccountValue'
2023-05-01 19:01:14 -07:00
const TABS = ['account-value', 'account:assets-liabilities']
export type ChartToShow =
| ''
| 'account-value'
| 'cumulative-interest-value'
| 'pnl'
| 'hourly-funding'
2023-07-03 20:24:13 -07:00
| 'hourly-volume'
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()
2023-02-11 04:40:23 -08:00
const { mangoAccount, mangoAccountAddress } = useMangoAccount()
const [chartToShow, setChartToShow] = useState<ChartToShow>('')
2023-01-15 21:13:34 -08:00
const [showPnlHistory, setShowPnlHistory] = useState<boolean>(false)
2022-12-21 03:07:17 -08:00
const { width } = useViewport()
2023-01-06 02:43:07 -08:00
const isMobile = width ? width < breakpoints.md : false
2022-12-20 01:13:24 -08:00
// const tourSettings = mangoStore((s) => s.settings.tours)
// const [isOnBoarded] = useLocalStorageState(IS_ONBOARDED_KEY)
2023-02-05 18:37:31 -08:00
const [activeTab, setActiveTab] = useLocalStorageState(
'accountHeroKey-0.1',
'account-value'
)
2022-09-01 10:33:29 -07:00
2023-07-06 22:01:47 -07:00
const {
data: performanceData,
isLoading: loadingPerformanceData,
isFetching: fetchingPerformanceData,
} = useQuery(
['performance', mangoAccountAddress],
() => fetchAccountPerformance(mangoAccountAddress, 31),
{
cacheTime: 1000 * 60 * 10,
staleTime: 1000 * 60,
retry: 3,
refetchOnWindowFocus: false,
enabled: !!mangoAccountAddress,
}
)
2022-09-01 10:33:29 -07:00
const {
2023-07-03 18:24:36 -07:00
data: hourlyVolumeData,
isLoading: loadingHourlyVolumeData,
isFetching: fetchingHourlyVolumeData,
} = useQuery(
2023-07-03 18:24:36 -07:00
['hourly-volume', mangoAccountAddress],
() => fetchHourlyVolume(mangoAccountAddress),
{
cacheTime: 1000 * 60 * 10,
staleTime: 1000 * 60,
retry: 3,
refetchOnWindowFocus: false,
enabled: !!mangoAccountAddress,
}
)
const rollingDailyData: PerformanceDataItem[] | [] = useMemo(() => {
2023-02-11 04:40:23 -08:00
if (!performanceData || !performanceData.length) return []
const nowDate = new Date()
return performanceData.filter((d) => {
const dataTime = new Date(d.time).getTime()
return dataTime >= nowDate.getTime() - 86400000
})
}, [performanceData])
2022-09-01 10:33:29 -07:00
const handleHideChart = () => {
setChartToShow('')
}
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-10-05 03:16:32 -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-03 18:24:36 -07:00
const loadingHourlyVolume =
fetchingHourlyVolumeData || loadingHourlyVolumeData
2023-07-06 22:01:47 -07:00
const performanceLoading = loadingPerformanceData || fetchingPerformanceData
2022-09-01 10:33:29 -07:00
return !chartToShow ? (
<>
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
2023-04-19 18:12:45 -07:00
className={`rounded-md py-1.5 px-2.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}
loading={performanceLoading}
rollingDailyData={rollingDailyData}
setChartToShow={setChartToShow}
/>
2023-02-12 15:30:33 -08:00
) : null}
{activeTab === 'account:assets-liabilities' ? (
<AssetsLiabilities isMobile={isMobile} />
2023-02-05 18:37:31 -08:00
) : null}
</div>
2022-09-01 10:33:29 -07:00
</div>
2023-02-12 15:30:33 -08:00
<div className="mt-6 mb-1 lg:mt-0">
<AccountActions />
</div>
2022-09-01 10:33:29 -07:00
</div>
<AccountHeroStats
accountPnl={accountPnl}
accountValue={accountValue}
hourlyVolumeData={hourlyVolumeData}
loadingHourlyVolume={loadingHourlyVolume}
rollingDailyData={rollingDailyData}
setChartToShow={setChartToShow}
setShowPnlHistory={setShowPnlHistory}
/>
<AccountTabs />
2022-12-20 01:13:24 -08:00
{/* {!tourSettings?.account_tour_seen && isOnBoarded && connected ? (
2022-09-21 21:25:24 -07:00
<AccountOnboardingTour />
2022-12-20 01:13:24 -08:00
) : null} */}
2023-01-15 21:13:34 -08:00
{showPnlHistory ? (
<PnlHistoryModal
2023-07-06 22:01:47 -07:00
loading={performanceLoading}
performanceData={performanceData}
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
</>
) : (
<>
2022-09-19 16:57:58 -07:00
{chartToShow === 'account-value' ? (
<AccountChart
chartToShow="account-value"
setChartToShow={setChartToShow}
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"
/>
) : chartToShow === 'pnl' ? (
<AccountChart
chartToShow="pnl"
setChartToShow={setChartToShow}
2023-07-06 22:01:47 -07:00
data={performanceData?.concat(latestAccountData)}
2022-09-19 16:57:58 -07:00
hideChart={handleHideChart}
yKey="pnl"
/>
) : chartToShow === 'hourly-funding' ? (
2023-07-03 20:24:13 -07:00
<FundingChart hideChart={handleHideChart} />
) : chartToShow === 'hourly-volume' ? (
<VolumeChart
chartData={hourlyVolumeData}
hideChart={handleHideChart}
loading={loadingHourlyVolume}
/>
2022-09-19 16:57:58 -07:00
) : (
<AccountChart
chartToShow="cumulative-interest-value"
setChartToShow={setChartToShow}
2023-02-11 04:40:23 -08:00
data={performanceData}
2022-09-19 16:57:58 -07:00
hideChart={handleHideChart}
yKey="interest_value"
/>
)}
</>
2022-09-01 10:33:29 -07:00
)
}
export default AccountPage