Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/components/cout/AssemblyCatalog.tsx9 * Module : components10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-09-0812 * Last Modified : 2026-09-0813 *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// Vrai-Prix — méthode du coût : catalogue des assemblages (/cout/assemblage) avec filtre par catégorie / recherche43// et, si ?item=, la fiche d'un article (prix canonique, historique, assemblages qui l'utilisent).44"use client";45import { useState } from "react";46import Link from "next/link";47import { useLang } from "@/components/LangContext";48import type { CostItem, ItemPrice, UxCategory } from "@/lib/cost/types";49import { UX_CATEGORIES, categoryLabel } from "@/lib/cost/taxonomy";50import { UNIT_LABELS } from "@/lib/cost/units";51import { num } from "@/components/avendre/fmt";52import { KindIcon, KindLegend, fmtDate } from "./Provenance";53import { PriceSourceCard } from "./SourceDrawer";54import { Sparkline } from "./Charts";5556export interface CatalogRow { code: string; nameFr: string; nameEn: string; unit: string; category: UxCategory; quality: string | null; economicLife: number | null; material: number; labour: number; equipment: number; direct: number; observedShare: number; freshestObservation: string | null; components: number }57export interface ItemCard { item: CostItem; price: ItemPrice | null; history: { date: string; price: number; source: string; url: string | null; regular: boolean; kind: string; outlier: boolean }[]; usedBy: { code: string; nameFr: string; nameEn: string; unit: string; quantity: number }[] }5859export default function AssemblyCatalog({ rows, asOf, item }: { rows: CatalogRow[]; asOf: string; item: ItemCard | null }) {60 const { lang } = useLang();61 const fr = lang === "fr";62 const [q, setQ] = useState("");63 const [cat, setCat] = useState<UxCategory | "">("");64 const unitL = (x: string) => UNIT_LABELS[x as keyof typeof UNIT_LABELS]?.[fr ? "fr" : "en"] ?? x;65 const fold = (s: string) => s.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase();66 const list = rows.filter((r) => (!cat || r.category === cat) && (!q || fold(fr ? r.nameFr : r.nameEn).includes(fold(q)) || r.code.toLowerCase().includes(q.toLowerCase())));67 const cats = UX_CATEGORIES.filter((c) => rows.some((r) => r.category === c.key));68 const hist = item ? (() => { const byDate = new Map<string, number[]>(); for (const h of item.history) if (!h.outlier && h.kind !== "reference") byDate.set(h.date, [...(byDate.get(h.date) ?? []), h.price]); return [...byDate.entries()].map(([date, ps]) => ({ date, price: ps.sort((a, b) => a - b)[Math.floor(ps.length / 2)] })).sort((a, b) => a.date.localeCompare(b.date)); })() : [];69 return (70 <div className="space-y-12 py-8">71 <section className="rise">72 <div className="flex flex-wrap items-baseline justify-between gap-x-4 gap-y-1 border-b-2 border-ink pb-2.5">73 <nav className="vp-mono flex flex-wrap items-center gap-2 text-[10.5px] uppercase tracking-[0.08em] text-ink-3">74 <Link href="/" className="text-ink-2 hover:text-accent-deep">Vrai Prix</Link><span aria-hidden="true">/</span>75 <Link href="/cout" className="text-ink-2 hover:text-accent-deep">{fr ? "Coût" : "Cost"}</Link><span aria-hidden="true">/</span>76 <span>{fr ? "Assemblages" : "Assemblies"}</span>77 </nav>78 <span className="vp-mono text-[10px] uppercase tracking-[0.08em] text-ink-3">{rows.length} {fr ? "assemblages · prix au" : "assemblies · prices as of"} {asOf} · Montréal</span>79 </div>80 <span className="kicker mt-6 inline-flex">{fr ? "Catalogue" : "Catalogue"}</span>81 <h1 className="vp-display mt-2 text-[clamp(26px,4vw,42px)] font-bold uppercase leading-[1.02] tracking-[-0.03em]">{fr ? "Les assemblages de construction" : "Construction assemblies"}</h1>82 <p className="mt-3 max-w-2xl text-[14.5px] leading-relaxed text-ink-2">{fr ? "Chaque assemblage décompose une composante réelle (mur, toiture, fenêtre, cuisine…) en articles chiffrés, heures de métier et équipement. Le coût direct par unité est recalculé à chaque synchronisation des sources." : "Each assembly breaks a real component (wall, roof, window, kitchen…) down into priced items, trade hours and equipment. The direct cost per unit is recomputed at every source synchronisation."}</p>83 </section>8485 {item && (86 <section className="grid gap-x-12 gap-y-6 border border-[var(--line-strong)] bg-surface p-5 lg:grid-cols-[1.2fr_1fr]">87 <div>88 <p className="kicker">{fr ? "Article" : "Item"} · {item.item.code}</p>89 <h2 className="vp-display mt-2 text-[22px] font-bold leading-tight">{fr ? item.item.nameFr : item.item.nameEn}</h2>90 <p className="vp-mono mt-1 text-[10.5px] uppercase tracking-[0.05em] text-ink-3">MasterFormat {item.item.masterformat} · {item.item.division} · {categoryLabel(item.item.category, fr)} · {fr ? "pertes par défaut" : "default waste"} {Math.round(item.item.defaultWastePct * 100)} %</p>91 <div className="mt-4"><PriceSourceCard fr={fr} itemName={fr ? item.item.nameFr : item.item.nameEn} unit={item.item.unit} price={item.price} /></div>92 </div>93 <div>94 <p className="klabel">{fr ? "Historique (médiane par date, aberrations exclues)" : "History (median per date, outliers excluded)"}</p>95 <div className="mt-2 max-w-[360px]"><Sparkline points={hist} lang={lang} /></div>96 {item.history.length > 0 && (97 <div className="src-wrap mt-3 max-h-[240px] overflow-y-auto"><table className="src-table"><thead><tr><th>{fr ? "Date" : "Date"}</th><th>{fr ? "Source" : "Source"}</th><th className="text-right">{fr ? "Prix" : "Price"}</th><th /></tr></thead><tbody>98 {[...item.history].reverse().slice(0, 40).map((h, i) => (<tr key={i} className={h.outlier ? "opacity-50" : ""}><td className="vp-mono text-[12px]">{h.date}</td><td className="text-[12.5px]">{h.url ? <a href={h.url} target="_blank" rel="noopener noreferrer nofollow" className="hover:text-accent-deep">{h.source} ↗</a> : h.source}</td><td className="vp-mono text-right text-[12px]">{num(h.price, lang, 2)} $</td><td className="vp-mono text-[9.5px] uppercase text-ink-3">{h.outlier ? (fr ? "aberrant" : "outlier") : !h.regular ? (fr ? "promo" : "sale") : ""}</td></tr>))}99 </tbody></table></div>100 )}101 <p className="klabel mt-4">{fr ? "Utilisé par" : "Used by"}</p>102 <ul className="mt-1 flex flex-wrap gap-2">103 {item.usedBy.map((u) => (<li key={u.code}><Link href={`/cout/assemblage/${encodeURIComponent(u.code)}`} className="vp-mono inline-block border border-[var(--line)] px-2 py-1 text-[10.5px] uppercase tracking-[0.05em] hover:border-ink">{fr ? u.nameFr : u.nameEn} · {num(u.quantity, lang, 3)} / {fr ? "unité" : "unit"}</Link></li>))}104 {!item.usedBy.length && <li className="text-[12.5px] text-ink-3">—</li>}105 </ul>106 </div>107 </section>108 )}109110 <section>111 <div className="flex flex-wrap items-end gap-4">112 <input value={q} onChange={(e) => setQ(e.target.value)} placeholder={fr ? "Rechercher un assemblage…" : "Search an assembly…"} className="vp-input max-w-sm text-base" aria-label={fr ? "Rechercher" : "Search"} />113 <div className="flex flex-wrap gap-1">114 <button type="button" aria-pressed={cat === ""} onClick={() => setCat("")} className="vp-tab">{fr ? "Toutes" : "All"}</button>115 {cats.map((c) => (<button key={c.key} type="button" aria-pressed={cat === c.key} onClick={() => setCat(c.key)} className="vp-tab">{fr ? c.fr : c.en}</button>))}116 </div>117 </div>118 <div className="mt-3"><KindLegend fr={fr} /></div>119 <div className="src-wrap mt-4">120 <table className="src-table">121 <thead><tr><th>{fr ? "Assemblage" : "Assembly"}</th><th>{fr ? "Catégorie" : "Category"}</th><th className="text-right">{fr ? "Matériaux" : "Materials"}</th><th className="text-right">{fr ? "Main-d'œuvre" : "Labour"}</th><th className="text-right">{fr ? "Équip." : "Equip."}</th><th className="text-right">{fr ? "Direct / unité" : "Direct / unit"}</th><th className="text-right">{fr ? "Observé" : "Observed"}</th><th>{fr ? "Vie" : "Life"}</th><th>{fr ? "Mise à jour" : "Updated"}</th></tr></thead>122 <tbody>123 {list.map((r) => (124 <tr key={r.code}>125 <td><Link href={`/cout/assemblage/${encodeURIComponent(r.code)}`} className="font-semibold hover:text-accent-deep">{fr ? r.nameFr : r.nameEn}</Link><br /><span className="vp-mono text-[10px] uppercase text-ink-3">{r.code} · {r.components} {fr ? "composants" : "components"}</span></td>126 <td className="text-[12.5px]">{categoryLabel(r.category, fr)}</td>127 <td className="vp-mono text-right text-[12px]">{num(r.material, lang, 2)}</td>128 <td className="vp-mono text-right text-[12px]">{num(r.labour, lang, 2)}</td>129 <td className="vp-mono text-right text-[12px]">{num(r.equipment, lang, 2)}</td>130 <td className="vp-mono text-right font-bold">{num(r.direct, lang, 2)} $ <span className="text-[10px] font-normal text-ink-3">/ {unitL(r.unit)}</span></td>131 <td className="vp-mono text-right text-[12px]"><KindIcon kind={r.observedShare >= 0.5 ? "observed" : r.observedShare > 0 ? "derived" : "reference"} fr={fr} /> {Math.round(r.observedShare * 100)} %</td>132 <td className="vp-mono text-[12px]">{r.economicLife ?? "—"}</td>133 <td className="vp-mono text-[11px]">{r.freshestObservation ? fmtDate(r.freshestObservation, fr) : (fr ? "référence" : "reference")}</td>134 </tr>135 ))}136 {!list.length && <tr><td colSpan={9} className="text-ink-2">{fr ? "Aucun assemblage." : "No assembly."}</td></tr>}137 </tbody>138 </table>139 </div>140 </section>141 </div>142 );143}144