Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/components/StatsDashboard.tsx9 * Module : components10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-08-1912 * Last Modified : 2026-08-2513 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142/**43 * Tableau de bord /stats v2 — monté sur le kit de graphiques maison (components/charts) : bandeau KPI (sparklines), jauges,44 * courbes + stats de séries, multi-courbes, barres empilées, répartitions,45 * distributions, géographie, calendrier d'activité, tableaux, records,46 * fraîcheur + PdfButton (5 rapports). Données : /api/stats/dashboard47 * (instantané du rôle 2026 + corpus de ventes réelles — period non applicable).48 */49"use client";50import { useCallback, useState } from "react";51import {52 KpiCard, GaugeCard, LineChart, MultiLineChart, StackedBarChart, BarChart,53 Donut, Histogram, CalendarHeatmap, StatSummary, DataTable, RecordCard,54 PdfButton, Fraicheur,55 type Kpi, type Serie, type MultiSerie, type StackedSerie, type BreakItem,56 type Distribution, type Gauge, type TableSpec, type RecordFact,57} from "@/components/charts/charts";58import { useLang } from "./LangContext";5960export interface DashboardPayload {61 updated: string;62 period: { label: string; applicable?: boolean };63 kpis: Kpi[];64 gauges?: Gauge[];65 series?: Serie[];66 multiseries?: MultiSerie[];67 stacked?: StackedSerie[];68 breakdowns?: { id: string; title: string; kind: "donut" | "bars"; items: BreakItem[] }[];69 distributions?: Distribution[];70 geo?: { title: string; items: BreakItem[] };71 heatmap?: { title: string; cells: { date: string; value: number }[] };72 tables?: TableSpec[];73 records?: RecordFact[];74}7576function SectionTitle({ kicker, title }: { kicker: string; title: string }) {77 return (78 <div className="mb-4 mt-12">79 <span className="kicker">{kicker}</span>80 <h2 className="vp-display mt-2 text-[22px] font-bold uppercase tracking-[-0.02em]">{title}</h2>81 </div>82 );83}8485export default function StatsDashboard({ initial }: { initial: DashboardPayload }) {86 const { lang } = useLang();87 const fr = lang === "fr";88 const [data, setData] = useState<DashboardPayload>(initial);89 const refresh = useCallback(async () => {90 try {91 const r = await fetch("/api/stats/dashboard?period=tout", { cache: "no-store" });92 if (r.ok) setData(await r.json());93 } catch {94 /* on garde les données courantes */95 }96 }, []);9798 const keySeries = new Set(["valeur_millesime", "ventes_mois", "prix_median_mois"]);99100 return (101 <div>102 {/* ---- fraîcheur + rapports PDF (5 modes) ---- */}103 <div className="mb-2 mt-10 flex flex-wrap items-center justify-between gap-3 border-y border-[var(--line)] px-1 py-3">104 <div>105 <p className="vp-mono text-[10px] font-bold uppercase tracking-[0.08em] text-ink-3">106 {fr107 ? `${data.period.label} · corpus de ventes 2021-2026 · périodes non applicables (instantané)`108 : `${data.period.label} · 2021-2026 sales corpus · periods not applicable (snapshot)`}109 </p>110 <div className="mt-1.5">111 <Fraicheur updated={data.updated} onRefresh={refresh} />112 </div>113 </div>114 <PdfButton period="tout" />115 </div>116117 {/* ---- 1. bandeau KPI ---- */}118 <SectionTitle kicker={fr ? "Indicateurs" : "Indicators"} title={fr ? "Indicateurs clés" : "Key indicators"} />119 <div className="grid grid-cols-1 gap-3.5 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">120 {data.kpis.map((k) => <KpiCard key={k.id} k={k} />)}121 </div>122123 {/* ---- 3. jauges ---- */}124 {!!data.gauges?.length && (125 <>126 <SectionTitle kicker={fr ? "Couverture" : "Coverage"} title={fr ? "Couverture & complétude du rôle" : "Roll coverage & completeness"} />127 <div className="grid grid-cols-2 gap-3.5 lg:grid-cols-4">128 {data.gauges.map((g) => <GaugeCard key={g.id} g={g} />)}129 </div>130 </>131 )}132133 {/* ---- 4. évolutions ---- */}134 {!!data.series?.length && (135 <>136 <SectionTitle kicker="2021 → 2026" title={fr ? "Évolution — millésimes & ventes réelles" : "Trends — vintages & real sales"} />137 <div className="grid gap-4">138 {data.series.map((s) => (139 <div key={s.id}>140 <LineChart serie={s} />141 {keySeries.has(s.id) && <StatSummary serie={s} />}142 </div>143 ))}144 </div>145 </>146 )}147 {!!data.multiseries?.length && (148 <>149 <SectionTitle kicker={fr ? "Marché" : "Market"} title={fr ? "Indices de marché & prix au m²" : "Market indices & price per m²"} />150 <div className="grid gap-4">151 {data.multiseries.map((ms) => <MultiLineChart key={ms.id} ms={ms} />)}152 </div>153 </>154 )}155 {!!data.stacked?.length && (156 <div className="mt-4 grid gap-4">157 {data.stacked.map((st) => <StackedBarChart key={st.id} st={st} />)}158 </div>159 )}160161 {/* ---- 5. répartitions & distributions ---- */}162 {(!!data.breakdowns?.length || !!data.distributions?.length) && (163 <>164 <SectionTitle kicker={fr ? "Répartitions" : "Breakdowns"} title={fr ? "Répartitions & distributions" : "Breakdowns & distributions"} />165 <div className="grid gap-4 lg:grid-cols-2">166 {data.breakdowns?.map((b) =>167 b.kind === "donut"168 ? <Donut key={b.id} title={b.title} items={b.items} />169 : <BarChart key={b.id} title={b.title} items={b.items} />170 )}171 {data.distributions?.map((d) => <Histogram key={d.id} dist={d} />)}172 </div>173 </>174 )}175176 {/* ---- 6. géographie ---- */}177 {!!data.geo?.items?.length && (178 <>179 <SectionTitle kicker={fr ? "Géographie" : "Geography"} title={data.geo.title} />180 <BarChart title={data.geo.title} items={data.geo.items} />181 </>182 )}183184 {/* ---- 7. calendrier d'activité ---- */}185 {!!data.heatmap?.cells?.length && (186 <>187 <SectionTitle kicker={fr ? "Activité" : "Activity"} title={fr ? "Ventes réelles au calendrier" : "Real sales calendar"} />188 <CalendarHeatmap title={data.heatmap.title} cells={data.heatmap.cells} />189 </>190 )}191192 {/* ---- 8. tableaux détaillés ---- */}193 {!!data.tables?.length && (194 <>195 <SectionTitle kicker={fr ? "Détails" : "Details"} title={fr ? "Tableaux détaillés" : "Detailed tables"} />196 <div className="grid gap-5">197 {data.tables.map((t) => <DataTable key={t.id} spec={t as TableSpec} />)}198 </div>199 </>200 )}201202 {/* ---- 9. records ---- */}203 {!!data.records?.length && (204 <>205 <SectionTitle kicker={fr ? "Faits marquants" : "Highlights"} title={fr ? "Records & faits marquants" : "Records & highlights"} />206 <div className="grid gap-3 sm:grid-cols-2">207 {data.records.map((r) => <RecordCard key={r.label} r={r} />)}208 </div>209 </>210 )}211212 {/* ---- 10. fraîcheur (rappel de bas de page) ---- */}213 <div className="mt-10 flex justify-between gap-3">214 <Fraicheur updated={data.updated} onRefresh={refresh} />215 <PdfButton period="tout" />216 </div>217 </div>218 );219}220