"use client"; import React, { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { ArrowUp, ArrowDown, Trash2, Plus, RotateCcw, Eye, PencilLine, Sparkles } from "lucide-react"; import { apiFetch } from "@/lib/api"; import { errorMessage } from "@/lib/utils"; import { PrivacyNoticeSection, type NoticeSection } from "@/components/shared/PrivacyNoticeSection"; type Version = { version: number; publishedAt: string; changeSummary: string | null; requiresAcceptance: boolean; publishedBy: string | null; acceptances: number }; // Changes to the built-in text since this site last reviewed them (only once it has published its own). type BuiltInUpdate = { revision: string; summary: string[]; sections: string[] }; type Draft = { sections: NoticeSection[]; rendered: NoticeSection[]; isDraft: boolean; savedAt: string | null; versions: Version[]; builtIn: NoticeSection[]; builtInUpdates?: BuiltInUpdate[] }; type Msg = { type: "ok" | "err"; text: string } | null; const PLACEHOLDER_HELP: [string, string][] = [ ["{org}", "organisation name"], ["{orgEmail}", "organisation email"], ["{website}", "website (Site settings → Legal)"], ["{ioName}", "Information Officer"], ["{ioEmail}", "Information Officer's email"], ["{medicalRetentionDays}", "days before medical details are deleted"], ["{operators}", "on its own line: one bullet per active operator"], ["{retention}", "on its own line: one bullet per retention period"], ]; const fmt = (d: string) => 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"; /** Admin → Privacy → Privacy notice: edit the sections, preview, publish versions. */ export function NoticeEditor({ token }: { token: string }) { const [draft, setDraft] = useState(null); const [sections, setSections] = useState([]); const [dirty, setDirty] = useState(false); const [preview, setPreview] = useState(false); const [busy, setBusy] = useState(false); const [msg, setMsg] = useState(null); const [publishing, setPublishing] = useState(false); const [changeSummary, setChangeSummary] = useState(""); const [requiresAcceptance, setRequiresAcceptance] = useState(false); const load = useCallback(async () => { try { const d = await apiFetch("/api/privacy/notice/draft", { authToken: token }); setDraft(d); setSections(d.sections); setDirty(false); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not load the notice." }); } }, [token]); useEffect(() => { load(); }, [load]); const change = (next: NoticeSection[]) => { setSections(next); setDirty(true); }; const update = (i: number, patch: Partial) => change(sections.map((s, j) => (j === i ? { ...s, ...patch } : s))); const move = (i: number, by: number) => { const next = [...sections]; const [s] = next.splice(i, 1); next.splice(i + by, 0, s); change(next); }; const save = async () => { setBusy(true); setMsg(null); try { const res = await apiFetch<{ sections: NoticeSection[]; rendered: NoticeSection[]; savedAt: string }>("/api/privacy/notice/draft", { method: "PUT", authToken: token, body: { sections } }); setDraft(d => (d ? { ...d, sections: res.sections, rendered: res.rendered, isDraft: true, savedAt: res.savedAt } : d)); setSections(res.sections); setDirty(false); setMsg({ type: "ok", text: "Draft saved. It isn't public until you publish it." }); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not save." }); } finally { setBusy(false); } }; const discard = async () => { if (!confirm("Throw away the draft and go back to the published text?")) return; setBusy(true); try { await apiFetch("/api/privacy/notice/draft", { method: "DELETE", authToken: token }); await load(); setMsg({ type: "ok", text: "Draft discarded." }); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not discard." }); } finally { setBusy(false); } }; const publish = async (e: React.FormEvent) => { e.preventDefault(); setBusy(true); setMsg(null); try { const res = await apiFetch<{ version: number }>("/api/privacy/notice/publish", { method: "POST", authToken: token, body: { sections, changeSummary, requiresAcceptance } }); setPublishing(false); setChangeSummary(""); setRequiresAcceptance(false); await load(); setMsg({ type: "ok", text: `Version ${res.version} is published.${requiresAcceptance ? " Everyone will be asked to accept it after they next log in." : ""}` }); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not publish." }); } finally { setBusy(false); } }; // The built-in text changed since this site last looked: hide the list once the admin has been through it. const markReviewed = async () => { setBusy(true); try { await apiFetch("/api/privacy/notice/builtin-reviewed", { method: "POST", authToken: token }); setDraft(d => (d ? { ...d, builtInUpdates: [] } : d)); setMsg({ type: "ok", text: "Done. Remember to save the draft and publish if you took any new text." }); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not save." }); } finally { setBusy(false); } }; if (!draft) return msg ?

{msg.text}

:

Loading…

; const builtInById = new Map(draft.builtIn.map(s => [s.id, s])); const current = draft.versions[0]; const updates = draft.builtInUpdates || []; const updatedIds = new Set(updates.flatMap(u => u.sections)); const differs = (s: NoticeSection) => { const b = builtInById.get(s.id); return !!b && (b.body !== s.body || b.title !== s.title); }; // Built-in sections this notice doesn't have (removed, or new in the built-in text). const missing = draft.builtIn.filter(b => !sections.some(s => s.id === b.id)); const pendingIds = sections.filter(s => updatedIds.has(s.id) && differs(s)).map(s => s.id); /** Puts a built-in section back where it sits in the built-in text. */ const addBuiltIn = (b: NoticeSection) => { const order = draft.builtIn.map(x => x.id); const after = order.slice(0, order.indexOf(b.id)).reverse().find(id => sections.some(s => s.id === id)); const at = after ? sections.findIndex(s => s.id === after) + 1 : 0; change([...sections.slice(0, at), { ...b }, ...sections.slice(at)]); }; const useAllNew = () => { if (!confirm("Replace every changed section with the new built-in text? Your own wording in those sections is replaced (other sections stay as they are). Nothing is public until you publish.")) return; const next = sections.map(s => { const b = updatedIds.has(s.id) ? builtInById.get(s.id) : undefined; return b ? { ...b } : s; }); change(next); }; return (
{current ? ( <>Published: version {current.version} ({fmt(current.publishedAt)}). ) : ( <>Nothing published yet — the site shows the built-in notice. )} {draft.isDraft && You have an unpublished draft{draft.savedAt ? ` (saved ${fmt(draft.savedAt)})` : ""}. } View the public page
How to write it

One paragraph per line; start a line with "• " for a bullet. These are filled in when the notice is shown, so they stay up to date:

    {PLACEHOLDER_HELP.map(([p, d]) =>
  • {p} — {d}
  • )}

This is tooling to help you comply, not legal advice — your Information Officer should check the wording.

{draft.isDraft && ( )} {dirty && Unsaved changes}
{updates.length > 0 && (
The built-in privacy notice has been updated

Your published notice keeps your own text, so the new wording from Cross Events {updates.map(u => u.revision).join(", ")} isn't in it yet:

    {updates.flatMap(u => u.summary).map((line, i) =>
  • {line}
  • )}

{pendingIds.length > 0 ? <>Sections with new text are marked below. Use the new text where you want it (it replaces your wording in that section), then save the draft and publish. : <>Your draft already has the new text in every changed section. Save the draft and publish to make it live.} {" "}Tick "Significant change" when publishing if people should accept it again.

{pendingIds.length > 0 && }
)} {msg &&
{msg.text}
} {publishing && (
Publish version {(current?.version ?? 0) + 1}