feat(web): update button in Settings (#15)

Shows the installed commit, checks for new commits, starts the update after
a confirmation and follows its output through the service restart.
This commit is contained in:
Florian Schmidt
2026-11-16 15:31:01 +01:00
parent 936abbef9d
commit 2025185489
6 changed files with 373 additions and 3 deletions
+209
View File
@@ -0,0 +1,209 @@
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<typeof settingsApi, "updateStatus" | "checkUpdate" | "startUpdate">;
/** 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<UpdateStatusResponse | "error">();
const [check, setCheck] = useState<UpdateCheckResponse | "checking" | "error">();
const [watch, setWatch] = useState<Watch>();
const [confirming, setConfirming] = useState(false);
const [error, setError] = useState("");
const logRef = useRef<HTMLPreElement>(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 (
<section className="card">
<h2>Updates</h2>
<p className="muted">Loading…</p>
</section>
);
if (status === "error")
return (
<section className="card">
<h2>Updates</h2>
<p className="error">Unable to load the update status.</p>
</section>
);
return (
<section className="card update-panel" aria-label="Updates">
<h2>Updates</h2>
<dl className="facts">
<dt>Installed</dt>
<dd>{status.current ? <code>{status.current}</code> : "unknown (not a git checkout)"}</dd>
{status.available && status.startedAt && !following && (
<>
<dt>Last update</dt>
<dd>
{status.state === "failed" ? "failed" : status.state === "succeeded" ? "succeeded" : status.state},{" "}
{when(status.finishedAt ?? status.startedAt)}
</dd>
</>
)}
</dl>
{!status.available ? (
<p className="muted">
Updating from the web UI is not set up on this installation. Run <code>shellbook-update</code> as root in the
container (from the Proxmox host: <code>pct exec &lt;ctid&gt; -- shellbook-update</code>); that run also
adds the button here.
</p>
) : (
<>
{watch && (
<p role="status" className={`banner${watch.phase === "failed" || watch.phase === "stalled" ? " error" : ""}`}>
{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.
<button className="primary" onClick={() => location.reload()}>
Reload
</button>
</>
)}
{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."}
</p>
)}
{error && <p className="banner error">{error}</p>}
{check === "error" && <p className="error">Unable to reach the repository.</p>}
{check && check !== "checking" && check !== "error" && (
check.pinned ? (
<p className="muted">This installation is pinned to a fixed version and is not updated from the branch.</p>
) : check.changes.length === 0 ? (
<p className="ok">Up to date{check.latest ? ` (${check.latest})` : ""}.</p>
) : (
<div>
<p>
{check.changes.length === 1 ? "1 new change" : `${check.changes.length} new changes`} (
<code>{check.current}</code> → <code>{check.latest}</code>):
</p>
<ul className="changes">
{check.changes.map((change) => (
<li key={change.commit}>
<code>{change.commit}</code> {change.subject}
</li>
))}
</ul>
</div>
)
)}
<div className="actions">
<button
disabled={following || check === "checking"}
onClick={() => {
setCheck("checking");
api.checkUpdate().then(setCheck, () => setCheck("error"));
}}
>
{check === "checking" ? "Checking…" : "Check for updates"}
</button>
<button className="primary" disabled={following} onClick={() => setConfirming(true)}>
Update now
</button>
</div>
{status.log && (following || watch?.phase === "failed" || watch?.phase === "done" || !watch) && (
<details open={following || watch?.phase === "failed"}>
<summary>{following ? "Update output" : "Output of the last update"}</summary>
<pre ref={logRef} className="update-log">
{status.log}
</pre>
</details>
)}
</>
)}
{confirming && (
<Dialog title="Update ShellBook?" onClose={() => setConfirming(false)}>
<p>
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.
</p>
<div className="dialog-actions">
<button onClick={() => setConfirming(false)}>Cancel</button>
<button className="primary" onClick={() => void start()}>
Update now
</button>
</div>
</Dialog>
)}
</section>
);
}