2023-01-05 20:01:03 -08:00
|
|
|
import { toUiDecimalsForQuote } from '@blockworks-foundation/mango-v4'
|
2022-08-14 22:43:15 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2022-11-20 16:50:25 -08:00
|
|
|
import { 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-14 03:53:28 -08:00
|
|
|
import { numberCompacter } from 'utils/numbers'
|
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
|
|
|
data,
|
|
|
|
hideChart,
|
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
|
|
|
|
data: Array<any>
|
2022-08-14 22:43:15 -07:00
|
|
|
hideChart: () => void
|
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')
|
2022-08-14 22:43:15 -07:00
|
|
|
const loading = mangoStore((s) => s.mangoAccount.stats.performance.loading)
|
|
|
|
|
2022-11-29 21:30:18 -08:00
|
|
|
const handleDaysToShow = async (days: string) => {
|
2023-01-05 20:01:03 -08:00
|
|
|
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(() => {
|
2023-01-05 20:01:03 -08:00
|
|
|
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(
|
2023-01-05 20:01:03 -08:00
|
|
|
mangoAccount.getEquity(group).toNumber()
|
2022-11-20 16:50:25 -08:00
|
|
|
)
|
|
|
|
const time = Date.now()
|
|
|
|
return [{ account_equity: currentAccountValue, time: time }]
|
|
|
|
}
|
|
|
|
return []
|
2023-01-05 20:01:03 -08:00
|
|
|
}, [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}
|
2023-01-08 20:58:01 -08:00
|
|
|
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-14 03:53:28 -08:00
|
|
|
tickFormat={(x) => `$${numberCompacter.format(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
|