SPB Git

Repository Public

ui.tsx 4.4 KB 61 lines tsx

1"use client";2import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";3import { api, ApiClientError } from "@/lib/client-api";45/** Loads JSON from the admin API. `reload` refetches; errors are surfaced, never swallowed. */6export function useApi<T>(path: string | null) {7  const [data, setData] = useState<T | null>(null), [error, setError] = useState<string | null>(null), [loading, setLoading] = useState(!!path);8  const reload = useCallback(async () => {9    if (!path) return;10    setLoading(true);11    try { setData(await api<T>(path)); setError(null); } catch (e) { setError(e instanceof ApiClientError ? e.message : "Error"); }12    setLoading(false);13  }, [path]);14  useEffect(() => { void reload(); }, [reload]);15  return { data, error, loading, reload, setData };16}1718export const errorText = (e: unknown, fr: boolean) => (e instanceof ApiClientError ? (e.code === "network" ? (fr ? "Connexion au serveur impossible." : "Cannot reach the server.") : e.message) : fr ? "Erreur inattendue." : "Unexpected error.");1920export function PageHeader({ title, lead, actions }: { title: string; lead?: string; actions?: ReactNode }) {21  return (22    <div className="mb-6 flex flex-wrap items-end justify-between gap-4">23      <div><h1 className="font-display text-3xl font-semibold tracking-tight sm:text-4xl">{title}</h1>{lead && <p className="mt-2 max-w-[70ch] text-muted">{lead}</p>}</div>24      {actions && <div className="flex flex-wrap gap-2">{actions}</div>}25    </div>26  );27}2829export function Notice({ tone = "info", children }: { tone?: "info" | "ok" | "warn" | "bad"; children: ReactNode }) {30  const c = { info: "border-line bg-raised", ok: "border-ok/40 bg-ok/10", warn: "border-warn/40 bg-warn/10", bad: "border-bad/40 bg-bad/10" }[tone];31  return <div role={tone === "bad" ? "alert" : "status"} className={`rounded-xl border px-4 py-3 text-sm leading-relaxed ${c}`}>{children}</div>;32}3334export function StatusPill({ status, fr }: { status: string; fr: boolean }) {35  const label: Record<string, [string, string]> = { draft: ["Brouillon", "Draft"], in_review: ["En révision", "In review"], published: ["Publiée", "Published"], archived: ["Archivée", "Archived"], queued: ["En file", "Queued"], running: ["En cours", "Running"], succeeded: ["Terminé", "Done"], failed: ["Échec", "Failed"], cancelled: ["Annulé", "Cancelled"] };36  const tone = status === "published" || status === "succeeded" ? "text-ok border-ok/40" : status === "failed" ? "text-bad border-bad/40" : status === "in_review" || status === "running" ? "text-warn border-warn/40" : "text-muted border-line";37  return <span className={`inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold ${tone}`}>{label[status]?.[fr ? 0 : 1] ?? status}</span>;38}3940/** Modal dialog on the native <dialog>: focus trap, Escape and backdrop handled by the browser. */41export function Dialog({ open, onClose, title, children, wide }: { open: boolean; onClose: () => void; title: string; children: ReactNode; wide?: boolean }) {42  const ref = useRef<HTMLDialogElement>(null);43  // The native "close" event also fires when we close the dialog ourselves (state changed, e.g. one dialog opening44  // another): only a close made by the person (Escape, backdrop, button) is reported to the owner.45  const byUs = useRef(false);46  useEffect(() => { const d = ref.current; if (!d) return; if (open && !d.open) d.showModal(); if (!open && d.open) { byUs.current = true; d.close(); } }, [open]);47  return (48    <dialog ref={ref} onClose={() => { if (byUs.current) byUs.current = false; else onClose(); }} onClick={(e) => { if (e.target === ref.current) onClose(); }}49      className={`m-auto w-[calc(100vw-1.5rem)] ${wide ? "max-w-3xl" : "max-w-lg"} rounded-2xl border border-line bg-surface p-0 text-ink shadow-card backdrop:bg-black/50`}>50      <div className="flex items-center justify-between gap-3 border-b border-line px-5 py-3">51        <h2 className="font-display text-xl font-semibold">{title}</h2>52        <button type="button" onClick={onClose} className="btn btn-ghost btn-sm !min-h-11 !px-3" aria-label="Fermer / Close">✕</button>53      </div>54      <div className="max-h-[75dvh] overflow-y-auto p-5">{open && children}</div>55    </dialog>56  );57}5859export const Spinner = () => <span className="inline-block h-4 w-4 animate-spin rounded-full border-2 border-current border-r-transparent" aria-hidden="true" />;60export const money = (usd: number) => (usd < 0.01 && usd > 0 ? "< 0,01 $ US" : `${usd.toFixed(2)} $ US`);61
Files main
Menu

Theme