"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 | 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(null); const [msg, setMsg] = useState(warn ? { type: "err", text: warn } : null); const [problems, setProblems] = useState([]); const [releases, setReleases] = useState([]); const [tab, setTab] = useState("jobs"); const [popup, setPopup] = useState(null); // Hub 2.6.0: every job of this install, a page at a time. const jobsPaged = useServerPaged("/api/admin/jobs", { deploymentId: String(id) }, r => r.jobs as Job[], 20); const load = useCallback(() => { jobsPaged.reload(); return api(`/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("/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) => { if (busy) return; setBusy(true); try { done(await fn()); } catch (e) { setMsg({ type: "err", text: errorText(e) }); } finally { setBusy(false); } }; if (!d) return
{msg ? : }
; 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 ( <> { setMsg(null); setProblems([]); }} /> {!!problems.length &&
    {problems.map(p =>
  • {p}
  • )}
}
{/* Hub 2.6.0: the site registers with the Hub itself; its banners, licence and heartbeats need it. */} {!x.install && running(x) && (

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.

)} {/* Hub 2.12.0: gone from its server without the Hub (uninstalled by hand, server rebuilt …)? */} {d.stale.stale && (
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.
)} {plan.planned && (

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"}.{" "} See the job to cancel it.

)} {(x.status === "waiting_for_proxy" || x.status === "installing") && (

Set up the site in FastPanel

{d.fastPanel}
{x.status === "installing" &&

Waiting for the install to finish (see its job under Jobs).

}
)}
{shown === "jobs" && } {shown === "settings" && } {shown === "exports" && }
{popup === "update" && } {popup === "adopt" && } {popup === "demo" && } {popup === "uninstall" && } {(popup === "record" || popup === "forget") && router.push("/servers/installs")} />} ); }