Hub 2.17.0: popups on the server, settings, install and invoice pages

- Server page: Configuration is a read view with an Edit popup; Update agent is a popup.
- frp config: the file shows read-only; Edit opens it in a popup with Restart, the two-factor
  code and Save on the server (its confirmation unchanged).
- Ports & defaults: install defaults, port range and central IP as a read view with an Edit popup;
  Mark as taken is a popup from the port list.
- Install page (/installs/[id]): Clear public key is a popup with the two-factor code; the notes
  are a read view with an Edit popup; the page message is under the header.
- Invoice: always the read view; Edit (draft, approved) opens a popup, so Approve no longer needs
  a save first. Void and Refund are popups instead of prompt().
- docs/POPUPS-PLAN.md done. Frontend only: no migration, no API changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-23 12:42:52 +02:00
co-authored by Claude Opus 5.5
parent a600b67ea8
commit 691f46844d
11 changed files with 385 additions and 209 deletions
+66 -39
View File
@@ -1,13 +1,13 @@
"use client";
import React, { useCallback, useEffect, useState } from "react";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import Link from "next/link";
import { useParams, useRouter } from "next/navigation";
import { Plus, Trash2 } from "lucide-react";
import { Plus, Trash2, Upload } from "lucide-react";
import { api, errorText } from "@/lib/api";
import { ago, bytes, fmtDate } from "@/lib/format";
import { useServerPaged } from "@/lib/paging";
import { Loading, Message, PageTitle, Pager } from "@/components/ui";
import { Field, Fields, FormError, Loading, Message, Modal, ModalButtons, PageMessage, PageTitle, Pager, ReadCard, useSubmit } from "@/components/ui";
import { PercentChart, ServersNav, StatusPill, pct, type Deployment, type Job, type ServerSummary } from "@/components/servers";
import { FrpConfig, type FrpState } from "@/components/FrpConfig";
@@ -32,36 +32,56 @@ const SUGGESTED: { key: string; hint: string }[] = [
{ key: "DB_ADMIN_URL", hint: "postgresql://admin:…@host/postgres: may create databases and roles. Blank: the local PostgreSQL via sudo -u postgres" },
];
function ConfigEditor({ id, config, onSaved }: { id: string; config: Record<string, string>; onSaved: (m: Msg) => void }) {
const [rows, setRows] = useState<[string, string][]>(() => {
const keys = [...new Set([...SUGGESTED.map(s => s.key), ...Object.keys(config)])];
return keys.map(k => [k, config[k] ?? (k === "TRUST_PROXY" ? "loopback" : "")]);
});
/** The keys a church server's configuration shows: the suggested ones, then any others it has. */
const configKeys = (config: Record<string, string>) => [...new Set([...SUGGESTED.map(x => x.key), ...Object.keys(config)])];
/** Hub 2.17.0: the server's configuration, edited in a popup. */
function ConfigModal({ id, config, onClose, onSaved }: { id: string; config: Record<string, string>; onClose: () => void; onSaved: (text: string) => void }) {
const initial = useMemo<[string, string][]>(() => configKeys(config).map(k => [k, config[k] ?? (k === "TRUST_PROXY" ? "loopback" : "")]), [config]);
const [rows, setRows] = useState(initial);
const { busy, error, run } = useSubmit();
const set = (i: number, j: 0 | 1, v: string) => setRows(rows.map((r, n) => (n === i ? (j === 0 ? [v.toUpperCase(), r[1]] : [r[0], v]) : r)));
const save = async () => {
try {
const save = (e: React.FormEvent) => {
e.preventDefault();
run(async () => {
await api(`/api/admin/servers/${id}/config`, { method: "PUT", body: { values: Object.fromEntries(rows.filter(r => r[0].trim())) } });
onSaved({ type: "ok", text: "Configuration saved. Jobs from now on carry it, sealed to this server's key." });
} catch (e) { onSaved({ type: "err", text: errorText(e) }); }
onSaved("Configuration saved. Jobs from now on carry it, sealed to this server's key.");
});
};
return (
<div className="card">
<h2 className="font-medium">Configuration</h2>
<p className="mt-1 text-sm text-slate-500">The server layer of every install&apos;s .env here. Stored encrypted; sent only inside jobs, sealed to this server. Values ending in _URL, KEY, SECRET or PASSWORD show as dots: leave them to keep them.</p>
<div className="mt-3 space-y-2">
{rows.map(([k, v], i) => (
<div key={i} className="grid gap-2 md:grid-cols-[14rem_1fr_auto]">
<input className="input font-mono" value={k} onChange={e => set(i, 0, e.target.value)} placeholder="NAME" aria-label="Name" />
<input className="input font-mono" value={v} onChange={e => set(i, 1, e.target.value)} placeholder={SUGGESTED.find(s => s.key === k)?.hint || "value"} aria-label={`${k} value`} />
<button type="button" className="btn-secondary" onClick={() => setRows(rows.filter((_, n) => n !== i))} aria-label={`Remove ${k}`}><Trash2 className="h-4 w-4" /></button>
</div>
))}
</div>
<div className="mt-3 flex gap-2">
<Modal title="Configuration" onClose={onClose} dirty={JSON.stringify(rows) !== JSON.stringify(initial)} wide>
<form onSubmit={save} className="space-y-3">
<p className="text-sm text-slate-500">Values ending in _URL, KEY, SECRET or PASSWORD show as dots: leave them to keep them.</p>
<div className="space-y-2">
{rows.map(([k, v], i) => (
<div key={i} className="grid gap-2 sm:grid-cols-[12rem_1fr_auto]">
<input className="input font-mono" value={k} onChange={e => set(i, 0, e.target.value)} placeholder="NAME" aria-label="Name" />
<input className="input font-mono" value={v} onChange={e => set(i, 1, e.target.value)} placeholder={SUGGESTED.find(x => x.key === k)?.hint || "value"} aria-label={`${k} value`} />
<button type="button" className="btn-secondary" onClick={() => setRows(rows.filter((_, n) => n !== i))} aria-label={`Remove ${k}`}><Trash2 className="h-4 w-4" /></button>
</div>
))}
</div>
<button type="button" className="btn-secondary" onClick={() => setRows([...rows, ["", ""]])}><Plus className="h-4 w-4" /> Add a setting</button>
<button type="button" className="btn-primary" onClick={save}>Save</button>
</div>
</div>
<FormError error={error} />
<ModalButtons busy={busy} onCancel={onClose} />
</form>
</Modal>
);
}
/** Hub 2.17.0: Update agent in a popup; queuing it opens the job. */
function UpdateAgentModal({ current, onClose, queue }: { current: string | null; onClose: () => void; queue: (version: string) => Promise<void> }) {
const [version, setVersion] = useState("");
const { busy, error, run } = useSubmit();
return (
<Modal title="Update agent" onClose={onClose} dirty={version.trim() !== ""}>
<form onSubmit={e => { e.preventDefault(); run(() => queue(version.trim())); }} className="space-y-3">
<p className="text-sm text-slate-600">The agent here is {current || "an unknown version"}. The new version is queued as a job on this server.</p>
<div><label className="label" htmlFor="agent-version">Version</label><input id="agent-version" className="input font-mono" placeholder="agent@1.0.1" value={version} onChange={e => setVersion(e.target.value)} required /></div>
<FormError error={error} />
<ModalButtons busy={busy} onCancel={onClose} label="Update agent" busyLabel="Queuing…" />
</form>
</Modal>
);
}
@@ -73,19 +93,19 @@ export default function ServerPage() {
const jobsPaged = useServerPaged<Job>("/api/admin/jobs", { serverId: String(id) }, r => r.jobs as Job[], 20);
const [range, setRange] = useState<"24h" | "7d" | "90d">("24h");
const [msg, setMsg] = useState<Msg>(null);
const [agentVersion, setAgentVersion] = useState("");
// Hub 2.17.0: the popup that's open.
const [popup, setPopup] = useState<"config" | "agent" | null>(null);
const load = useCallback(() => api<Detail>(`/api/admin/servers/${id}?range=${range}`).then(setD).catch(e => setMsg({ type: "err", text: errorText(e) })), [id, range]);
useEffect(() => { load(); }, [load]);
const job = async (type: string, args: Record<string, unknown> = {}) => {
setMsg(null);
try {
const r = await api<{ job: Job }>(`/api/admin/servers/${id}/jobs`, { method: "POST", body: { type, args } });
setMsg({ type: "ok", text: `Queued: the agent picks it up within 30 seconds.` });
router.push(`/jobs/${r.job.id}`);
} catch (e) { setMsg({ type: "err", text: errorText(e) }); }
/** Queues a job and opens it; throws when the Hub refuses, for a popup to show. */
const queue = async (type: string, args: Record<string, unknown> = {}) => {
const r = await api<{ job: Job }>(`/api/admin/servers/${id}/jobs`, { method: "POST", body: { type, args } });
setMsg({ type: "ok", text: `Queued: the agent picks it up within 30 seconds.` });
router.push(`/jobs/${r.job.id}`);
};
const job = (type: string) => { setMsg(null); queue(type).catch(e => setMsg({ type: "err", text: errorText(e) })); };
const remove = async () => {
if (!d || !window.confirm(`Remove ${d.server.name} from the Hub? Its agent stops getting jobs. (Uninstall the agent on the server too.)`)) return;
try { await api(`/api/admin/servers/${id}`, { method: "DELETE" }); router.push("/servers"); } catch (e) { setMsg({ type: "err", text: errorText(e) }); }
@@ -98,7 +118,7 @@ export default function ServerPage() {
<div>
<PageTitle title={s.name} subtitle={<>{s.mode === "central" ? "Central server" : "Church server"} · {s.hostname} · agent {s.agentVersion} · last heard {ago(s.lastSeenAt)} <StatusPill status={s.busy ? "busy" : s.online ? "online" : "offline"} /></>} />
<ServersNav active="/servers" />
<Message msg={msg} />
<PageMessage msg={msg} onClose={() => setMsg(null)} />
</div>
{!!d.alerts.filter(a => !a.resolvedAt).length && (
@@ -134,12 +154,19 @@ export default function ServerPage() {
<h2 className="font-medium">Actions</h2>
<button className="btn-secondary w-full" onClick={() => job("status")}>Check status and find installs</button>
<p className="text-xs text-slate-500">Asks the agent what&apos;s on the server. A site added there without the Hub (crosscode add, or by hand) is added to Installs and tied to its registration. It also runs by itself once a day when a registered site has no install record here.</p>
<div className="flex gap-2"><input className="input" placeholder="agent@1.0.1" value={agentVersion} onChange={e => setAgentVersion(e.target.value)} aria-label="Agent version" /><button className="btn-secondary" disabled={!agentVersion} onClick={() => job("update-agent", { version: agentVersion })}>Update agent</button></div>
<button className="btn-secondary w-full" onClick={() => setPopup("agent")}><Upload className="h-4 w-4" /> Update agent…</button>
<button className="btn-danger w-full" onClick={remove}><Trash2 className="h-4 w-4" /> Remove server</button>
</div>
</div>
{s.mode === "church" && <ConfigEditor key={JSON.stringify(d.config)} id={s.id} config={d.config} onSaved={m => { setMsg(m); load(); }} />}
{s.mode === "church" && (
<ReadCard title="Configuration" onEdit={() => setPopup("config")}>
<p className="text-sm text-slate-500">The server layer of every install&apos;s .env here. Stored encrypted; sent only inside jobs, sealed to this server.</p>
<Fields>{configKeys(d.config).map(k => <Field key={k} label={k}>{d.config[k] ? <span className="break-all font-mono">{d.config[k]}</span> : null}</Field>)}</Fields>
</ReadCard>
)}
{popup === "config" && <ConfigModal id={s.id} config={d.config} onClose={() => setPopup(null)} onSaved={text => { setPopup(null); setMsg({ type: "ok", text }); load(); }} />}
{popup === "agent" && <UpdateAgentModal current={s.agentVersion} onClose={() => setPopup(null)} queue={version => queue("update-agent", { version })} />}
{d.frp && <FrpConfig serverId={s.id} agentVersion={s.agentVersion} frp={d.frp} onChange={m => { setMsg(m); load(); }} />}