import { useTranslation } from 'next-i18next' import { Cell, Pie, PieChart, ResponsiveContainer, Sector, SectorProps, } from 'recharts' import { COLORS } from 'styles/colors' import { useMemo } from 'react' import { formatCurrencyValue } from 'utils/numbers' import { useViewport } from 'hooks/useViewport' import { HealthContribution } from 'types' import useThemeWrapper from 'hooks/useThemeWrapper' const HealthContributionsChart = ({ data, activeIndex, setActiveIndex, }: { data: HealthContribution[] activeIndex: number | undefined setActiveIndex: (i: number | undefined) => void }) => { const { t } = useTranslation(['common', 'account']) const { theme } = useThemeWrapper() const { isMobile } = useViewport() const handleMouseEnter = (data: HealthContribution, index: number) => { setActiveIndex(index) } const handleMouseLeave = () => { setActiveIndex(undefined) } const pieSizes = isMobile ? { size: 160, outerRadius: 80, innerRadius: 64 } : { size: 240, outerRadius: 120, innerRadius: 96 } const { size, outerRadius, innerRadius } = pieSizes const [chartHeroAsset, chartHeroValue] = useMemo(() => { if (!data.length) return [undefined, undefined] if (activeIndex === undefined) { const value = data.reduce((a, c) => { const assetOrLiabMultiplier = c.isAsset ? 1 : -1 return a + c.contribution * assetOrLiabMultiplier }, 0) return [t('total'), value] } else { const asset = data[activeIndex] const assetOrLiabMultiplier = asset.isAsset ? 1 : -1 const value = asset.contribution * assetOrLiabMultiplier return [asset.asset, value] } }, [activeIndex, data]) const renderActiveShape = ({ cx, cy, innerRadius, outerRadius, startAngle, endAngle, fill, }: SectorProps) => { return ( ) } return data.length ? (
{data.map((entry, index) => { const fillColor = entry.isAsset ? COLORS.UP[theme] : COLORS.DOWN[theme] let opacity if (entry.isAsset) { opacity = 1 - index * 0.1 } else { opacity = 1 - Math.abs((index - (data.length - 1)) * 0.1) } return ( ) })} {chartHeroValue !== undefined ? (

{chartHeroAsset}

{formatCurrencyValue(chartHeroValue, 2)}
) : null}
) : null } export default HealthContributionsChart