"use client"; import Link from "next/link"; import { useMemo } from "react"; import { CategoryIconBadge } from "@/components/categorias/category-icon-badge"; import { Table, TableBody, TableCell, TableFooter, TableHead, TableHeader, TableRow, } from "@/components/ui/table"; import type { CategoryReportItem } from "@/lib/relatorios/types"; import { formatCurrency, formatPeriodLabel } from "@/lib/relatorios/utils"; import { formatPeriodForUrl } from "@/lib/utils/period"; import DotIcon from "../dot-icon"; import { Card } from "../ui/card"; import { CategoryCell } from "./category-cell"; export interface CategoryTableProps { title: string; categories: CategoryReportItem[]; periods: string[]; colorIndexOffset: number; } export function CategoryTable({ title, categories, periods, colorIndexOffset, }: CategoryTableProps) { // Calculate section totals const sectionTotals = useMemo(() => { const totalsMap = new Map(); let grandTotal = 0; for (const category of categories) { grandTotal += category.total; for (const period of periods) { const monthData = category.monthlyData.get(period); const current = totalsMap.get(period) ?? 0; totalsMap.set(period, current + (monthData?.amount ?? 0)); } } return { totalsMap, grandTotal }; }, [categories, periods]); if (categories.length === 0) { return null; } return ( Categoria {periods.map((period) => ( {formatPeriodLabel(period)} ))} Total {categories.map((category, index) => { const colorIndex = colorIndexOffset + index; const periodParam = formatPeriodForUrl(periods[periods.length - 1]); return (
{category.name}
{periods.map((period, periodIndex) => { const monthData = category.monthlyData.get(period); const isFirstMonth = periodIndex === 0; return ( ); })} {formatCurrency(category.total)}
); })}
Total {periods.map((period) => { const periodTotal = sectionTotals.totalsMap.get(period) ?? 0; return ( {formatCurrency(periodTotal)} ); })} {formatCurrency(sectionTotals.grandTotal)}
); }