Simpl-Resultat/src/components/reports/CategoryZoomMetrics.tsx
le king fu d70ca128e0 fix(reports): open the category zoom on the clicked card category, fill its left box
The category zoom now reads the zoomed category from the `cat` query param
written by the report cards' links, and writes it back (replace) when the
selection changes, so a click on a card opens the zoom on that category and
the selector and the URL always agree. Data read for another category is
never shown.

getCategoryZoom sums each direct child over its whole subtree (bounded
recursive CTE), so the donut of a first-level category shows its
subcategories instead of nothing, and returns the key figures of the
period's transactions (count, average, largest, smallest), oriented like the
total and computed in SQL over every row, a split transaction counting once
for its net in the scope. When the donut has no part to draw (a leaf, the
subcategories left out), the box beside the evolution chart shows those
figures; "No data" only when there is no transaction.

test: reproduces #548 (hook mounted on ?cat=, level-1 parts on real SQLite,
leaf metrics, render of the box)

Fixes #548

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 01:25:36 +00:00

66 lines
2.6 KiB
TypeScript

import { useTranslation } from "react-i18next";
import type { CategoryZoomMetrics as Metrics } from "../../shared/types";
export interface CategoryZoomMetricsProps {
/** The zoom's total over the period, oriented like the figures (`rollupTotal`). */
total: number;
metrics: Metrics;
/** Subcategories left out of the donut for a negative amount (D7, #392). */
omittedCount?: number;
}
/**
* Key figures of the zoomed category's transactions (#548), shown in place of
* the donut when it has no part to draw: a leaf, the subcategories left out,
* or no subcategory with a positive amount. « Aucune donnée » only when the
* period has no transaction at all.
*/
export default function CategoryZoomMetrics({ total, metrics, omittedCount = 0 }: CategoryZoomMetricsProps) {
const { t, i18n } = useTranslation();
const money = new Intl.NumberFormat(i18n.language === "fr" ? "fr-CA" : "en-CA", {
style: "currency",
currency: "CAD",
});
const omitted =
omittedCount > 0 ? (
<p className="text-xs text-[var(--muted-foreground)] text-center mt-2">
{t("reports.category.donutOmitted", { count: omittedCount })}
</p>
) : null;
if (metrics.transactionCount === 0) {
return (
<div className="bg-[var(--card)] border border-[var(--border)] rounded-xl p-6 text-center text-[var(--muted-foreground)] italic">
{t("reports.empty.noData")}
{omitted}
</div>
);
}
const figure = (value: number | null) => (value === null ? "—" : money.format(value));
const rows: Array<[string, string]> = [
[t("reports.category.metrics.count"), new Intl.NumberFormat(i18n.language).format(metrics.transactionCount)],
[t("reports.category.metrics.average"), figure(metrics.average)],
[t("reports.category.metrics.largest"), figure(metrics.largest)],
[t("reports.category.metrics.smallest"), figure(metrics.smallest)],
];
return (
<div className="bg-[var(--card)] border border-[var(--border)] rounded-xl p-4">
<h3 className="text-sm font-semibold mb-3">{t("reports.category.metrics.title")}</h3>
<div className="mb-4">
<span className="block text-xs text-[var(--muted-foreground)]">{t("reports.category.breakdown")}</span>
<span className="text-2xl font-bold">{money.format(total)}</span>
</div>
<dl className="grid grid-cols-2 gap-3">
{rows.map(([label, value]) => (
<div key={label}>
<dt className="text-xs text-[var(--muted-foreground)]">{label}</dt>
<dd className="text-base font-semibold">{value}</dd>
</div>
))}
</dl>
{omitted}
</div>
);
}