- Install page (/servers/installs/[id]): header with status, Schedule update and a ⋯ menu (move onto the monorepo, demo site, uninstall, remove from the Hub, remove the record); tiles for site/registration, version, server, ports and last heartbeat. - Tabs: Jobs, Settings (.env), Exports; the tab in the URL (?tab=settings). - Every action is a popup (Update, Adopt, Demo, Uninstall, Remove) using the 2.14.0 Modal: asks before discarding, error by its button. Confirmations unchanged. - EnvPanel: read view in the tab, each file edited in a popup. - Banners for not registered, stale (no heartbeat for a day) and a planned uninstall. - Shared PageMessage (also on the customer page); ModalButtons gets danger/disabled. - Logic split into components/install/ (useSubmit, post with LIVE_EVENT re-confirm, uninstallPlan). Frontend only: no migration, no API changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
155 lines
8.9 KiB
TypeScript
155 lines
8.9 KiB
TypeScript
"use client";
|
||
|
||
import React, { useCallback, useEffect, useState } from "react";
|
||
import Link from "next/link";
|
||
import { useParams, useRouter, useSearchParams } from "next/navigation";
|
||
import { api, errorText } from "@/lib/api";
|
||
import { fmtDate } from "@/lib/format";
|
||
import { useServerPaged } from "@/lib/paging";
|
||
import type { Release } from "@/lib/types";
|
||
import { Loading, Message, PageMessage, Tabs } from "@/components/ui";
|
||
import { ServersNav, type Job } from "@/components/servers";
|
||
import { EnvPanel } from "@/components/EnvPanel";
|
||
import { InstallHeader, type InstallAction } from "@/components/install/InstallHeader";
|
||
import { ExportsCard, JobsCard } from "@/components/install/JobsCard";
|
||
import { UpdateModal } from "@/components/install/UpdateModal";
|
||
import { AdoptModal } from "@/components/install/AdoptModal";
|
||
import { DemoModal } from "@/components/install/DemoModal";
|
||
import { UninstallModal } from "@/components/install/UninstallModal";
|
||
import { RemoveModal } from "@/components/install/RemoveModal";
|
||
import { post, productName, running, uninstallPlan, withWarnings, type Detail, type Done, type Msg } from "@/components/install/shared";
|
||
|
||
type Tab = "jobs" | "settings" | "exports";
|
||
type Popup = Exclude<InstallAction, "undemo"> | null;
|
||
|
||
/**
|
||
* One install on a server (§7–9). Hub 2.15.0: a header with the status and tiles, then Jobs,
|
||
* Settings (.env) and Exports as tabs; every update, move, demo, uninstall and removal is a popup.
|
||
*/
|
||
export default function DeploymentPage() {
|
||
const { id } = useParams<{ id: string }>();
|
||
const router = useRouter();
|
||
const warn = useSearchParams().get("warn");
|
||
const [d, setD] = useState<Detail | null>(null);
|
||
const [msg, setMsg] = useState<Msg>(warn ? { type: "err", text: warn } : null);
|
||
const [problems, setProblems] = useState<string[]>([]);
|
||
const [releases, setReleases] = useState<Release[]>([]);
|
||
const [tab, setTab] = useState<Tab>("jobs");
|
||
const [popup, setPopup] = useState<Popup>(null);
|
||
|
||
// Hub 2.6.0: every job of this install, a page at a time.
|
||
const jobsPaged = useServerPaged<Job>("/api/admin/jobs", { deploymentId: String(id) }, r => r.jobs as Job[], 20);
|
||
const load = useCallback(() => {
|
||
jobsPaged.reload();
|
||
return api<Detail>(`/api/admin/deployments/${id}`).then(setD).catch(e => setMsg({ type: "err", text: errorText(e) }));
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps -- reload is a fresh function each render
|
||
}, [id]);
|
||
useEffect(() => { load(); }, [load]);
|
||
useEffect(() => { api<Release[]>("/api/admin/releases").then(setReleases).catch(() => {}); }, []);
|
||
|
||
// The tab is in the address (?tab=settings), so a reload or a shared link opens it.
|
||
useEffect(() => {
|
||
const q = new URLSearchParams(window.location.search).get("tab");
|
||
if (q === "settings" || q === "exports") setTab(q);
|
||
}, []);
|
||
const openTab = (t: Tab) => {
|
||
setTab(t);
|
||
const q = new URLSearchParams(window.location.search);
|
||
if (t === "jobs") q.delete("tab"); else q.set("tab", t);
|
||
const s = q.toString();
|
||
window.history.replaceState(null, "", s ? `?${s}` : window.location.pathname);
|
||
};
|
||
|
||
/** Every popup and card reports here: close the popup, show the message, reload. */
|
||
const done: Done = m => { setPopup(null); setMsg(m); load(); };
|
||
const close = () => setPopup(null);
|
||
|
||
// 2.7.1: the page's own buttons (proxy done, cancel a job, not a demo) are off from a click until the Hub answers.
|
||
const [busy, setBusy] = useState(false);
|
||
const act = async (fn: () => Promise<Msg>) => {
|
||
if (busy) return;
|
||
setBusy(true);
|
||
try { done(await fn()); } catch (e) { setMsg({ type: "err", text: errorText(e) }); } finally { setBusy(false); }
|
||
};
|
||
|
||
if (!d) return <div><ServersNav active="/servers/installs" />{msg ? <Message msg={msg} /> : <Loading />}</div>;
|
||
const x = d.deployment;
|
||
const plan = uninstallPlan(d);
|
||
const hasEnv = ["events", "accounts"].includes(x.product) && running(x);
|
||
const tabs: { key: Tab; label: string }[] = [
|
||
{ key: "jobs", label: "Jobs" },
|
||
...(hasEnv ? [{ key: "settings" as const, label: "Settings (.env)" }] : []),
|
||
...(x.exports.length ? [{ key: "exports" as const, label: `Exports (${x.exports.length})` }] : []),
|
||
];
|
||
const shown = tabs.some(t => t.key === tab) ? tab : "jobs";
|
||
|
||
const proxyDone = () => act(async () => {
|
||
const r = await api<{ live: boolean; problems: string[] }>(`/api/admin/deployments/${id}/proxy-done`, { method: "POST" });
|
||
setProblems(r.problems);
|
||
return r.live ? { type: "ok", text: "Live. The site admin got the welcome email." } : { type: "err", text: "The site isn't answering as expected yet:" };
|
||
});
|
||
const cancelJob = (j: Job) => act(async () => {
|
||
const r = await post(`/api/admin/jobs/${j.id}/cancel`);
|
||
return { type: "ok", text: withWarnings("Cancelled; the church was told.", r) };
|
||
});
|
||
const onAction = (a: InstallAction) => {
|
||
if (a === "undemo") {
|
||
if (!window.confirm(`${x.shortName} won't be a demo site any more: its data can't be replaced from the Hub then.`)) return;
|
||
act(async () => { await post(`/api/admin/deployments/${x.id}/demo`, { isDemo: false }); return { type: "ok", text: "No longer a demo site." }; });
|
||
} else setPopup(a);
|
||
};
|
||
|
||
return (
|
||
<>
|
||
<InstallHeader d={d} onAction={onAction} />
|
||
<ServersNav active="/servers/installs" />
|
||
|
||
<PageMessage msg={msg} onClose={() => { setMsg(null); setProblems([]); }} />
|
||
{!!problems.length && <ul className="-mt-2 mb-4 list-disc pl-6 text-sm text-red-800">{problems.map(p => <li key={p}>{p}</li>)}</ul>}
|
||
|
||
<div className="mb-6 space-y-3 empty:hidden">
|
||
{/* Hub 2.6.0: the site registers with the Hub itself; its banners, licence and heartbeats need it. */}
|
||
{!x.install && running(x) && (
|
||
<p className="rounded-lg border border-amber-200 bg-amber-50 p-3 text-sm text-amber-900">The site hasn't registered with the Hub yet. It registers by itself with its licence key when it starts, and is tied to this record at its first heartbeat; if that doesn't happen, check its HUB_URL and HUB_LICENCE_KEY.</p>
|
||
)}
|
||
{/* Hub 2.12.0: gone from its server without the Hub (uninstalled by hand, server rebuilt …)? */}
|
||
{d.stale.stale && (
|
||
<div className="flex flex-wrap items-center justify-between gap-3 rounded-lg border border-red-200 bg-red-50 p-3 text-sm text-red-900">
|
||
<span>No heartbeat since {d.stale.since ? fmtDate(d.stale.since) : "it was made"}. If the site is gone from {x.server.name}, remove it from the Hub; if it's still there, uninstall it.</span>
|
||
<button type="button" className="btn-secondary px-2.5 py-1.5" onClick={() => setPopup("forget")}>Remove from the Hub…</button>
|
||
</div>
|
||
)}
|
||
{plan.planned && (
|
||
<p className="rounded-lg border border-amber-200 bg-amber-50 p-3 text-sm text-amber-900">
|
||
Uninstall planned for {fmtDate(plan.planned.notBefore)}{plan.planned.products ? (plan.planned.products.length > 1 ? `, with ${plan.planned.products.filter(p => p !== x.product).map(productName).join(" and ")}` : "") : ": the whole church"}.{" "}
|
||
<Link className="underline" href={`/jobs/${plan.planned.jobId}`}>See the job</Link> to cancel it.
|
||
</p>
|
||
)}
|
||
{(x.status === "waiting_for_proxy" || x.status === "installing") && (
|
||
<section className="card space-y-3">
|
||
<h2 className="font-semibold">Set up the site in FastPanel</h2>
|
||
<pre className="overflow-x-auto whitespace-pre-wrap rounded-lg bg-slate-50 p-3 text-xs">{d.fastPanel}</pre>
|
||
<div className="flex flex-wrap items-center gap-3">
|
||
<button type="button" className="btn-primary" disabled={x.status !== "waiting_for_proxy" || busy} aria-busy={busy} onClick={proxyDone}>{busy ? "Checking…" : "Proxy done"}</button>
|
||
{x.status === "installing" && <p className="text-xs text-slate-500">Waiting for the install to finish (see its job under Jobs).</p>}
|
||
</div>
|
||
</section>
|
||
)}
|
||
</div>
|
||
|
||
<Tabs tabs={tabs} active={shown} onChange={openTab} />
|
||
<div role="tabpanel" id={`panel-${shown}`} aria-labelledby={`tab-${shown}`}>
|
||
{shown === "jobs" && <JobsCard paged={jobsPaged} fallback={x.jobs} busy={busy} onCancel={cancelJob} />}
|
||
{shown === "settings" && <EnvPanel deploymentId={x.id} product={x.product} onMessage={setMsg} />}
|
||
{shown === "exports" && <ExportsCard exports={x.exports} />}
|
||
</div>
|
||
|
||
{popup === "update" && <UpdateModal x={x} releases={releases} onClose={close} onDone={done} />}
|
||
{popup === "adopt" && <AdoptModal x={x} onClose={close} onDone={done} />}
|
||
{popup === "demo" && <DemoModal x={x} onClose={close} onDone={done} />}
|
||
{popup === "uninstall" && <UninstallModal x={x} plan={plan} onClose={close} onDone={done} />}
|
||
{(popup === "record" || popup === "forget") && <RemoveModal kind={popup} x={x} stale={d.stale} onClose={close} onRemoved={() => router.push("/servers/installs")} />}
|
||
</>
|
||
);
|
||
}
|