"use client"; import React, { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { ArrowUp, ArrowDown, Trash2, Plus, RotateCcw, Eye, PencilLine } 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 }; type Draft = { sections: NoticeSection[]; rendered: NoticeSection[]; isDraft: boolean; savedAt: string | null; versions: Version[]; builtIn: NoticeSection[] }; 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); } }; if (!draft) return msg ?

{msg.text}

:

Loading…

; const builtInById = new Map(draft.builtIn.map(s => [s.id, s])); const current = draft.versions[0]; 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}
{msg &&
{msg.text}
} {publishing && (
Publish version {(current?.version ?? 0) + 1}