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

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-11-20 16:50:25 -08:00
import {
MangoAccount,
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'
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,
mangoAccount,
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
mangoAccount: MangoAccount
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) => {
2022-09-07 23:25:32 -07:00
await actions.fetchAccountPerformance(
mangoAccount.publicKey.toString(),
2022-11-29 21:30:18 -08:00
parseInt(days)
2022-09-07 23:25:32 -07:00
)
2022-08-14 22:43:15 -07:00
setDaysToShow(days)
}
2022-11-20 16:50:25 -08:00
const currentValue = useMemo(() => {
if (chartToShow === 'account-value') {
const group = mangoStore.getState().group
const currentAccountValue = toUiDecimalsForQuote(
mangoAccount.getEquity(group!)!.toNumber()
)
const time = Date.now()
return [{ account_equity: currentAccountValue, time: time }]
}
return []
}, [chartToShow, mangoAccount])
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}
hideChart={hideChart}
loading={loading}
2022-12-06 03:58:22 -08:00
prefix="$"
2022-08-14 22:43:15 -07:00
setDaysToShow={handleDaysToShow}
tickFormat={(x) => `$${x.toFixed(2)}`}
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