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

89 lines
2.5 KiB
TypeScript
Raw Normal View History

import { toUiDecimalsForQuote } from '@blockworks-foundation/mango-v4'
2022-08-14 22:43:15 -07:00
import { useTranslation } from 'next-i18next'
2023-01-15 21:13:34 -08:00
import { useEffect, useMemo, useState } from 'react'
2022-09-12 08:53:57 -07:00
import mangoStore from '@store/mangoStore'
2022-10-10 14:15:35 -07:00
import dynamic from 'next/dynamic'
2023-01-19 02:42:39 -08:00
import { formatYAxis } from 'utils/formatting'
2022-10-10 14:15:35 -07:00
const DetailedAreaChart = dynamic(
() => import('@components/shared/DetailedAreaChart'),
{ ssr: false }
)
2022-08-14 22:43:15 -07:00
2022-08-15 17:16:21 -07:00
const AccountChart = ({
chartToShow,
2022-08-14 22:43:15 -07:00
hideChart,
2023-01-15 21:13:34 -08:00
mangoAccountAddress,
2022-08-15 17:16:21 -07:00
yKey,
2022-08-14 22:43:15 -07:00
}: {
2022-08-15 17:16:21 -07:00
chartToShow: string
2022-08-14 22:43:15 -07:00
hideChart: () => void
2023-01-15 21:13:34 -08:00
mangoAccountAddress: string
2022-08-15 17:16:21 -07:00
yKey: string
2022-08-14 22:43:15 -07:00
}) => {
const { t } = useTranslation('common')
2023-01-03 14:20:00 -08:00
const actions = mangoStore.getState().actions
2022-11-29 21:30:18 -08:00
const [daysToShow, setDaysToShow] = useState<string>('1')
2023-01-15 21:13:34 -08:00
const loading = mangoStore((s) => s.mangoAccount.performance.loading)
const performanceData = mangoStore((s) => s.mangoAccount.performance.data)
useEffect(() => {
if (mangoAccountAddress) {
actions.fetchAccountPerformance(mangoAccountAddress, 1)
}
}, [actions, mangoAccountAddress])
const data: any = useMemo(() => {
if (!performanceData.length) return []
if (chartToShow === 'cumulative-interest-value') {
performanceData.map((d) => ({
interest_value:
d.borrow_interest_cumulative_usd + d.deposit_interest_cumulative_usd,
time: d.time,
}))
}
return performanceData
}, [performanceData])
2022-08-14 22:43:15 -07:00
2022-11-29 21:30:18 -08:00
const handleDaysToShow = async (days: string) => {
const mangoAccount = mangoStore.getState().mangoAccount.current
if (mangoAccount) {
await actions.fetchAccountPerformance(
mangoAccount.publicKey.toString(),
parseInt(days)
)
setDaysToShow(days)
}
2022-08-14 22:43:15 -07:00
}
2022-11-20 16:50:25 -08:00
const currentValue = useMemo(() => {
const mangoAccount = mangoStore.getState().mangoAccount.current
const group = mangoStore.getState().group
if (group && mangoAccount && chartToShow === 'account-value') {
2022-11-20 16:50:25 -08:00
const currentAccountValue = toUiDecimalsForQuote(
mangoAccount.getEquity(group).toNumber()
2022-11-20 16:50:25 -08:00
)
const time = Date.now()
return [{ account_equity: currentAccountValue, time: time }]
}
return []
}, [chartToShow])
2022-11-20 16:50:25 -08:00
2022-08-14 22:43:15 -07:00
return (
<DetailedAreaChart
2022-11-20 16:50:25 -08:00
data={data.concat(currentValue)}
2022-08-14 22:43:15 -07:00
daysToShow={daysToShow}
heightClass="h-[calc(100vh-200px)]"
2022-08-14 22:43:15 -07:00
hideChart={hideChart}
loading={loading}
2022-12-06 03:58:22 -08:00
prefix="$"
2022-08-14 22:43:15 -07:00
setDaysToShow={handleDaysToShow}
2023-01-19 02:42:39 -08:00
tickFormat={(x) => `$${formatYAxis(x)}`}
2022-08-15 17:16:21 -07:00
title={t(chartToShow)}
2022-08-14 22:43:15 -07:00
xKey="time"
2022-08-15 17:16:21 -07:00
yKey={yKey}
2022-08-14 22:43:15 -07:00
/>
)
}
2022-08-15 17:16:21 -07:00
export default AccountChart