Repository Public
1"use client";2import Link from "next/link";3import { useState } from "react";4import { api, fmtDate, useLoad } from "./client";5import type { Job, JobState } from "./types";6import { Badge, JOB_LABEL, Loading, Notice, PageTitle, ProgressBar, useAction } from "./ui";78const TONE: Record<JobState, "blue" | "teal" | "coral" | ""> = { queued: "", running: "blue", succeeded: "teal", failed: "coral", cancelled: "" };9const KIND: Record<string, string> = { transcode: "Encodage vidéo" };1011export function JobsPanel() {12 const { data, error, reload } = useLoad<{ jobs: Job[]; counts: Partial<Record<JobState, number>> }>("jobs", { pollMs: 3000 }), act = useAction(), [open, setOpen] = useState<number | null>(null);13 if (!data) return <><PageTitle eyebrow="Studio" title="Traitements" /><Loading error={error} what="les traitements" /></>;14 const run = (path: string) => act.run(async () => { await api(path, { body: {} }); await reload(); });15 return (16 <div data-testid="jobs-panel">17 <PageTitle eyebrow="Studio" title="Traitements">18 <p>Les encodages vidéo sont exécutés par un processus séparé du site, un à la fois par défaut. Un traitement interrompu est repris automatiquement ; un échec est réessayé (trois essais), puis signalé ici.</p>19 </PageTitle>20 <ul className="mb-4 flex flex-wrap gap-2">{(Object.keys(JOB_LABEL) as JobState[]).map((k) => <li key={k} className="chip" data-count={k}>{JOB_LABEL[k]} : {data.counts[k] ?? 0}</li>)}</ul>21 {act.feedback}22 <div className="card st-scroll">23 <table className="st-table">24 <thead><tr><th scope="col">Nº</th><th scope="col">Traitement</th><th scope="col">État</th><th scope="col">Avancement</th><th scope="col">Créé</th><th scope="col"><span className="sr-only">Actions</span></th></tr></thead>25 <tbody>26 {data.jobs.map((j) => { const p = j.payload as { sessionId?: string; capsuleId?: string }; return (27 <tr key={j.id} data-job={j.id} data-state={j.state}>28 <td className="st-mono">{j.id}</td>29 <td>{KIND[j.kind] ?? j.kind}{p.sessionId && <><br /><Link href={`/admin/seances/${p.sessionId}#capsules`} className="link text-sm">{p.sessionId} / {p.capsuleId}</Link></>}</td>30 <td><Badge tone={TONE[j.state]}>{JOB_LABEL[j.state]}</Badge><p className="mt-1 text-sm text-muted">essai {j.attempts} sur {j.maxAttempts}</p></td>31 <td className="min-w-[14rem]">{j.state === "running" || j.state === "queued" ? <><ProgressBar value={j.progress} label={`Avancement du traitement ${j.id}`} /><p className="mt-1 text-sm text-muted">{j.step ?? "en attente"}{j.cancelRequested ? " — annulation demandée" : ""}</p></> : j.error ? <><p className="text-sm text-coral">{j.error.slice(0, 220)}</p>{j.error.length > 220 && <button type="button" className="link text-sm" onClick={() => setOpen(open === j.id ? null : j.id)}>{open === j.id ? "Réduire" : "Tout lire"}</button>}{open === j.id && <pre className="st-pre mt-2">{j.error}</pre>}</> : <span className="text-sm text-muted">{j.step ?? "—"}</span>}</td>32 <td className="text-sm text-muted">{fmtDate(j.createdAt)}{j.finishedAt ? <><br />fini : {fmtDate(j.finishedAt)}</> : null}</td>33 <td><div className="flex justify-end gap-1.5">34 {(j.state === "queued" || j.state === "running") && <button type="button" className="btn btn-ghost btn-sm" disabled={act.busy || j.cancelRequested} onClick={() => run(`jobs/${j.id}/cancel`)}>Annuler</button>}35 {(j.state === "failed" || j.state === "cancelled") && <button type="button" className="btn btn-ghost btn-sm" disabled={act.busy} onClick={() => run(`jobs/${j.id}/retry`)}>Relancer</button>}36 </div></td>37 </tr>38 ); })}39 {!data.jobs.length && <tr><td colSpan={6} className="text-muted">Aucun traitement n’a encore été demandé. Ils apparaissent ici dès qu’une vidéo est téléversée dans une capsule.</td></tr>}40 </tbody>41 </table>42 </div>43 {(data.counts.queued ?? 0) > 0 && !(data.counts.running ?? 0) && <div className="mt-4"><Notice tone="amber"><p>Des traitements attendent et aucun n’est en cours : vérifiez sur le tableau de bord que le processus de traitement tourne et que ffmpeg est disponible.</p></Notice></div>}44 </div>45 );46}47