import { useEffect, useState } from "react"; import { Link } from "react-router-dom"; import ImpactNote from "./ImpactNote"; import { api, type IntegrationSummary, type SemaphoreTemplate, type UpdatesServer } from "../api/client"; import { confirmDialog } from "../utils/dialogs"; import { readableError } from "../utils/errors"; import { toast } from "../utils/toast"; import DetailDrawer from "./DetailDrawer"; import { BlockSkeleton } from "./Skeleton"; import UpdatesBadge from "./UpdatesBadge"; interface Loaded { integration: IntegrationSummary; templates: SemaphoreTemplate[]; } const WINDOWS = [ { minutes: 45, label: "45 minutes" }, { minutes: 90, label: "1½ hours" }, { minutes: 180, label: "3 hours" }, { minutes: 360, label: "6 hours" }, ]; const LAST_KEY = "hlm.updateRun.template"; const remembered = () => { try { return localStorage.getItem(LAST_KEY) ?? ""; } catch { return ""; } }; /** * "Update this server now": pick the playbook that applies updates, and Homelab Manager puts the server under maintenance, runs it for this * server only, follows it, waits for the server to report again and says what is left and whether a restart is due. It asks before it * starts, and says what it will do. */ export default function UpdateRunDrawer({ server, onClose, onStarted }: { server: UpdatesServer; onClose: () => void; onStarted: () => void }) { const [loaded, setLoaded] = useState(null); const [failed, setFailed] = useState(false); const [choice, setChoice] = useState(remembered); const [limit, setLimit] = useState(server.name); const [minutes, setMinutes] = useState(90); const [busy, setBusy] = useState(false); // People streaming from a Jellyfin on this machine: the update may restart it, so they are named first and the person asked. const [watching, setWatching] = useState(""); useEffect(() => { let alive = true; api.jellyfin .watching(server.id) .then((r) => alive && setWatching(r.words)) .catch(() => undefined); // no Jellyfin, or it can't be read: nothing to warn about return () => { alive = false; }; }, [server.id]); useEffect(() => { let alive = true; api.integrations .list() .then(async (res) => { const sems = res.integrations.filter((i) => i.type === "semaphore" && i.enabled); const all = await Promise.all(sems.map((integration) => api.integrations.semaphore.templates(integration.id).then((r): Loaded => ({ integration, templates: r.templates })).catch((): Loaded => ({ integration, templates: [] })))); if (alive) setLoaded(all); }) .catch(() => alive && setFailed(true)); return () => { alive = false; }; }, []); const options = (loaded ?? []).flatMap((l) => l.templates.map((t) => ({ key: `${l.integration.id}:${t.projectId}:${t.id}`, integration: l.integration, template: t, label: `${t.name} — ${t.projectName}${(loaded ?? []).length > 1 ? ` (${l.integration.name})` : ""}` })), ); const picked = options.find((o) => o.key === choice); const count = server.updates?.count ?? null; async function start() { if (!picked) return; const ok = await confirmDialog({ title: `Update ${server.name} now?`, message: `${watching ? `${watching[0].toUpperCase()}${watching.slice(1)}, and the update may restart the machine. ` : ""}This silences alerts for ${server.name} for up to ${WINDOWS.find((w) => w.minutes === minutes)?.label ?? `${minutes} minutes`}, runs "${picked.template.name}" in ${picked.integration.name} limited to ${limit.trim()}, and waits for the server to report again. The playbook may restart it.`, confirmLabel: watching ? "Update anyway" : "Start the update", }); if (!ok) return; setBusy(true); try { await api.updateRuns.start({ serverId: server.id, integrationId: picked.integration.id, projectId: picked.template.projectId, templateId: picked.template.id, limit: limit.trim(), windowMinutes: minutes, startAnyway: watching !== "" }); try { localStorage.setItem(LAST_KEY, picked.key); } catch { // a remembered choice is only a convenience } toast.success(`Started updating ${server.name}.`); onStarted(); onClose(); } catch (err) { toast.error(readableError(err)); } finally { setBusy(false); } } return (
{count !== null && count > 0 && server.updates && server.updates.security !== null && {server.updates.security} of them security}
{failed ? (
Couldn't load the integrations.
) : loaded === null ? ( ) : loaded.length === 0 ? (
There is no Semaphore connected. Add one under Integrations, with a playbook that applies updates, and this server can be updated from here.
) : options.length === 0 ? (
No templates found in Semaphore.
) : ( <>
{watching && (
Somebody is watching. {watching[0].toUpperCase()} {watching.slice(1)}. The update may restart this machine; you will be asked once more.
)}
  1. Alerts for this server are silenced (a maintenance window).
  2. The playbook runs in Semaphore, for this server only.
  3. Homelab Manager waits for the server to report again — it may be restarting.
  4. It tells you what is still waiting and whether a restart is due, and the window ends.
setLimit(e.target.value)} placeholder="e.g. web01" />
An update run is always for this one server, never the whole inventory.
The window ends sooner, as soon as the run does.
)}
); }