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

104 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-08-14 22:43:15 -07:00
import { useTranslation } from 'next-i18next'
2023-02-11 04:40:23 -08:00
import { useMemo, useState } from 'react'
2023-01-19 02:42:39 -08:00
import { formatYAxis } from 'utils/formatting'
import { HourlyFundingChartData, PerformanceDataItem } from 'types'
import { ContentType } from 'recharts/types/component/Tooltip'
import DetailedAreaOrBarChart from '@components/shared/DetailedAreaOrBarChart'
2023-07-11 05:00:23 -07:00
import { ViewToShow } from './AccountPage'
2023-07-06 04:00:47 -07:00
import { ArrowLeftIcon, NoSymbolIcon } from '@heroicons/react/20/solid'
2023-07-11 05:00:23 -07:00
const CHART_TABS: ViewToShow[] = [
'account-value',
'pnl',
'cumulative-interest-value',
]
2022-08-14 22:43:15 -07:00
2022-08-15 17:16:21 -07:00
const AccountChart = ({
2023-07-11 05:00:23 -07:00
chartName,
2023-07-11 06:14:44 -07:00
handleViewChange,
customTooltip,
2023-02-11 04:40:23 -08:00
data,
2022-08-14 22:43:15 -07:00
hideChart,
loading,
yDecimals,
2022-08-15 17:16:21 -07:00
yKey,
2022-08-14 22:43:15 -07:00
}: {
2023-07-11 05:00:23 -07:00
chartName: ViewToShow
2023-07-11 06:14:44 -07:00
handleViewChange: (view: ViewToShow) => void
customTooltip?: ContentType<number, string>
2023-07-06 22:01:47 -07:00
data: PerformanceDataItem[] | HourlyFundingChartData[] | undefined
2022-08-14 22:43:15 -07:00
hideChart: () => void
loading?: boolean
yDecimals?: number
2022-08-15 17:16:21 -07:00
yKey: string
2022-08-14 22:43:15 -07:00
}) => {
const { t } = useTranslation('common')
2022-11-29 21:30:18 -08:00
const [daysToShow, setDaysToShow] = useState<string>('1')
2023-01-15 21:13:34 -08:00
const chartData = useMemo(() => {
2023-07-06 22:01:47 -07:00
if (!data || !data.length) return []
2023-07-11 05:00:23 -07:00
if (chartName === 'cumulative-interest-value') {
2023-05-02 21:39:25 -07:00
return data.map((d) => ({
2023-01-15 21:13:34 -08:00
interest_value:
2023-06-22 02:48:09 -07:00
d.borrow_interest_cumulative_usd * -1 +
d.deposit_interest_cumulative_usd,
2023-01-15 21:13:34 -08:00
time: d.time,
}))
}
2023-02-11 04:40:23 -08:00
return data
2023-07-11 05:00:23 -07:00
}, [data, chartName])
2022-11-20 16:50:25 -08:00
2022-08-14 22:43:15 -07:00
return (
<>
<div className="hide-scroll mb-3 flex h-14 items-center space-x-4 overflow-x-auto border-b border-th-bkg-3">
2023-05-29 20:48:43 -07:00
<button
className="flex h-14 w-14 flex-shrink-0 items-center justify-center border-r border-th-bkg-3 focus-visible:bg-th-bkg-3 md:hover:bg-th-bkg-2"
onClick={hideChart}
>
<ArrowLeftIcon className="h-5 w-5" />
</button>
<div className="flex space-x-2">
{CHART_TABS.map((tab) => (
<button
className={`whitespace-nowrap 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-07-11 05:00:23 -07:00
chartName === tab
? 'bg-th-bkg-3 text-th-active md:hover:text-th-active'
: 'text-th-fgd-3 md:hover:text-th-fgd-2'
}`}
2023-07-11 06:14:44 -07:00
onClick={() => handleViewChange(tab)}
key={tab}
>
{t(tab)}
</button>
))}
</div>
</div>
<div className="px-6 pt-4">
2023-07-06 04:00:47 -07:00
{chartData.length ? (
<DetailedAreaOrBarChart
customTooltip={customTooltip}
data={chartData}
daysToShow={daysToShow}
heightClass="h-[calc(100vh-240px)]"
loaderHeightClass="h-[calc(100vh-166px)]"
loading={loading}
prefix="$"
setDaysToShow={setDaysToShow}
tickFormat={(x) => `$${formatYAxis(x)}`}
xKey="time"
yDecimals={yDecimals}
yKey={yKey}
/>
) : (
<div className="flex flex-col items-center rounded-lg border border-th-bkg-3 p-8">
<NoSymbolIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
<p>{t('account:no-data')}</p>
</div>
)}
</div>
</>
2022-08-14 22:43:15 -07:00
)
}
2022-08-15 17:16:21 -07:00
export default AccountChart