"use client"; import React, { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { useParams } from "next/navigation"; import { ArrowLeft, Copy, KeyRound } from "lucide-react"; import { api, errorText } from "@/lib/api"; import { ago, fmtDate } from "@/lib/format"; import { PLANS } from "@/lib/tiers"; import type { Customer, Licence, LicenceStatus } from "@/lib/types"; import { Badge, Loading, Message, PageTitle } from "@/components/ui"; type Msg = { type: "ok" | "err"; text: string } | null; const dateInput = (d: string | null) => (d ? d.slice(0, 10) : ""); /** One licence: its key, and the settings that go into the next token the install receives. */ function LicenceCard({ licence, onSaved, setMsg }: { licence: Licence; onSaved: () => void; setMsg: (m: Msg) => void }) { const [form, setForm] = useState({ status: licence.status, plan: licence.plan, expiresAt: dateInput(licence.expiresAt), graceDays: String(licence.graceDays), aiRand: String(licence.aiMonthlyCapCents / 100), aiMarkup: licence.aiMarkupPercent == null ? "" : String(licence.aiMarkupPercent), notes: licence.notes || "", }); const [busy, setBusy] = useState(false); const save = async () => { if (form.status !== licence.status && form.status !== "active" && !confirm(`${form.status === "revoked" ? "Revoke" : "Suspend"} this licence? The site becomes read-only after its next heartbeat (tickets still scan, data can still be exported).`)) return; setBusy(true); try { await api(`/api/admin/licences/${licence.id}`, { method: "PUT", body: { status: form.status, plan: form.plan, // End of the chosen day, South African time. expiresAt: form.expiresAt ? `${form.expiresAt}T23:59:59+02:00` : null, graceDays: form.graceDays, aiMonthlyCapCents: Math.round((parseFloat(form.aiRand) || 0) * 100), aiMarkupPercent: form.aiMarkup.trim() === "" ? null : form.aiMarkup, notes: form.notes, }, }); setMsg({ type: "ok", text: "Licence saved. The site picks it up with its next heartbeat (within 15 minutes)." }); onSaved(); } catch (e) { setMsg({ type: "err", text: errorText(e) }); } finally { setBusy(false); } }; const release = async () => { if (!confirm("Release this licence from its site? The site's heartbeats are refused from now on, and the key can be registered again from a new address.")) return; try { await api(`/api/admin/licences/${licence.id}/release`, { method: "POST" }); setMsg({ type: "ok", text: "Released." }); onSaved(); } catch (e) { setMsg({ type: "err", text: errorText(e) }); } }; return (
{licence.key}
Shown on the church's Licence page. Founding discount: note it below.
{form.expiresAt ? "" : "No end date"}
0 = off (no AI buttons). Counted in charged rand.
Blank uses the default (Settings → AI).