SPB Git

Repository Public

ui.tsx 7.9 KB 106 lines tsx

1"use client";2import { useEffect, useId, useRef, useState, type ReactNode } from "react";3import { Icon, type IconName } from "@/components/ui/Icon";4import { errorText } from "./client";56export const VIS_LABEL = { private: "Privé", preview: "Aperçu seulement", public: "Public à la publication" } as const;7export const STATE_LABEL = { planned: "Planifiée", draft: "Brouillon", preview: "Aperçu" } as const;8export const JOB_LABEL = { queued: "En file", running: "En cours", succeeded: "Terminé", failed: "Échec", cancelled: "Annulé" } as const;9export const CAPSULE_LABEL = { empty: "Sans vidéo", queued: "En file d'attente", processing: "Encodage en cours", ready: "Prête", failed: "Échec de l'encodage" } as const;1011export function Badge({ tone = "", icon, children }: { tone?: "blue" | "teal" | "amber" | "coral" | "violet" | ""; icon?: IconName; children: ReactNode }) {12  return <span className={`chip ${tone ? `chip-${tone}` : ""}`}>{icon && <Icon name={icon} size={13} />}{children}</span>;13}1415/** The workflow position of a session, in words: never by colour alone. */16export function SessionBadges({ s }: { s: { state: keyof typeof STATE_LABEL; public: string; dirty: boolean; scheduled: unknown[]; inSource?: boolean } }) {17  return (18    <span className="inline-flex flex-wrap gap-1.5">19      {s.public === "published" ? <Badge tone="teal" icon="check">Publiée</Badge> : s.public === "archived" ? <Badge icon="lock">Archivée</Badge> : <Badge tone={s.state === "preview" ? "violet" : s.state === "draft" ? "amber" : ""} icon={s.state === "preview" ? "layers" : "clock"}>{STATE_LABEL[s.state]}</Badge>}20      {s.scheduled.length > 0 && <Badge tone="blue" icon="clock">Programmée</Badge>}21      {s.public === "published" && s.dirty && <Badge tone="amber">Modifications non publiées</Badge>}22      {s.inSource === false && <Badge>Hors dépôt</Badge>}23    </span>24  );25}2627export function Notice({ tone = "blue", title, children, testid }: { tone?: "blue" | "amber" | "teal" | "coral" | "violet"; title?: string; children?: ReactNode; testid?: string }) {28  const c = { blue: "border-blue bg-blue-soft", amber: "border-amber bg-amber-soft", teal: "border-teal bg-teal-soft", coral: "border-coral bg-coral-soft", violet: "border-violet bg-violet-soft" }[tone];29  return <div className={`rounded-xl border-l-4 p-3.5 ${c}`} role={tone === "coral" ? "alert" : "status"} data-testid={testid}>{title && <p className="font-semibold">{title}</p>}{children && <div className={`space-y-1.5 text-[0.92rem] ${title ? "mt-1" : ""}`}>{children}</div>}</div>;30}3132export function Field({ label, help, children, className = "" }: { label: string; help?: ReactNode; children: (id: string) => ReactNode; className?: string }) {33  const id = useId();34  return <div className={className}><label htmlFor={id} className="st-label">{label}</label>{children(id)}{help && <p className="st-help">{help}</p>}</div>;35}3637export function Panel({ title, actions, children, id, testid }: { title: string; actions?: ReactNode; children: ReactNode; id?: string; testid?: string }) {38  return (39    <section className="card p-4 md:p-5" aria-labelledby={id ? `${id}-t` : undefined} data-testid={testid}>40      <div className="mb-3 flex flex-wrap items-center justify-between gap-2"><h2 id={id ? `${id}-t` : undefined}>{title}</h2>{actions && <div className="flex flex-wrap gap-2">{actions}</div>}</div>41      {children}42    </section>43  );44}4546export function PageTitle({ eyebrow, title, children, actions }: { eyebrow?: string; title: string; children?: ReactNode; actions?: ReactNode }) {47  return (48    <header className="mb-6 flex flex-wrap items-end justify-between gap-3 border-b border-line pb-4">49      <div className="min-w-0">{eyebrow && <p className="eyebrow">{eyebrow}</p>}<h1 className="mt-1">{title}</h1>{children && <div className="mt-2 max-w-3xl text-muted">{children}</div>}</div>50      {actions && <div className="flex flex-wrap gap-2">{actions}</div>}51    </header>52  );53}5455export function Loading({ error, what = "les données" }: { error?: string; what?: string }) {56  return error ? <Notice tone="coral" title={`Impossible de charger ${what}.`}><p>{error}</p></Notice> : <p className="text-muted" role="status">Chargement…</p>;57}5859/** A modal dialog (native <dialog>: focus trap, Escape, backdrop). Closing restores focus to what opened it. */60export function Dialog({ open, onClose, title, children, wide = false, testid }: { open: boolean; onClose: () => void; title: string; children: ReactNode; wide?: boolean; testid?: string }) {61  const ref = useRef<HTMLDialogElement>(null), id = useId();62  useEffect(() => { const d = ref.current; if (!d) return; if (open && !d.open) d.showModal(); else if (!open && d.open) d.close(); }, [open]);63  return (64    <dialog ref={ref} className="st-dialog" style={wide ? { maxWidth: "min(56rem, calc(100vw - 2rem))" } : undefined} aria-labelledby={id} onClose={onClose} onCancel={onClose} data-testid={testid}>65      {open && <div className="p-5 md:p-6"><h2 id={id} className="!text-xl">{title}</h2><div className="mt-3">{children}</div></div>}66    </dialog>67  );68}6970/**71 * Confirmation of an action that changes what the public sees, or that cannot be undone. The action runs72 * only on the explicit button; an error stays in the dialog.73 */74export function Confirm({ open, onClose, title, children, confirmLabel, danger = false, action, testid, disabled = false }: { open: boolean; onClose: () => void; title: string; children?: ReactNode; confirmLabel: string; danger?: boolean; action: () => Promise<void>; testid?: string; disabled?: boolean }) {75  const [busy, setBusy] = useState(false), [error, setError] = useState("");76  const close = () => { setError(""); onClose(); };77  const run = async () => { setBusy(true); setError(""); try { await action(); close(); } catch (e) { setError(errorText(e)); } finally { setBusy(false); } };78  return (79    <Dialog open={open} onClose={close} title={title} testid={testid}>80      <div className="space-y-3 text-[0.95rem]">{children}</div>81      {error && <div className="mt-4"><Notice tone="coral"><p>{error}</p></Notice></div>}82      <div className="mt-5 flex flex-wrap justify-end gap-2">83        <button type="button" className="btn btn-ghost" onClick={close} disabled={busy}>Annuler</button>84        <button type="button" className={`btn ${danger ? "btn-danger" : "btn-accent"}`} onClick={run} disabled={busy || disabled} data-testid={testid ? `${testid}-ok` : undefined}>{busy ? "En cours…" : confirmLabel}</button>85      </div>86    </Dialog>87  );88}8990/** Runs an async action from a button, shows its failure next to it, and announces success to screen readers. */91export function useAction() {92  const [busy, setBusy] = useState(false), [error, setError] = useState(""), [done, setDone] = useState("");93  const run = async (fn: () => Promise<unknown>, success = "") => { setBusy(true); setError(""); setDone(""); try { await fn(); setDone(success); return true; } catch (e) { setError(errorText(e)); return false; } finally { setBusy(false); } };94  const feedback = <>{error && <Notice tone="coral"><p>{error}</p></Notice>}{done && <p role="status" className="text-sm font-medium text-teal">{done}</p>}</>;95  return { busy, error, done, run, feedback, clear: () => { setError(""); setDone(""); } };96}9798export function ProgressBar({ value, tone = "blue", label }: { value: number; tone?: "blue" | "teal" | "amber" | "coral"; label: string }) {99  const pct = Math.max(0, Math.min(100, Math.round(value * 100)));100  return <div className="st-bar" data-tone={tone} role="progressbar" aria-label={label} aria-valuemin={0} aria-valuemax={100} aria-valuenow={pct}><span style={{ width: `${pct}%` }} /></div>;101}102103export function VisibilitySelect({ value, onChange, label, id }: { value: keyof typeof VIS_LABEL; onChange: (v: keyof typeof VIS_LABEL) => void; label: string; id?: string }) {104  return <select id={id} className="field !w-auto" aria-label={label} value={value} onChange={(e) => onChange(e.target.value as keyof typeof VIS_LABEL)}>{Object.entries(VIS_LABEL).map(([k, l]) => <option key={k} value={k}>{l}</option>)}</select>;105}106
Files main
Menu

Theme