"use client"; import React, { useCallback, useEffect, useState } from "react"; import { KeyRound, RefreshCw, ShieldCheck, ShieldAlert, ArrowUpCircle, Undo2, BellOff, BellRing, Megaphone } 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; urgentUpdate: boolean; updateSnoozed: boolean; releases: HubRelease[]; // `outdated`: a newer version than the one asked for is out (the request can be changed to it). updateRequest: { requestedAt: string; version: string; outdated?: boolean } | null; notices: HubNotice[]; }; export type HubRelease = { version: string; releasedAt: string | null; urgent: boolean; notes: string }; export type HubNotice = { id: string; level: "info" | "maintenance" | "warning"; title: string; message: string; startsAt: string | null; endsAt: string | null; updatedAt: string | null }; /** Release notes from the provider: lines starting with "- " are a list, the rest paragraphs. */ export function ReleaseNotes({ notes }: { notes: string }) { const blocks: ({ list: false; text: string } | { list: true; items: string[] })[] = []; for (const raw of notes.split(/\r?\n/)) { const line = raw.trim(); if (!line) continue; const item = /^[-*•]\s+(.*)$/.exec(line); const last = blocks[blocks.length - 1]; if (item) { if (last?.list) last.items.push(item[1]); else blocks.push({ list: true, items: [item[1]] }); } else blocks.push({ list: false, text: line }); } if (!blocks.length) return

No details given.

; return (
{blocks.map((b, i) => (b.list ? :

{b.text}

))}
); } 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); } }; // Request, withdraw, "Not now" and "remind me again" all return the new status. const updateAction = async (path: string, body: unknown, ok: string) => { setBusy(true); setMsg(null); try { setStatus(await apiFetch(path, { method: "POST", authToken: token, body })); setMsg({ type: "ok", text: ok }); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "The Hub could not be reached." }); } finally { setBusy(false); } }; const requestUpdate = () => updateAction("/api/hub/update-request", undefined, "Update requested. Your provider has been told and will update the site."); // Same request again: the Hub moves the open request on to the latest version. const changeRequest = () => updateAction("/api/hub/update-request", undefined, `Request changed to version ${status?.latestVersion}. Your provider has been told.`); const withdrawRequest = () => { if (!confirm("Withdraw the update request? Your provider is told not to update the site for now.")) return; updateAction("/api/hub/update-request/cancel", undefined, "Update request withdrawn. Your provider has been told."); }; const snooze = (on: boolean) => updateAction("/api/hub/update-snooze", { snooze: on }, on ? "Got it — no more reminders until the next version comes out." : "Update reminders are back on."); if (!status) return msg ?

{msg.text}

:

Loading…

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

Licence

This site checks in with the Cross 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 Cross 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 Cross Events {status.version}.{status.latestVersion && !status.updateAvailable && " It's up to date."}

{status.updateAvailable && (
{status.urgentUpdate && (

This update fixes a security problem or a serious bug. Please ask for it soon.

)} {status.updateRequest ? (

You asked for version {status.updateRequest.version} on {fmt(status.updateRequest.requestedAt)} — your provider will install it.

{status.connected && ( )}
{status.updateRequest.outdated && (

Version {status.latestVersion} has come out since. Ask for it instead, and your provider installs that one.

{status.connected && ( )}
)}
) : (

Version {status.latestVersion} is available.{status.updateSnoozed && You chose "Not now", so there are no reminders until the next version.}

{status.connected && ( )} {!status.urgentUpdate && (status.updateSnoozed ? ( ) : ( ))}
)} {status.releases.length > 0 && (

What's new

{status.releases.map(r => (
{r.version} {r.urgent && Security or urgent fix} {r.releasedAt && {new Date(r.releasedAt).toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric" })}}
))}
)}
)}
{status.notices.length > 0 && (
From your provider
{status.notices.map(n => (

{n.title}

{n.message}

{n.endsAt &&

Until {fmt(n.endsAt)}

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