import { useCallback, useEffect, useRef, useState } from "react"; import type { UpdateCheckResponse, UpdateStatusResponse } from "@shellbook/core"; import { settingsApi } from "./settingsApi"; import { Dialog } from "./ui"; type UpdateApi = Pick; /** How long a requested update may take to show up in systemd before we say so. */ const START_TIMEOUT_MS = 30_000; /** systemd timestamps have second resolution; allow for that and small clock steps. */ const START_SLACK_MS = 5_000; type Watch = | { phase: "starting" | "running" | "restarting"; since: number } | { phase: "done" | "failed" | "stalled" }; const when = (iso: string | null) => (iso ? new Date(iso).toLocaleString() : ""); /** * Settings card that starts ShellBook's own update. The server only requests it; systemd * runs the updater as root and restarts the service, so this card keeps polling through * the restart and then offers a reload for the new web app. */ export function UpdatePanel({ api = settingsApi, pollMs = 2000 }: { api?: UpdateApi; pollMs?: number }) { const [status, setStatus] = useState(); const [check, setCheck] = useState(); const [watch, setWatch] = useState(); const [confirming, setConfirming] = useState(false); const [error, setError] = useState(""); const logRef = useRef(null); // Initial load; an update already running (e.g. after a reload) is followed as well. useEffect(() => { let active = true; api.updateStatus().then( (next) => { if (!active) return; setStatus(next); if (next.state === "running") setWatch({ phase: "running", since: Date.parse(next.startedAt ?? "") || Date.now() }); }, () => active && setStatus("error"), ); return () => { active = false; }; }, [api]); const following = watch && (watch.phase === "starting" || watch.phase === "running" || watch.phase === "restarting"); const since = watch && "since" in watch ? watch.since : 0; const poll = useCallback(async () => { let next: UpdateStatusResponse; try { next = await api.updateStatus(); } catch { // The update restarts ShellBook; the API is gone for a moment. setWatch((current) => (current && "since" in current ? { ...current, phase: "restarting" } : current)); return; } setStatus(next); const started = next.startedAt !== null && Date.parse(next.startedAt) >= since - START_SLACK_MS; if (next.state === "running") setWatch({ phase: "running", since }); else if (!started) setWatch(Date.now() - since > START_TIMEOUT_MS ? { phase: "stalled" } : { phase: "starting", since }); else setWatch({ phase: next.state === "succeeded" ? "done" : "failed" }); }, [api, since]); useEffect(() => { if (!following) return; const timer = setInterval(() => void poll(), pollMs); return () => clearInterval(timer); }, [following, poll, pollMs]); // Keep the newest log lines in view while an update runs. const log = status && status !== "error" ? status.log : ""; useEffect(() => { if (following && logRef.current) logRef.current.scrollTop = logRef.current.scrollHeight; }, [log, following]); const start = async () => { setConfirming(false); setError(""); try { const { requestedAt } = await api.startUpdate(); setWatch({ phase: "starting", since: Date.parse(requestedAt) }); } catch { setError("Unable to start the update. It may already be running; reload this page."); } }; if (status === undefined) return (

Updates

Loading…

); if (status === "error") return (

Updates

Unable to load the update status.

); return (

Updates

Installed
{status.current ? {status.current} : "unknown (not a git checkout)"}
{status.available && status.startedAt && !following && ( <>
Last update
{status.state === "failed" ? "failed" : status.state === "succeeded" ? "succeeded" : status.state},{" "} {when(status.finishedAt ?? status.startedAt)}
)}
{!status.available ? (

Updating from the web UI is not set up on this installation. Run shellbook-update as root in the container (from the Proxmox host: pct exec <ctid> -- shellbook-update); that run also adds the button here.

) : ( <> {watch && (

{watch.phase === "starting" && "Starting the update…"} {watch.phase === "running" && "Updating… This takes a few minutes; ShellBook restarts at the end."} {watch.phase === "restarting" && "ShellBook is restarting…"} {watch.phase === "done" && ( <> Update finished{status.current ? ` (${status.current})` : ""}. Reload to use the new version. )} {watch.phase === "failed" && "The update failed. The log below shows why."} {watch.phase === "stalled" && "The update did not start. Check systemctl status shellbook-update.path in the container."}

)} {error &&

{error}

} {check === "error" &&

Unable to reach the repository.

} {check && check !== "checking" && check !== "error" && ( check.pinned ? (

This installation is pinned to a fixed version and is not updated from the branch.

) : check.changes.length === 0 ? (

Up to date{check.latest ? ` (${check.latest})` : ""}.

) : (

{check.changes.length === 1 ? "1 new change" : `${check.changes.length} new changes`} ( {check.current} → {check.latest}):

    {check.changes.map((change) => (
  • {change.commit} {change.subject}
  • ))}
) )}
{status.log && (following || watch?.phase === "failed" || watch?.phase === "done" || !watch) && (
{following ? "Update output" : "Output of the last update"}
                {status.log}
              
)} )} {confirming && ( setConfirming(false)}>

Downloads the installer from the repository, pulls the newest version, rebuilds and restarts ShellBook. Running steps are interrupted by the restart; terminal sessions keep running.

)}
); }