[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
+141
View File
@@ -0,0 +1,141 @@
<!-- 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 §5: the main screen.
import { Banner, Button, Icon } from "@ianitor/ui";
import ChangeBar from "../components/ChangeBar.svelte";
import EnginePopover from "../components/EnginePopover.svelte";
import HostList from "../components/HostList.svelte";
import HostPanel from "../components/HostPanel.svelte";
import IntermediateBanner from "../components/IntermediateBanner.svelte";
import ShareDialog from "../components/ShareDialog.svelte";
import { call, newKey, post, sentence } from "../lib/api";
import { app, changesBlocked, navigate, refresh, toast } from "../lib/app.svelte";
import { countsSentence, when } from "../lib/format";
import type { ApplyView, HostSpec } from "../lib/types";
let { route }: { route: { newHost: boolean; edit?: string } } = $props();
const o = $derived(app.overview);
let prefill = $state<HostSpec | null>(null);
let focusField = $state<string | undefined>(undefined);
let problem = $state<string | null>(null);
const panelOpen = $derived(route.newHost || !!route.edit || app.panel?.kind === "host");
const blocked = $derived(changesBlocked());
function closePanel() {
prefill = null;
focusField = undefined;
if (app.panel?.kind === "host") app.panel = null;
if (route.newHost || route.edit) navigate("/");
void refresh();
}
async function remove(h: string) {
problem = null;
try {
await call("DELETE", `/hosts/${encodeURIComponent(h)}?base_rev=${encodeURIComponent(o?.rev ?? "")}`, undefined, { idem: newKey() });
await refresh();
} catch (e) {
problem = sentence(e);
}
}
async function retry(h: string) {
try {
await post(`/hosts/${encodeURIComponent(h)}/certificate/retry`);
toast("Trying Let's Encrypt again.");
} catch (e) {
problem = sentence(e);
}
}
function editAgain(a: ApplyView, field?: string) {
prefill = a.attempted_host ?? null;
focusField = field;
if (a.kind === "add" || !a.hosts[0]) {
navigate("/hosts/new");
} else {
navigate(`/hosts/${encodeURIComponent(a.hosts[0])}/edit`);
}
}
const engineName = $derived(o?.engine.running === "caddy" ? "Caddy" : "Pingora");
const engineLabel = $derived(
o?.engine.state === "switching" ? "Engine: switching" : `Engine: ${engineName}${o?.engine.state === "stopped" ? ", not answering" : ""}. Change engine`,
);
</script>
<div class="head">
<div>
<h1 class="ia-display">Hosts</h1>
{#if o}<p class="ia-text2">{countsSentence(o.counts)}</p>{/if}
</div>
<div class="actions">
{#if o}
<button type="button" class="ia-engine" aria-haspopup="dialog" aria-expanded={app.panel?.kind === "engine"} aria-label={engineLabel}
onclick={() => (app.panel = app.panel?.kind === "engine" ? null : { kind: "engine" })}>
{#if o.engine.state === "switching"}<Icon name="clock" /> Engine Switching…{:else}
<span class="ia-status" data-s={o.engine.state === "stopped" ? "down" : "healthy"}><span class="ia-dot"></span></span>
<small class="desktop">Engine</small> {engineName}{#if o.engine.running === "caddy"} <small>(fallback)</small>{/if}{/if}
<Icon name="chevron" />
</button>
{/if}
<Button variant="primary" icon="plus" label="Add a host" disabled={!!blocked} describedby={blocked ? "add-blocked" : undefined} onclick={() => navigate("/hosts/new")}>Host</Button>
{#if blocked}<span class="sr-only" id="add-blocked">{blocked}</span>{/if}
</div>
</div>
<IntermediateBanner />
{#if problem}<Banner tone="notice" live="status" ondismiss={() => (problem = null)}>{#snippet title()}{problem}{/snippet}</Banner>{/if}
<ChangeBar onshowsteps={(id) => (app.panel = { kind: "host", resume: id })} onedit={editAgain} />
{#if !o && !app.loadError}
<div class="ia-list" aria-busy="true"><span class="sr-only">Loading hosts…</span>
{#each [0, 1, 2] as i (i)}<div class="ia-row"><span class="ia-placeholder"></span><span class="ia-placeholder"></span><span class="ia-placeholder"></span><span></span></div>{/each}
</div>
{:else if app.loadError && !o}
<Banner tone="down" live="alert">{#snippet title()}This page could not load. Ianitor's control service may be restarting.{/snippet}
{#snippet actions()}<Button onclick={() => refresh()}>Try again now</Button>{/snippet}</Banner>
{:else if o && o.hosts.length === 0}
<div class="ia-frame"><h2 class="ia-h3">No hosts yet</h2><p class="ia-text2">Add the first one: the name people type, and where Ianitor should send them.</p></div>
{:else if o}
<HostList hosts={o.hosts}
onedit={(h) => navigate(`/hosts/${encodeURIComponent(h)}/edit`)}
onremove={remove}
onshare={(h) => (app.panel = { kind: "share", hostname: h })}
onhistory={(h) => navigate(`/history?host=${encodeURIComponent(h)}`)}
onretry={retry} />
{/if}
{#if o?.repo}
<hr class="ia-ornament" />
<p class="ia-muted footer">
Routes live in the {o.repo.name} repository, one file per host.
{#if o.repo.last_push_at}Last pushed to Gitea at {when(o.repo.last_push_at, app.now)}.{/if}
{#if o.repo.unpushed}{o.repo.unpushed} changes not yet pushed.{/if}
<button type="button" class="domi-btn link" onclick={() => navigate("/history")}>See history</button>
</p>
{/if}
{#if panelOpen}
{#key route.edit ?? (app.panel?.kind === "host" ? app.panel.resume : "new")}
<HostPanel hostname={route.edit} resume={app.panel?.kind === "host" ? app.panel.resume : undefined} {prefill} {focusField} onclose={closePanel} />
{/key}
{/if}
{#if app.panel?.kind === "share"}
<ShareDialog hostname={app.panel.hostname} onclose={() => { app.panel = null; void refresh(); }} />
{/if}
{#if app.panel?.kind === "engine"}
<EnginePopover onclose={() => (app.panel = null)} />
{/if}
<style>
.head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-end; gap: 16px; }
.actions { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.footer { text-align: center; }
@media (max-width: 719.98px) { .desktop { display: none; } }
</style>