"use client"; import React, { useCallback, useEffect, useState } from "react"; import { KeyRound, RefreshCw, ShieldCheck, ShieldAlert, ArrowUpCircle } from "lucide-react"; import { apiFetch } from "@/lib/api"; import { useAuth } from "@/hooks/useAuth"; import { errorMessage } from "@/lib/utils"; export type HubStatus = { state: "active" | "grace" | "suspended" | "revoked" | "unlicensed"; readOnly: boolean; daysLeft: number | null; endsAt: string | null; graceEndsAt: string | null; licence: { customer: string; plan: string; status: string; licenceExpiresAt: string | null; graceDays: number; issuedAt: string; validUntil: string } | null; connected: boolean; keyConfigured: boolean; hubUrl: string; lastHeartbeatAt: string | null; lastError: string | null; version: string; latestVersion: string | null; updateAvailable: boolean; }; const STATE: Record = { active: { label: "Active", tone: "bg-green-50 text-green-800 border-green-200", text: "Everything works." }, grace: { label: "Needs renewing", tone: "bg-amber-50 text-amber-900 border-amber-200", text: "Everything still works for now — renew before the grace period ends." }, suspended: { label: "Suspended", tone: "bg-red-50 text-red-800 border-red-200", text: "Read-only: events can't be created or edited, and no new registrations or payments. Tickets still scan and all data can be exported." }, revoked: { label: "Revoked", tone: "bg-red-50 text-red-800 border-red-200", text: "Read-only: events can't be created or edited, and no new registrations or payments. Tickets still scan and all data can be exported." }, unlicensed: { label: "No licence", tone: "bg-red-50 text-red-800 border-red-200", text: "Read-only until you connect a licence key: events can't be created or edited, and no new registrations or payments." }, }; const fmt = (d: string | null) => (d ? new Date(d).toLocaleString(undefined, { day: "numeric", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit" }) : "—"); const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500"; /** Enter a licence key and connect. Used in Site settings → Licence and the /setup wizard. */ export function ConnectForm({ token, onConnected, compact = false }: { token: string | null; onConnected: (s: HubStatus) => void; compact?: boolean }) { const [key, setKey] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const submit = async (e: React.FormEvent) => { e.preventDefault(); setBusy(true); setError(null); try { onConnected(await apiFetch("/api/hub/connect", { method: "POST", authToken: token, body: { licenceKey: key } })); setKey(""); } catch (err) { setError(errorMessage(err) || "Could not connect."); } finally { setBusy(false); } }; return (
setKey(e.target.value)} required autoComplete="off" />
{error &&

{error}

}
); } /** Site settings → Licence: licence state, the Hub connection, and this site's version. */ export function LicenceTab({ active }: { active: boolean }) { const { token } = useAuth(); const [status, setStatus] = useState(null); const [busy, setBusy] = useState(false); const [msg, setMsg] = useState<{ type: "ok" | "err"; text: string } | null>(null); const load = useCallback(async () => { if (!token) return; try { setStatus(await apiFetch("/api/hub/status", { authToken: token })); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not load the licence." }); } }, [token]); useEffect(() => { if (active) load(); }, [active, load]); const checkNow = async () => { setBusy(true); setMsg(null); try { setStatus(await apiFetch("/api/hub/heartbeat", { method: "POST", authToken: token })); setMsg({ type: "ok", text: "Checked with the Hub." }); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "The Hub could not be reached." }); load(); } finally { setBusy(false); } }; if (!status) return msg ?

{msg.text}

:

Loading…

; const st = STATE[status.state]; return (

Licence

This site checks in with the Hope Events Hub 30 seconds after starting and every 15 minutes after that: version, health and totals only — never anyone's personal information.

{status.state === "active" ? : } {st.label} {status.state === "grace" && status.daysLeft != null && — {status.daysLeft} day{status.daysLeft === 1 ? "" : "s"} left}

{st.text}

{!status.keyConfigured && (

This version has no Hub key built in yet, so it can't check licences. Update Hope Events, or set HUB_PUBLIC_KEY on the server for testing.

)} {status.licence && (
Licensed to
{status.licence.customer}
Plan
{status.licence.plan}
Licence ends
{status.licence.licenceExpiresAt ? fmt(status.licence.licenceExpiresAt) : "No end date"}
Grace period
{status.licence.graceDays} days
)}
Hub connection
Hub
{status.hubUrl}
Last check-in
{fmt(status.lastHeartbeatAt)}
{status.lastError &&

Last problem: {status.lastError}

} {status.connected && ( )}

{status.connected ? "New licence key, or moved to a new address? Connect again:" : "Enter the licence key you received:"}

{ setStatus(s); setMsg({ type: "ok", text: "Connected to the Hub." }); }} />
Version

This site runs Hope Events {status.version}.{status.latestVersion && !status.updateAvailable && " It's up to date."}

{status.updateAvailable && (

Version {status.latestVersion} is available. On the server: bash scripts/deploy.sh v{status.latestVersion}

)}
{msg &&
{msg.text}
}
); }