Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/components/cout/SourceDrawer.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 : tiroir latéral générique + fiche « Source » d'un prix43// (source, date, URL, valeur observée, unité d'origine, conversion, facteur régional, méthode, synchro, confiance).44"use client";45import { useEffect } from "react";46import type { ItemPrice, Provenance } from "@/lib/cost/types";47import { UNIT_LABELS } from "@/lib/cost/units";48import { fmtDate, KindIcon, kindLabel } from "./Provenance";49import { money, num } from "@/components/avendre/fmt";5051export function Drawer({ open, onClose, title, children, fr }: { open: boolean; onClose: () => void; title: string; children: React.ReactNode; fr: boolean }) {52 useEffect(() => {53 if (!open) return;54 const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); };55 document.addEventListener("keydown", onKey);56 const prev = document.body.style.overflow;57 document.body.style.overflow = "hidden";58 return () => { document.removeEventListener("keydown", onKey); document.body.style.overflow = prev; };59 }, [open, onClose]);60 if (!open) return null;61 return (62 <div className="fixed inset-0 z-[var(--z-modal,900)] flex justify-end" role="dialog" aria-modal="true" aria-label={title}>63 <button type="button" aria-label={fr ? "Fermer" : "Close"} onClick={onClose} className="absolute inset-0 bg-[rgba(22,24,27,0.45)]" />64 <aside className="relative flex h-full w-full max-w-[520px] flex-col overflow-y-auto border-l border-[var(--line-strong)] bg-paper shadow-xl">65 <div className="sticky top-0 flex items-center justify-between border-b-2 border-ink bg-paper px-5 py-3">66 <p className="vp-display text-[15px] font-bold uppercase tracking-[-0.01em]">{title}</p>67 <button type="button" onClick={onClose} className="vp-mono flex h-10 w-10 items-center justify-center text-[15px]" aria-label={fr ? "Fermer" : "Close"}>✕</button>68 </div>69 <div className="px-5 pb-[max(24px,env(safe-area-inset-bottom))] pt-4">{children}</div>70 </aside>71 </div>72 );73}7475export function KV({ k, v, mono = false }: { k: string; v: React.ReactNode; mono?: boolean }) {76 return (77 <div className="kv-cell">78 <p className="k">{k}</p>79 <p className={`v ${mono ? "vp-mono text-[13px] font-medium" : ""}`}>{v}</p>80 </div>81 );82}8384/** Contenu d'un tiroir source pour un prix canonique d'article. */85export function PriceSourceCard({ price, fr, itemName, unit, locationFactor, locationName, lastSync }: { price: ItemPrice | Provenance | null; fr: boolean; itemName: string; unit?: string; locationFactor?: number; locationName?: string; lastSync?: string | null }) {86 const lang = fr ? "fr" : "en";87 if (!price) return <p className="text-[13.5px] text-ink-2">{fr ? "Donnée non disponible." : "Data not available."}</p>;88 const isItem = "itemCode" in price;89 const kind = price.kind;90 const unitLabel = unit ? (UNIT_LABELS[unit as keyof typeof UNIT_LABELS]?.[lang] ?? unit) : "";91 return (92 <div className="space-y-4">93 <div>94 <p className="klabel">{fr ? "Article" : "Item"}</p>95 <p className="vp-display mt-1 text-[17px] font-bold">{itemName}</p>96 <p className="mt-1 inline-flex items-center gap-2 text-[12.5px] text-ink-2"><KindIcon kind={kind} fr={fr} source={isItem ? price.sources[0]?.source : price.source} observedAt={price.observedAt} note={price.note} /> {kindLabel(kind, fr)}</p>97 </div>98 <div className="grid grid-cols-2 gap-x-6 gap-y-2">99 {isItem ? (100 <>101 <KV k={fr ? "Prix canonique" : "Canonical price"} v={`${num(price.price, lang, 2)} $ / ${unitLabel}`} mono />102 <KV k={fr ? "Fourchette (min–max sources)" : "Range (min–max sources)"} v={`${num(price.low, lang, 2)} – ${num(price.high, lang, 2)} $`} mono />103 <KV k={fr ? "Nombre de sources" : "Source count"} v={price.sourceCount} mono />104 <KV k={fr ? "Date d'observation" : "Observation date"} v={fmtDate(price.observedAt, fr)} />105 <KV k={fr ? "Confiance" : "Confidence"} v={`${price.confidence} / 100`} mono />106 <KV k={fr ? "Méthode" : "Method"} v={fr ? (price.sourceCount >= 2 ? "Médiane robuste multi-source (prix régulier préféré, aberrations écartées)" : price.kind === "indexed" ? "Actualisation par indice StatCan" : price.kind === "reference" ? "Prix de référence interne (hypothèse)" : "Observation unique") : price.sourceCount >= 2 ? "Robust multi-source median (regular price preferred, outliers flagged)" : price.kind === "indexed" ? "StatCan index update" : price.kind === "reference" ? "Internal reference price (assumption)" : "Single observation"} />107 </>108 ) : (109 <>110 <KV k={fr ? "Source" : "Source"} v={price.source} />111 <KV k={fr ? "Date" : "Date"} v={fmtDate(price.observedAt ?? price.effectiveDate, fr)} />112 <KV k={fr ? "Confiance" : "Confidence"} v={`${price.confidence} / 100`} mono />113 </>114 )}115 {locationFactor != null && <KV k={fr ? "Facteur régional (matériaux)" : "Regional factor (materials)"} v={`× ${num(locationFactor, lang, 3)}${locationName ? ` — ${locationName}` : ""}`} mono />}116 {lastSync !== undefined && <KV k={fr ? "Dernière synchronisation" : "Last synchronisation"} v={lastSync ? fmtDate(lastSync, fr) : fr ? "Donnée non disponible" : "Data not available"} />}117 </div>118 {isItem && price.indexed && (119 <p className="border-l-2 border-accent pl-3 text-[12.5px] text-ink-2">120 {fr ? `Prix du ${fmtDate(price.indexed.fromDate, fr)} actualisé au ${fmtDate(price.indexed.toDate, fr)} avec l'indice ${price.indexed.index} (× ${num(price.indexed.factor, lang, 3)}).` : `Price of ${fmtDate(price.indexed.fromDate, fr)} updated to ${fmtDate(price.indexed.toDate, fr)} with the ${price.indexed.index} index (× ${num(price.indexed.factor, lang, 3)}).`}121 </p>122 )}123 {price.note && <p className="border-l-2 border-[var(--amber)] pl-3 text-[12.5px] text-ink-2">{price.note}</p>}124 {isItem && price.sources.length > 0 && (125 <div className="src-wrap">126 <table className="src-table">127 <thead><tr><th>{fr ? "Source" : "Source"}</th><th className="text-right">{fr ? "Valeur observée" : "Observed value"}</th><th>{fr ? "Unité d'origine" : "Original unit"}</th><th>{fr ? "Conversion" : "Conversion"}</th><th>{fr ? "Date" : "Date"}</th></tr></thead>128 <tbody>129 {price.sources.map((s, i) => (130 <tr key={i}>131 <td>{s.url ? <a href={s.url} target="_blank" rel="noopener noreferrer nofollow" className="border-b border-transparent text-accent hover:border-accent">{s.source} ↗</a> : s.source}{!s.regular && <span className="vp-mono ml-1 text-[9px] uppercase text-[var(--amber)]">{fr ? "promo" : "sale"}</span>}</td>132 <td className="vp-mono text-right">{s.conversionFactor !== 1 ? `${num(s.price / s.conversionFactor, lang, 2)} $` : `${num(s.price, lang, 2)} $`}</td>133 <td className="vp-mono text-[12px]">{s.sourceUnit}</td>134 <td className="vp-mono text-[12px]">{s.conversionFactor === 1 ? "—" : `× ${s.conversionFactor < 0.01 ? s.conversionFactor.toExponential(2) : num(s.conversionFactor, lang, 4)} → ${num(s.price, lang, 2)} $/${unitLabel}`}</td>135 <td className="vp-mono text-[12px]">{s.observedAt}</td>136 </tr>137 ))}138 </tbody>139 </table>140 </div>141 )}142 <p className="vp-mono text-[10px] uppercase tracking-[0.05em] text-ink-3">{fr ? "Chaque nombre affiché est retraçable : source → observation → conversion → prix canonique → formule → résultat." : "Every displayed number is traceable: source → observation → conversion → canonical price → formula → result."}</p>143 </div>144 );145}146147export { money };148