import type { FC } from "react"; import { Paper, Stack, Text, Title } from "@mantine/core"; import { useTranslation } from "react-i18next"; import { formatMoney } from "@/shared/lib"; import type { IncomeDisplayModel } from "../AccountIncomePage.vm"; import classes from "../AccountIncomePage.module.css"; type ComparisonProps = NonNullable & { title: string; }; type ComparisonValuesProps = { kind: "compared" | "difference" | "selected"; label: string; percentages?: ComparisonProps["values"][number]["percentages"]; values: ComparisonProps["values"][number]["selected"]; }; type IncomeComparisonsProps = { comparison: IncomeDisplayModel["comparison"]; notice: string; title: string; }; const IncomeComparisons: FC = (props) => { return ( {props.title} {props.comparison ? ( ) : ( {props.notice} )} ); }; const Comparison: FC = (props) => { const { t } = useTranslation(); return (
{props.values.map(value => (
))} {props.values.length === 0 && ( {t("income.comparisonEmpty")} )}
); }; const ComparisonValues: FC = (props) => { const { t } = useTranslation(); const data_tone = props.kind === "difference" ? differenceTone(props.values.total.value) : undefined; return (
{props.label}
{(["total", "coupon", "dividend"] as const).map((key) => { const money // eslint-disable-next-line security/detect-object-injection -- This lookup uses a fixed typed key set, not an arbitrary external property name. = props.values[key]; const percentage // eslint-disable-next-line security/detect-object-injection -- This lookup uses a fixed typed key set, not an arbitrary external property name. = props.percentages?.[key]; const tone = props.kind === "difference" ? differenceTone(money.value) : undefined; const appearanceClass = key === "total" ? classes.comparisonTotal : classes.comparisonDetail; return (
{t(`income.${key}`)}
{tone === "positive" ? "+" : ""} {formatMoney(money)} {!!percentage && ( {percentage} )}
); })}
); }; function differenceTone(value: string): "negative" | "neutral" | "positive" { if (!/[1-9]/.test(value)) return "neutral"; return value.startsWith("-") ? "negative" : "positive"; } export { IncomeComparisons };