Repository Public
1// Briques communes aux vues du Dashboard.23import { errorMessage, isSilent } from "../api.js";4import { h, icon, mount } from "../dom.js";5import { DASH, has } from "../format.js";6import { note } from "../ui.js";7import { sparkline } from "../charts.js";89export function card({ title, sub, actions, body, flush = false, cls = "" }) {10 const bodyEl = h("div", { class: `card-body${flush ? " flush" : ""}` }, body);11 const el = h("section", { class: `card ${cls}`.trim() },12 title ? h("div", { class: "card-head" }, h("div", null, h("h2", null, title), sub ? h("p", null, sub) : null), actions || null) : null,13 bodyEl);14 el.bodyEl = bodyEl;15 return el;16}1718/** Tuile chiffrée : set(valeur, unité, sous-texte) ; spark(valeurs). */19export function kpi(label, { color = "var(--c1)", spark = false } = {}) {20 const value = h("div", { class: "kpi-value" }, DASH);21 const sub = h("div", { class: "kpi-sub" }, " ");22 const sparkEl = spark ? h("div", { class: "kpi-spark" }) : null;23 const el = h("div", { class: "card kpi" }, h("div", { class: "kpi-label" }, label), value, sub, sparkEl);24 return {25 el,26 set(text, unit, subText) {27 mount(value, text, unit && text !== DASH ? h("small", null, unit) : null);28 sub.textContent = subText || " ";29 },30 spark(values) {31 if (sparkEl) mount(sparkEl, sparkline(values, { color, label: `${label} : évolution récente` }));32 },33 };34}3536export function meter(fraction, cls = "") {37 const f = has(fraction) ? Math.max(0, Math.min(1, fraction)) : 0;38 return h("div", { class: `meter ${cls}`.trim(), role: "presentation" }, h("i", { style: { width: `${(f * 100).toFixed(1)}%` } }));39}4041export function pill(text, kind = "", extra = "") {42 return h("span", { class: `pill ${kind} ${extra}`.trim() }, text);43}4445export function dl(rows) {46 return h("dl", { class: "dl" }, rows.filter(Boolean).map(([k, v]) => h("div", null, h("dt", null, k), h("dd", null, has(v) && v !== "" ? v : DASH))));47}4849export const PRESSURE = {50 normal: ["Normale", "ok"],51 warn: ["Élevée", "warn"],52 critical: ["Critique", "danger"],53};5455export function pressure(level) {56 return PRESSURE[level] || [has(level) ? String(level) : DASH, ""];57}5859export function errorBox(err, retry) {60 return note("danger", h("b", null, "Chargement impossible. "), errorMessage(err), " ",61 retry ? h("button", { class: "btn btn-sm", type: "button", style: { "margin-top": "8px", display: "flex" }, onclick: retry }, icon("refresh", "icon-sm"), "Réessayer") : null);62}6364/** Charge des données puis rend ; affiche un squelette, puis une erreur avec « Réessayer » le cas échéant. */65export async function load(host, fetcher, render) {66 if (!host.childNodes.length) mount(host, h("div", { class: "skeleton", "aria-hidden": "true" }), h("span", { class: "sr-only", role: "status" }, "Chargement…"));67 try {68 const data = await fetcher();69 render(data);70 return data;71 } catch (err) {72 if (!isSilent(err)) mount(host, errorBox(err, () => { mount(host); load(host, fetcher, render); }));73 return null;74 }75}7677/** Tableau responsive : colonnes = [{ label, cls, lead, render(row) }]. Sous 760 px, chaque ligne devient une carte. */78export function table(columns, rows, { stack = true, rowClass } = {}) {79 return h("div", { class: "table-wrap" },80 h("table", { class: `rt${stack ? " stack" : ""}` },81 h("thead", null, h("tr", null, columns.map((c) => h("th", { scope: "col", class: c.cls || null }, c.label)))),82 h("tbody", null, rows.map((row) => h("tr", { class: rowClass ? rowClass(row) : null },83 columns.map((c) => h("td", { class: [c.cls, c.lead ? "lead" : "", c.acts ? "acts" : ""].filter(Boolean).join(" ") || null, "data-label": c.label }, c.render(row))))))));84}85