import { useTranslation } from "react-i18next"; import type { MonthlyTrendItem } from "../../shared/types"; const cadFormatter = (value: number) => new Intl.NumberFormat("en-CA", { style: "currency", currency: "CAD", maximumFractionDigits: 0 }).format(value); function formatMonth(month: string): string { const [year, m] = month.split("-"); const date = new Date(Number(year), Number(m) - 1); return date.toLocaleDateString("default", { month: "short", year: "2-digit" }); } interface MonthlyTrendsTableProps { data: MonthlyTrendItem[]; } export default function MonthlyTrendsTable({ data }: MonthlyTrendsTableProps) { const { t } = useTranslation(); if (data.length === 0) { return (
{t("dashboard.noData")}
); } const totals = data.reduce( (acc, row) => ({ income: acc.income + row.income, expenses: acc.expenses + row.expenses }), { income: 0, expenses: 0 }, ); return (
{data.map((row) => ( ))}
{t("reports.pivot.month")} {t("dashboard.income")} {t("dashboard.expenses")} {t("dashboard.net")}
{formatMonth(row.month)} {cadFormatter(row.income)} {cadFormatter(row.expenses)} = 0 ? "text-[var(--positive)]" : "text-[var(--negative)]"}`}> {cadFormatter(row.income - row.expenses)}
{t("common.total")} {cadFormatter(totals.income)} {cadFormatter(totals.expenses)} = 0 ? "text-[var(--positive)]" : "text-[var(--negative)]"}`}> {cadFormatter(totals.income - totals.expenses)}
); }