[web] The web UI: hosts, add/edit with apply progress, undo, history and restore, share links, engine switch, certificates and device installs, signing by hand, settings and tokens; smoke test counts every §4 flow's clicks

This commit is contained in:
2026-11-07 23:44:27 +01:00
parent aed5fbfcff
commit 3e326e85a0
47 changed files with 7868 additions and 3 deletions
+179
View File
@@ -0,0 +1,179 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
<script lang="ts">
// Handoff §8.1: History → pick → Restore this version (3 clicks with the nav click).
import { onMount } from "svelte";
import { Banner, Button, Status } from "@ianitor/ui";
import type { StatusKind } from "@ianitor/ui";
import HostPanel from "../components/HostPanel.svelte";
import { get, post, sentence } from "../lib/api";
import { app, changesBlocked, navigate } from "../lib/app.svelte";
import { FIELD_LABEL, fieldValue, longDate, when } from "../lib/format";
import type { ApplyView, HistoryEntry, HostSpec, RestorePreview } from "../lib/types";
let { host, pick }: { host?: string; pick?: string } = $props();
let entries = $state<HistoryEntry[]>([]);
let next = $state<string | null>(null);
let loaded = $state(false);
let problem = $state<string | null>(null);
let chosen = $state<string | null>(null);
let preview = $state<RestorePreview | null>(null);
let attempt = $state<ApplyView | null>(null);
let restoring = $state(false);
let resume = $state<string | null>(null);
let editFrom = $state<HostSpec | null>(null);
async function load(before?: string) {
try {
const q = new URLSearchParams({ limit: "50" });
if (host) q.set("host", host);
if (before) q.set("before", before);
const r = await get<{ entries: HistoryEntry[]; next_before: string | null }>(`/history?${q}`);
entries = before ? [...entries, ...r.entries] : r.entries;
next = r.next_before;
problem = null;
} catch (e) {
problem = sentence(e);
} finally {
loaded = true;
}
}
async function choose(id: string) {
chosen = id;
preview = null;
attempt = null;
try {
preview = await get<RestorePreview>(`/history/${encodeURIComponent(id)}/restore-preview`);
if (preview.blocked?.code === "restore.not_kept") attempt = await get<ApplyView>(`/history/${encodeURIComponent(id)}`);
} catch (e) {
problem = sentence(e);
}
}
async function restore() {
if (!chosen) return;
restoring = true;
try {
const a = await post<{ apply: ApplyView }>("/restore", { change_id: chosen, base_rev: app.overview?.rev ?? null });
resume = a.apply.id;
} catch (e) {
problem = sentence(e);
} finally {
restoring = false;
}
}
onMount(() => {
void load().then(() => {
if (pick) void choose(pick);
});
const focus = () => void load();
window.addEventListener("focus", focus);
return () => window.removeEventListener("focus", focus);
});
function stateOf(e: HistoryEntry): [StatusKind, string] {
switch (e.state) {
case "kept": return ["kept", "kept"];
case "reverted": return ["reverted", "reverted"];
case "failed": return ["reverted", "not applied"];
case "queued": return ["running", "waiting"];
default: return ["running", "applying"];
}
}
const entry = $derived(entries.find((e) => e.change_id === chosen));
const n = $derived(preview?.changes.length ?? 0);
const paused = $derived(app.overview?.paused ? !["apply.needs_attention", "control.files_unwritten"].includes(app.overview.paused_reason ?? "") : false);
const blocked = $derived(paused ? "Changes are paused." : changesBlocked() && !app.overview?.paused ? changesBlocked() : null);
function key(e: KeyboardEvent, i: number) {
const btns = Array.from(document.querySelectorAll<HTMLButtonElement>(".entries button"));
if (e.key === "ArrowDown") { e.preventDefault(); btns[Math.min(btns.length - 1, i + 1)]?.focus(); }
if (e.key === "ArrowUp") { e.preventDefault(); btns[Math.max(0, i - 1)]?.focus(); }
}
</script>
<h1 class="ia-display">{host ? `History of ${host}` : "History"}</h1>
{#if host}<div><button type="button" class="domi-btn link" onclick={() => navigate("/history")}>Show all changes</button></div>{/if}
{#if problem}<Banner tone="notice" live="status">{#snippet title()}{problem}{/snippet}</Banner>{/if}
<div class="cols">
<div class="list">
{#if loaded && entries.length === 0}
<div class="ia-frame"><h2 class="ia-h3">No changes yet</h2><p class="ia-text2">Every change you apply is listed here, newest first.</p></div>
{/if}
<ul class="entries">
{#each entries as e, i (e.change_id)}
{@const [k, w] = stateOf(e)}
<li>
<button type="button" class="entry" aria-current={chosen === e.change_id ? "true" : undefined} onclick={() => choose(e.change_id)} onkeydown={(ev) => key(ev, i)}>
<span class="sum">{e.summary}</span><Status kind={k} word={w} />
<span class="ia-sub">{when(e.at, app.now)} · {e.state === "kept" ? (e.commit_short ?? "") : (e.reason_short ?? "")}</span>
</button>
</li>
{/each}
</ul>
{#if next}<Button onclick={() => load(next ?? undefined)}>Show older changes</Button>{/if}
</div>
{#if chosen && preview}
<section class="detail" aria-label="Version">
<p class="ia-label">Version of {when(entry?.at, app.now)}</p>
{#if preview.blocked?.code === "restore.not_kept"}
<h2 class="ia-h3">This change did not stay</h2>
{#if attempt?.reason}<p>{attempt.reason.message}</p>{/if}
<p class="ia-text2">This change was undone at the time, so there is nothing to go back to.</p>
{#if attempt?.attempted_host}<div><Button variant="outline" onclick={() => (editFrom = attempt?.attempted_host ?? null)}>Edit and try again</Button></div>{/if}
{:else if preview.blocked?.code === "restore.same"}
<h2 class="ia-h3">Go back to this version?</h2>
<p class="ia-text2">This version is the same as the current one. Nothing to restore.</p>
<div class="buttons"><Button onclick={() => (chosen = null)}>Close</Button></div>
{:else}
<h2 class="ia-h3">Go back to this version?</h2>
<p>{n === 1 ? "Restoring puts 1 host back to how it was then:" : `Restoring puts ${n} hosts back to how they were then:`}</p>
<ul>
{#each preview.changes as c (c.hostname)}
{#if c.effect === "added"}<li><strong>{c.hostname}</strong> is added back</li>
{:else if c.effect === "removed"}<li><strong>{c.hostname}</strong> is removed</li>
{:else}
{#each c.fields as f (f.field)}
<li><strong>{c.hostname}</strong>: {#if f.field === "headers"}header rules change{:else}{FIELD_LABEL[f.field] ?? f.field} changes from {fieldValue(f.field, f.from)} to {fieldValue(f.field, f.to)}{/if}</li>
{/each}
{/if}
{/each}
</ul>
<p class="ia-hint">Applied as a new commit with the same {app.overview?.site?.watch_seconds ?? 60}-second watch. If health checks fail, it is undone.</p>
{#if blocked}<p class="ia-hint" id="restore-blocked">{blocked}</p>{/if}
<div class="buttons">
<Button onclick={() => (chosen = null)}>Close</Button>
<Button variant="primary" onclick={restore} pending={restoring} disabled={!!blocked} describedby={blocked ? "restore-blocked" : undefined}>Restore this version</Button>
</div>
{/if}
</section>
{/if}
</div>
{#if resume}
<HostPanel resume={resume} onclose={() => { resume = null; void load(); }} />
{/if}
{#if editFrom}
<HostPanel prefill={editFrom} hostname={undefined} onclose={() => { editFrom = null; void load(); }} />
{/if}
<span class="sr-only">{longDate(entry?.at)}</span>
<style>
.cols { display: flex; gap: 24px; flex-wrap: wrap; align-items: flex-start; }
.list { flex: 1 1 360px; max-width: 520px; display: flex; flex-direction: column; gap: 8px; }
.entries { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.entry { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; width: 100%; min-height: 44px; padding: 10px 12px; border-radius: var(--radius-sm); border: 1px solid transparent; background: none; color: var(--text); text-align: left; cursor: pointer; }
.entry:hover { background: var(--surface); }
.entry[aria-current="true"] { background: var(--surface-2); border-color: color-mix(in srgb, var(--accent) 55%, transparent); }
.sum { font-weight: 600; }
.detail { flex: 2 1 360px; background: var(--colonnade); border: 1px solid var(--hairline); border-radius: var(--radius-lg); padding: 20px; display: flex; flex-direction: column; gap: 10px; }
.detail ul { margin: 0; padding-left: 1.1rem; }
.buttons { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
</style>