diff --git a/frontend/src/AutoUpdate.tsx b/frontend/src/AutoUpdate.tsx index e5378be..985921f 100644 --- a/frontend/src/AutoUpdate.tsx +++ b/frontend/src/AutoUpdate.tsx @@ -1,7 +1,7 @@ -// Mises a jour automatiques programmees (crontab) : badge, editeur dans le -// detail d'une stack et vue de suivi de toutes les programmations. -import { useEffect, useState } from "react"; -import { api, type AutoInfo, type Me, type Scan, type Server, type Stack, type Target } from "./api"; +// Mises a jour automatiques programmees (crontab) : par stack (detail de la +// stack) et globale par serveur (dialogue du serveur), badges et vue de suivi. +import { useEffect, useState, type ReactNode } from "react"; +import { api, type AutoInfo, type HostAutoInfo, type Me, type Scan, type Server, type Stack, type Target } from "./api"; import { fmtDate, fmtRelative } from "./format"; import { CheckIcon, ClockIcon, RefreshIcon, WarningIcon } from "./icons"; @@ -13,6 +13,58 @@ export const AUTO_PRESETS: [string, string][] = [ ]; const norm = (s: string) => s.trim().split(/\s+/).join(" "); +const base = (dir: string) => dir.split("/").pop() || dir; + +/** Valide une planification cote serveur et renvoie ses prochaines executions. */ +function useCronPreview(expr: string, active: boolean) { + const [preview, setPreview] = useState<{ next?: string[]; error?: string }>({}); + useEffect(() => { + if (!active) return; + let live = true; + const h = setTimeout(() => { + api.cronPreview(expr).then( + (next) => live && setPreview({ next }), + (e: unknown) => live && setPreview({ error: e instanceof Error ? e.message : String(e) }), + ); + }, 300); + return () => { + live = false; + clearTimeout(h); + }; + }, [expr, active]); + return preview; +} + +/** Planification predefinie ou expression crontab libre. */ +function CronInputs({ expr, setExpr, children }: { expr: string; setExpr: (v: string) => void; children?: ReactNode }) { + const preset = AUTO_PRESETS.find(([p]) => p === norm(expr))?.[0] ?? ""; + return ( +
+ + setExpr(e.target.value)} placeholder="min heure jour mois jour-semaine" aria-label="Planification crontab" spellCheck={false} /> + {children} +
+ ); +} + +function CronPreview({ preview, tz }: { preview: { next?: string[]; error?: string }; tz?: string }) { + return ( +
+ {preview.error + ? preview.error + : preview.next?.length + ? `Prochaines executions : ${preview.next.map(fmtDate).join(", ")}${tz ? ` (heure ${tz})` : ""}` + : " "} +
+ ); +} /** Badge (couleur sarcelle) a cote du nom d'une stack programmee. */ export function AutoBadge({ auto }: { auto: AutoInfo }) { @@ -24,6 +76,16 @@ export function AutoBadge({ auto }: { auto: AutoInfo }) { ); } +/** Badge a cote du nom d'un serveur a mise a jour globale programmee. */ +export function HostAutoBadge({ auto }: { auto: HostAutoInfo }) { + const title = `Mise a jour globale programmee : ${auto.schedule}${auto.next ? `, prochaine le ${fmtDate(auto.next)}` : ""}`; + return ( + + {auto.running ? : } + + ); +} + /** Dernier resultat d'une execution programmee. */ export function AutoLast({ auto }: { auto: AutoInfo }) { if (auto.pending) return en attente (connexion occupee); @@ -39,40 +101,51 @@ export function AutoLast({ auto }: { auto: AutoInfo }) { ); } +/** Dernier resultat (ou execution en cours) d'une planification de serveur. */ +export function HostAutoLast({ auto }: { auto: HostAutoInfo }) { + if (auto.running) { + return ( + + Mise a jour en cours{auto.current ? ` : ${base(auto.current)}` : ""} + + ); + } + if (auto.pending) return en attente; + if (!auto.lastRun) return jamais executee; + const failed = (auto.results ?? []).filter((r) => !r.ok); + const title = failed.length ? failed.map((r) => `${r.name} : ${r.message}`).join("\n") : fmtDate(auto.lastRun); + return ( + + {auto.lastOk ? : } + + {fmtRelative(auto.lastRun)} + {auto.lastMessage ? ` : ${auto.lastMessage}` : ""} + + + ); +} + interface EditorProps { stack: Stack; busy: boolean; tz?: string; + /** Planification globale du serveur, remplacee par celle de la stack. */ + hostSchedule?: string; onSave: (schedule: string) => Promise; } /** Case « Mise a jour automatique programmee » et sa planification crontab. */ -export function AutoEditor({ stack, busy, tz, onSave }: EditorProps) { +export function AutoEditor({ stack, busy, tz, hostSchedule, onSave }: EditorProps) { const current = stack.auto?.schedule ?? ""; const [on, setOn] = useState(!!current); const [expr, setExpr] = useState(current || AUTO_PRESETS[0][0]); - const [preview, setPreview] = useState<{ next?: string[]; error?: string }>({}); + const preview = useCronPreview(expr, on); useEffect(() => { setOn(!!current); if (current) setExpr(current); }, [current]); - useEffect(() => { - if (!on) return; - let live = true; - const h = setTimeout(() => { - api.cronPreview(expr).then( - (next) => live && setPreview({ next }), - (e: unknown) => live && setPreview({ error: e instanceof Error ? e.message : String(e) }), - ); - }, 300); - return () => { - live = false; - clearTimeout(h); - }; - }, [expr, on]); - if (stack.excluded) { return (