[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
+99
View File
@@ -0,0 +1,99 @@
// 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/.
// Formatting only (handoff §3): times in the browser's zone, 24-hour clock, counts sentences.
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const LONG = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const two = (n: number) => String(n).padStart(2, "0");
/** "Today 14:02", "Yesterday 21:14", "5 Nov 21:14", "5 Nov 2025". */
export function when(iso: string | null | undefined, now = Date.now()): string {
if (!iso) return "";
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return "";
const n = new Date(now);
const hm = `${two(d.getHours())}:${two(d.getMinutes())}`;
const sameDay = (a: Date, b: Date) => a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
if (sameDay(d, n)) return `Today ${hm}`;
const y = new Date(n);
y.setDate(n.getDate() - 1);
if (sameDay(d, y)) return `Yesterday ${hm}`;
if (d.getFullYear() !== n.getFullYear()) return `${d.getDate()} ${MONTHS[d.getMonth()]} ${d.getFullYear()}`;
return `${d.getDate()} ${MONTHS[d.getMonth()]} ${hm}`;
}
/** "14 November 2026 at 14:20". */
export function longDate(iso: string | null | undefined, withTime = true): string {
if (!iso) return "";
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return "";
const s = `${d.getDate()} ${LONG[d.getMonth()]} ${d.getFullYear()}`;
return withTime ? `${s} at ${two(d.getHours())}:${two(d.getMinutes())}` : s;
}
/** "2 Mar 2027". */
export function shortDate(iso: string | null | undefined): string {
if (!iso) return "";
const d = new Date(iso);
return `${d.getDate()} ${MONTHS[d.getMonth()]} ${d.getFullYear()}`;
}
/** "8 s", "6 minutes", "2 hours", "3 days". */
export function duration(secs: number): string {
if (secs < 60) return `${Math.max(0, Math.round(secs))} s`;
const m = Math.round(secs / 60);
if (m < 60) return m === 1 ? "1 minute" : `${m} minutes`;
const h = Math.round(secs / 3600);
if (h < 48) return h === 1 ? "1 hour" : `${h} hours`;
const days = Math.round(secs / 86400);
return `${days} days`;
}
export function ago(iso: string | null | undefined, now: number): string {
if (!iso) return "";
return duration((now - Date.parse(iso)) / 1000) + " ago";
}
/** "7 hosts · 5 healthy, 1 degraded, 1 down" (handoff §5.1). */
export function countsSentence(c: { hosts: number; healthy: number; degraded: number; down: number; unknown: number }): string {
const parts = [`${c.healthy} healthy`];
if (c.degraded) parts.push(`${c.degraded} degraded`);
if (c.down) parts.push(`${c.down} down`);
if (c.unknown) parts.push(`${c.unknown} not checked`);
return `${c.hosts} ${c.hosts === 1 ? "host" : "hosts"} · ${parts.join(", ")}`;
}
/** Split a server message on its first sentence (handoff §7.5). */
export function firstSentence(m: string): [string, string] {
const i = m.indexOf(". ");
return i < 0 ? [m, ""] : [m.slice(0, i + 1), m.slice(i + 2)];
}
/** Field labels in sentences (handoff §6.5). */
export const FIELD_LABEL: Record<string, string> = {
hostname: "address",
upstreams: "sends traffic to",
tls: "certificate",
auth: "who can open it",
"health_check.path": "health check address",
"health_check.interval_secs": "checks every",
share_links: "share links",
device_certs: "device certificates",
access_log: "visit log",
websockets: "websockets",
headers: "header rules",
};
export function fieldValue(field: string, v: string): string {
if (v === "") return "none";
if (field === "tls") return v === "domi" ? "Domi certificate" : "Let's Encrypt";
if (field === "auth") return v === "forward-auth" ? "sign in first" : "anyone";
if (field === "access_log") return v === "truncated" ? "on" : "off";
if (v === "true") return "on";
if (v === "false") return "off";
if (field === "health_check.interval_secs") return `${v} s`;
return v;
}