"use client"; import React, { useCallback, useEffect, useMemo, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { Link2, Plus } from "lucide-react"; import { api, errorText } from "@/lib/api"; import { ago } from "@/lib/format"; import { matches, usePaged } from "@/lib/paging"; import { PRODUCT_NAMES } from "@/lib/products"; import { Loading, Message, PageTitle, Pager, SearchInput } from "@/components/ui"; import { ServersNav, StatusPill, pct, shownStatus, type Deployment, type ServerSummary } from "@/components/servers"; type Msg = { type: "ok" | "err"; text: string } | null; type Customer = { id: string; name: string; domain?: string | null }; type InstallRow = { id: string; name: string; url: string; customer: { id: string; name: string } | null; version: string | null }; const emptyNew = { product: "events", customerId: "", serverId: "", shortName: "", domain: "", accountsDomain: "", timezone: "", adminName: "", adminEmail: "", version: "" }; const emptyLink = { installId: "", serverId: "", shortName: "", webPort: "", apiPort: "" }; /** Installs on servers: New install (§7) and linking existing installs (§14). */ export default function InstallsPage() { const router = useRouter(); const [list, setList] = useState(null); // Hub 2.6.0: which product each install is, a filter for it, a search, and a page at a time. const [product, setProduct] = useState(""); const [query, setQuery] = useState(""); const shown = useMemo(() => (list || []).filter(d => (!product || d.product === product) && matches(query, d.customer?.name, d.shortName, d.domain, d.server?.name)), [list, product, query]); const paged = usePaged(shown, `${product}|${query}`); const [srv, setSrv] = useState([]); const [customers, setCustomers] = useState([]); const [installs, setInstalls] = useState([]); const [mode, setMode] = useState<"none" | "new" | "link">("none"); const [form, setForm] = useState(emptyNew); const [link, setLink] = useState(emptyLink); const [msg, setMsg] = useState(null); const [busy, setBusy] = useState(false); const load = useCallback(() => { api<{ deployments: Deployment[] }>("/api/admin/deployments").then(r => setList(r.deployments)).catch(e => setMsg({ type: "err", text: errorText(e) })); api<{ servers: ServerSummary[] }>("/api/admin/servers").then(r => setSrv(r.servers.filter(s => s.mode === "church"))).catch(() => {}); }, []); useEffect(() => { load(); }, [load]); useEffect(() => { if (mode === "new" && !customers.length) api("/api/admin/customers").then(setCustomers).catch(() => {}); if (mode === "link" && !installs.length) api("/api/admin/installs").then(setInstalls).catch(() => {}); }, [mode, customers.length, installs.length]); const submitNew = async (e: React.FormEvent) => { e.preventDefault(); setBusy(true); setMsg(null); try { const r = await api<{ deployment: Deployment; warnings: string[]; checkingPorts?: boolean; alsoInstalling?: { product: string; domain: string; version: string } }>("/api/admin/deployments", { method: "POST", body: { ...form, version: form.version || undefined, accountsDomain: form.accountsDomain || undefined } }); // Hub 2.5.0: Cross Events 3.x for a church without Accounts installs Accounts first. const notes = [ // Hub 2.11.0: the ports are checked on the central server and this one first. ...(r.checkingPorts ? ["The ports are being checked on the central server and this server first; the install starts on ports free on both (about a minute)."] : []), ...(r.alsoInstalling ? [`Accounts ${r.alsoInstalling.version} is installed first at ${r.alsoInstalling.domain}; Cross Events follows when it has finished.`] : []), ...r.warnings, ]; router.push(`/servers/installs/${r.deployment.id}${notes.length ? `?warn=${encodeURIComponent(notes.join(" "))}` : ""}`); } catch (err) { setMsg({ type: "err", text: errorText(err) }); } setBusy(false); }; const submitLink = async (e: React.FormEvent) => { e.preventDefault(); setBusy(true); setMsg(null); try { const r = await api<{ deployment: Deployment }>("/api/admin/deployments/link", { method: "POST", body: link }); router.push(`/servers/installs/${r.deployment.id}`); } catch (err) { setMsg({ type: "err", text: errorText(err) }); } setBusy(false); }; const f = (k: keyof typeof emptyNew) => ({ value: form[k], onChange: (e: React.ChangeEvent) => setForm({ ...form, [k]: e.target.value }) }); const l = (k: keyof typeof emptyLink) => ({ value: link[k], onChange: (e: React.ChangeEvent) => setLink({ ...link, [k]: e.target.value }) }); return (
} /> {mode === "new" && (

New install

{form.product === "accounts" ? "Install Accounts before the Website, the App or Cross Events 3.0. Use the church's short name to add it next to its Events on the same server." : "A church that already has a product here keeps its short name: the new one is added next to it on the same server."}

Folders, database and tunnels: a-z, 0-9 and -.

{form.product === "events" && (

Cross Events 3 signs in through the church's Accounts: a church without it gets it installed first, here.

)}

Gets the welcome email and update notices.

Server
{srv.map(s => { const low = (s.ram?.availableBytes ?? 0) < 1.5 * 1024 ** 3; return ( ); })}

The Hub makes the licence, picks two ports, and the agent installs within a minute. You get the report and the FastPanel steps by email.

)} {mode === "link" && (

Link an install that's already on a server

For sites installed by hand or moved with crosscode adopt. Nothing on the server changes; the Hub records the server, folder name and ports (see the server's church.env and frpc.ini).

Just the name, not a path: its folder is /var/www/sites/<name>. A site still in an old folder (/var/www/hope-events) gets that name when you move it onto the monorepo.

)} {!list ? : (
{paged.rows.map(d => ( ))}
ChurchProductDomainServerVersionLast heartbeatStatus
{d.customer?.name} ({d.shortName}){d.isDemo && demo}{PRODUCT_NAMES[d.product as keyof typeof PRODUCT_NAMES] || d.product}{d.domain}{d.server?.name}{d.install?.version || d.version || "–"}{d.installId ? ago(d.install?.lastHeartbeatAt) : not registered}
{!list.length ?

No installs on servers yet.

: !shown.length &&

No installs match.

}
)} ); }