Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr SMS Gateway8 * File : dashboard/src/ui.tsx9 * Module : dashboard10 * Repository : https://git.spboucher.ai/maclustr-sms11 * Project Website : https://sms.spboucher.ai12 * Created : 2026-11-3013 * Last Modified : 2026-11-3014 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@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 */4142import { type ReactNode, useEffect, useState } from "react"4344export const fmtTime = (s: string | null | undefined) => (s ? new Date(s).toLocaleString("fr-CA", { dateStyle: "short", timeStyle: "medium" }) : "—")45export function ago(s: string | null | undefined) {46 if (!s) return "jamais"47 const d = (Date.now() - new Date(s).getTime()) / 100048 if (d < 5) return "à l'instant"; if (d < 60) return `il y a ${Math.round(d)} s`; if (d < 3600) return `il y a ${Math.round(d / 60)} min`49 if (d < 86400) return `il y a ${Math.round(d / 3600)} h`; return `il y a ${Math.round(d / 86400)} j`50}51export const pct = (v: number | null | undefined) => (v == null ? "—" : (v * 100).toFixed(1).replace(".", ",") + " %")52export const secs = (v: number | null | undefined) => (v == null ? "—" : v < 10 ? v.toFixed(1).replace(".", ",") + " s" : Math.round(v) + " s")5354const TONE: Record<string, string> = { delivered: "ok", submitted: "info", received: "info", queued: "", dispatched: "", accepted: "", submitting: "", held: "warn", failed: "bad", expired: "bad", cancelled: "", unknown: "warn", pending: "", in_flight: "info" }55const LABEL: Record<string, string> = { delivered: "livré", submitted: "remis au réseau", received: "reçu", queued: "en file", dispatched: "transmis à l'appareil", accepted: "accepté", submitting: "remise au modem", held: "à approuver", failed: "échec", expired: "expiré", cancelled: "annulé", unknown: "issue inconnue", pending: "en attente", in_flight: "en cours" }56export const Status = ({ s }: { s: string }) => <span className={`badge ${TONE[s] ?? ""}`} title={s}>{LABEL[s] ?? s}</span>57export const Dot = ({ tone }: { tone: "ok" | "warn" | "bad" | "" }) => <span className={`dot ${tone}`} />58export const Badge = ({ tone = "", children }: { tone?: string; children: ReactNode }) => <span className={`badge ${tone}`}>{children}</span>5960export function Page({ title, sub, actions, children }: { title: string; sub?: ReactNode; actions?: ReactNode; children: ReactNode }) {61 return <><div className="head"><div><h1>{title}</h1>{sub && <p>{sub}</p>}</div><div className="row">{actions}</div></div>{children}</>62}63export function Modal({ title, onClose, wide, children }: { title: string; onClose: () => void; wide?: boolean; children: ReactNode }) {64 useEffect(() => { const k = (e: KeyboardEvent) => e.key === "Escape" && onClose(); window.addEventListener("keydown", k); return () => window.removeEventListener("keydown", k) }, [onClose])65 return <div className="overlay" onMouseDown={e => e.target === e.currentTarget && onClose()}><div className={`modal ${wide ? "wide" : ""}`} role="dialog" aria-label={title}><h3>{title}</h3>{children}</div></div>66}67export function Table({ head, rows, empty }: { head: ReactNode[]; rows: ReactNode[] | null | undefined; empty: string }) {68 return <div className="tablewrap">{!rows ? <div className="empty">Chargement…</div> : rows.length === 0 ? <div className="empty">{empty}</div> : <table><thead><tr>{head.map((h, i) => <th key={i}>{h}</th>)}</tr></thead><tbody>{rows}</tbody></table>}</div>69}70export const Tile = ({ k, v, s, tone }: { k: string; v: ReactNode; s?: ReactNode; tone?: string }) => <div className="tile"><div className="k">{k}</div><div className="v" style={tone ? { color: `var(--${tone})` } : undefined}>{v}</div>{s && <div className="s">{s}</div>}</div>7172/** Exécute une action, affiche l'erreur éventuelle. */73export function useAction() {74 const [busy, setBusy] = useState(false), [error, setError] = useState<string | null>(null)75 const run = async (fn: () => Promise<unknown>) => { setBusy(true); setError(null); try { await fn(); return true } catch (e: any) { setError(e.message ?? String(e)); return false } finally { setBusy(false) } }76 return { busy, error, run, setError }77}78export function Copy({ text }: { text: string }) {79 const [done, setDone] = useState(false)80 return <button className="small" onClick={() => { void navigator.clipboard.writeText(text); setDone(true); setTimeout(() => setDone(false), 1500) }}>{done ? "Copié" : "Copier"}</button>81}82export function Confirm({ title, body, label, danger, onConfirm, onClose }: { title: string; body: ReactNode; label: string; danger?: boolean; onConfirm: () => Promise<unknown>; onClose: () => void }) {83 const a = useAction()84 return <Modal title={title} onClose={onClose}><div>{body}</div>{a.error && <p className="err">{a.error}</p>}85 <div className="actions"><button onClick={onClose}>Annuler</button><button className={danger ? "danger solid" : "primary"} disabled={a.busy} onClick={async () => { if (await a.run(onConfirm)) onClose() }}>{label}</button></div></Modal>86}87export function navigate(to: string) { history.pushState(null, "", to); window.dispatchEvent(new PopStateEvent("popstate")) }88export const Link = ({ to, children, className }: { to: string; children: ReactNode; className?: string }) => <a href={to} className={className} onClick={e => { if (e.metaKey || e.ctrlKey) return; e.preventDefault(); navigate(to) }}>{children}</a>89