Archived
The built-in notice now covers volunteer rosters, event series, season passes, webhooks, how people agree to it, and the provider-notices storage item. Sites with their own published version see what changed in the editor (sections marked, use new text per section or all, add missing sections, "Done - hide this"). Account activation asks for agreement to the terms and notice; anyone who never accepted is asked once after login. The cookie button shows above any cookie section. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
306 lines
18 KiB
TypeScript
306 lines
18 KiB
TypeScript
"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<Draft | null>(null);
|
|
const [sections, setSections] = useState<NoticeSection[]>([]);
|
|
const [dirty, setDirty] = useState(false);
|
|
const [preview, setPreview] = useState(false);
|
|
const [busy, setBusy] = useState(false);
|
|
const [msg, setMsg] = useState<Msg>(null);
|
|
const [publishing, setPublishing] = useState(false);
|
|
const [changeSummary, setChangeSummary] = useState("");
|
|
const [requiresAcceptance, setRequiresAcceptance] = useState(false);
|
|
|
|
const load = useCallback(async () => {
|
|
try {
|
|
const d = await apiFetch<Draft>("/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<NoticeSection>) => 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 ? <p className="text-sm text-red-700">{msg.text}</p> : <p className="text-sm text-gray-400">Loading…</p>;
|
|
|
|
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 (
|
|
<div className="space-y-5">
|
|
<div className="text-sm text-gray-600">
|
|
{current ? (
|
|
<>Published: <strong>version {current.version}</strong> ({fmt(current.publishedAt)}). </>
|
|
) : (
|
|
<>Nothing published yet — the site shows the built-in notice. </>
|
|
)}
|
|
{draft.isDraft && <span className="text-amber-700">You have an unpublished draft{draft.savedAt ? ` (saved ${fmt(draft.savedAt)})` : ""}. </span>}
|
|
<Link href="/legal/privacy" target="_blank" className="text-brand-600 hover:underline">View the public page</Link>
|
|
</div>
|
|
|
|
<details className="text-xs text-gray-500 border rounded-lg p-3 bg-gray-50">
|
|
<summary className="cursor-pointer font-medium text-gray-700">How to write it</summary>
|
|
<p className="mt-2">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:</p>
|
|
<ul className="mt-1 grid sm:grid-cols-2 gap-x-4">
|
|
{PLACEHOLDER_HELP.map(([p, d]) => <li key={p}><code>{p}</code> — {d}</li>)}
|
|
</ul>
|
|
<p className="mt-2">This is tooling to help you comply, not legal advice — your Information Officer should check the wording.</p>
|
|
</details>
|
|
|
|
<div className="flex flex-wrap gap-2">
|
|
<button onClick={() => setPreview(p => !p)} className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg border bg-white text-sm hover:bg-gray-50">
|
|
{preview ? <><PencilLine className="w-4 h-4" /> Edit</> : <><Eye className="w-4 h-4" /> Preview</>}
|
|
</button>
|
|
<button onClick={save} disabled={busy || !dirty} className="px-3 py-1.5 rounded-lg bg-white border border-brand-600 text-brand-700 text-sm font-medium hover:bg-brand-50 disabled:opacity-50">
|
|
Save draft
|
|
</button>
|
|
<button onClick={() => setPublishing(true)} disabled={busy} className="px-3 py-1.5 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700 disabled:opacity-50">
|
|
Publish…
|
|
</button>
|
|
{draft.isDraft && (
|
|
<button onClick={discard} disabled={busy} className="px-3 py-1.5 rounded-lg text-sm text-gray-600 hover:bg-gray-100">Discard draft</button>
|
|
)}
|
|
{dirty && <span className="self-center text-xs text-amber-700">Unsaved changes</span>}
|
|
</div>
|
|
|
|
{updates.length > 0 && (
|
|
<div className="border border-amber-200 bg-amber-50 rounded-xl p-4 text-sm text-amber-900 space-y-2" data-testid="builtin-updates">
|
|
<div className="flex items-center gap-2 font-medium"><Sparkles className="w-4 h-4" /> The built-in privacy notice has been updated</div>
|
|
<p>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:</p>
|
|
<ul className="list-disc pl-5">{updates.flatMap(u => u.summary).map((line, i) => <li key={i}>{line}</li>)}</ul>
|
|
<p>
|
|
{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.
|
|
</p>
|
|
<div className="flex flex-wrap gap-2">
|
|
{pendingIds.length > 0 && <button onClick={useAllNew} disabled={busy} className="px-3 py-1.5 rounded-lg bg-amber-600 text-white text-sm font-medium hover:bg-amber-700 disabled:opacity-50">Use all the new text</button>}
|
|
<button onClick={markReviewed} disabled={busy} className="px-3 py-1.5 rounded-lg border border-amber-300 bg-white text-sm hover:bg-amber-100 disabled:opacity-50">Done — hide this</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{msg && <div className={`p-3 rounded-lg text-sm ${msg.type === "ok" ? "bg-green-50 text-green-800 border border-green-200" : "bg-red-50 text-red-800 border border-red-200"}`}>{msg.text}</div>}
|
|
|
|
{publishing && (
|
|
<form onSubmit={publish} className="border rounded-xl p-4 bg-brand-50/40 space-y-3">
|
|
<div className="font-medium text-gray-900">Publish version {(current?.version ?? 0) + 1}</div>
|
|
<div>
|
|
<label className="block text-sm font-medium text-gray-700 mb-1">What changed</label>
|
|
<textarea className={inputCls} rows={2} maxLength={2000} value={changeSummary} onChange={e => setChangeSummary(e.target.value)} placeholder="e.g. We now use PayFast for online payments." />
|
|
</div>
|
|
<label className="flex items-start gap-2 text-sm text-gray-700">
|
|
<input type="checkbox" className="mt-0.5 h-4 w-4 accent-brand-600" checked={requiresAcceptance} onChange={e => setRequiresAcceptance(e.target.checked)} />
|
|
<span>
|
|
<strong>Significant change</strong> — ask everyone to accept this version the next time they log in.
|
|
<span className="block text-xs text-gray-500">Leave unticked for small fixes (wording, typos, contact details).</span>
|
|
</span>
|
|
</label>
|
|
<div className="flex gap-2">
|
|
<button type="button" onClick={() => setPublishing(false)} className="px-3 py-1.5 rounded-lg bg-gray-100 text-sm hover:bg-gray-200">Cancel</button>
|
|
<button type="submit" disabled={busy || (requiresAcceptance && !changeSummary.trim())} className="px-3 py-1.5 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700 disabled:opacity-50">
|
|
{busy ? "Publishing…" : "Publish"}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)}
|
|
|
|
{preview ? (
|
|
<div className="border rounded-xl p-6 bg-white text-gray-700 leading-relaxed">
|
|
{dirty && <p className="mb-4 text-xs text-amber-700">Showing the last saved draft — save to preview your latest edits.</p>}
|
|
{draft.rendered.map((s, i) => <PrivacyNoticeSection key={s.id} section={s} first={i === 0} />)}
|
|
</div>
|
|
) : (
|
|
<div className="space-y-3">
|
|
{sections.map((s, i) => {
|
|
const original = builtInById.get(s.id);
|
|
const hasNew = updatedIds.has(s.id) && differs(s);
|
|
return (
|
|
<div key={`${s.id}-${i}`} className={`border rounded-xl p-3 bg-white ${hasNew ? "border-amber-300" : ""}`} data-testid={hasNew ? "section-has-new-text" : undefined}>
|
|
<div className="flex items-center gap-2 mb-2">
|
|
<input className={`${inputCls} font-medium`} value={s.title} onChange={e => update(i, { title: e.target.value })} aria-label="Section title" />
|
|
<button onClick={() => move(i, -1)} disabled={i === 0} className="p-1.5 rounded hover:bg-gray-100 disabled:opacity-30" aria-label="Move up"><ArrowUp className="w-4 h-4" /></button>
|
|
<button onClick={() => move(i, 1)} disabled={i === sections.length - 1} className="p-1.5 rounded hover:bg-gray-100 disabled:opacity-30" aria-label="Move down"><ArrowDown className="w-4 h-4" /></button>
|
|
{original && (original.body !== s.body || original.title !== s.title) && (
|
|
<button onClick={() => update(i, { title: original.title, body: original.body })} className="p-1.5 rounded hover:bg-gray-100" title="Restore the built-in text" aria-label="Restore the built-in text"><RotateCcw className="w-4 h-4" /></button>
|
|
)}
|
|
<button onClick={() => confirm(`Remove "${s.title}"?`) && change(sections.filter((_, j) => j !== i))} className="p-1.5 rounded hover:bg-red-50 text-red-600" aria-label="Remove section"><Trash2 className="w-4 h-4" /></button>
|
|
</div>
|
|
<textarea className={`${inputCls} font-mono text-xs`} rows={Math.min(18, Math.max(3, s.body.split("\n").length + 1))} value={s.body} onChange={e => update(i, { body: e.target.value })} aria-label={`${s.title} text`} />
|
|
{hasNew && original && (
|
|
<details className="mt-2 text-xs">
|
|
<summary className="cursor-pointer text-amber-800 font-medium">New built-in text for this section — show it</summary>
|
|
<pre className="mt-2 whitespace-pre-wrap rounded-lg bg-amber-50 border border-amber-200 p-2 font-mono text-[11px] text-gray-700">{original.body}</pre>
|
|
<button onClick={() => update(i, { title: original.title, body: original.body })} className="mt-2 px-3 py-1.5 rounded-lg bg-amber-600 text-white hover:bg-amber-700">Use the new text</button>
|
|
</details>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
{missing.length > 0 && (
|
|
<div className="border border-dashed rounded-xl p-3 text-sm text-gray-600 space-y-1">
|
|
<div className="font-medium text-gray-800">Built-in sections not in your notice</div>
|
|
{missing.map(b => (
|
|
<div key={b.id} className="flex items-center justify-between gap-2">
|
|
<span>{b.title}{updatedIds.has(b.id) && <span className="ml-1 text-xs text-amber-700">(new text)</span>}</span>
|
|
<button onClick={() => addBuiltIn(b)} className="inline-flex items-center gap-1 px-2 py-1 rounded border bg-white text-xs hover:bg-gray-50"><Plus className="w-3.5 h-3.5" /> Add</button>
|
|
</div>
|
|
))}
|
|
</div>
|
|
)}
|
|
<button onClick={() => change([...sections, { id: `section-${sections.length + 1}`, title: "New section", body: "" }])} className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg border bg-white text-sm hover:bg-gray-50">
|
|
<Plus className="w-4 h-4" /> Add section
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{draft.versions.length > 0 && (
|
|
<div>
|
|
<h3 className="text-sm font-semibold text-gray-900 mb-2">Published versions</h3>
|
|
<div className="overflow-x-auto border rounded-xl bg-white">
|
|
<table className="w-full text-sm">
|
|
<thead className="bg-gray-50 text-left text-xs text-gray-500">
|
|
<tr><th className="p-2">Version</th><th className="p-2">Published</th><th className="p-2">What changed</th><th className="p-2">Re-accept</th><th className="p-2 text-right">Accepted by</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
{draft.versions.map(v => (
|
|
<tr key={v.version} className="border-t">
|
|
<td className="p-2"><Link href={`/legal/privacy?version=${v.version}`} target="_blank" className="text-brand-600 hover:underline">{v.version}</Link></td>
|
|
<td className="p-2 whitespace-nowrap">{fmt(v.publishedAt)}{v.publishedBy ? ` · ${v.publishedBy}` : ""}</td>
|
|
<td className="p-2 text-gray-600">{v.changeSummary || "—"}</td>
|
|
<td className="p-2">{v.requiresAcceptance ? "Yes" : "No"}</td>
|
|
<td className="p-2 text-right">{v.acceptances}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|