import { SegmentedControl, Stack, Text, Title } from "@mantine/core"; import type { FC } from "react"; import { useState } from "react"; import { useTranslation } from "react-i18next"; import { formatMoney } from "@/shared/lib"; import styles from "../AccountIncomePage.module.css"; import type { AccountIncomePageViewModel } from "../AccountIncomePage.vm"; type LoadedProps = Extract; type IncomeTrendProps = { trend: LoadedProps["display"]["trend"] }; const IncomeTrend: FC = (props) => { const currencies = props.trend.map(({ currency }) => currency); const [currency, setCurrency] = useState(currencies[0] ?? ""); const { t } = useTranslation(); const buckets = props.trend.find((item) => item.currency === currency)?.buckets ?? []; const values = buckets.map((bucket) => ({ ...bucket, coupon: Number(bucket.total.coupon.value), dividend: Number(bucket.total.dividend.value), })); const scale = Math.max( 1, ...values.map((value) => Math.max( positive(value.coupon) + positive(value.dividend), negative(value.coupon) + negative(value.dividend), ), ), ); return ( {t("income.trend")} {currencies.length > 1 ? ( ) : null} {buckets.map((bucket) => ( {bucket.period.from} — {bucket.period.to}: {t("income.coupon")}{" "} {formatMoney(bucket.total.coupon)}, {t("income.dividend")}{" "} {formatMoney(bucket.total.dividend)} ))} ); }; function positive(value: number): number { return Math.max(0, value); } function negative(value: number): number { return Math.max(0, -value); } function chartHeight(value: number, scale: number): string { return `${(Math.abs(value) / scale) * 100}%`; } export { IncomeTrend };