Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
221 lines
14 KiB
TypeScript
221 lines
14 KiB
TypeScript
"use client";
|
|
|
|
import React, { useCallback, useEffect, useState } from "react";
|
|
import { Plus, Copy, Pencil } from "lucide-react";
|
|
import { errorMessage } from "@/lib/utils";
|
|
import { useSiteSettings } from "@/contexts/SiteSettingsContext";
|
|
import { apiV2Data } from "@/lib/apiV2";
|
|
|
|
type Status = "open" | "contained" | "closed";
|
|
type Breach = {
|
|
id: string; title: string; status: Status; discoveredAt: string; occurredAt: string | null; description: string; dataAffected: string;
|
|
peopleAffected: number | null; whoAffected: string | null; consequences: string | null; actionsTaken: string | null; adviceToPeople: string | null;
|
|
causedBy: string | null; regulatorNotifiedAt: string | null; peopleNotifiedAt: string | null; createdBy: { name: string } | null; createdAt: string;
|
|
};
|
|
type Form = Record<"title" | "description" | "dataAffected" | "whoAffected" | "consequences" | "actionsTaken" | "adviceToPeople" | "causedBy" | "discoveredAt" | "occurredAt" | "peopleAffected", string>;
|
|
|
|
const EMPTY: Form = { title: "", description: "", dataAffected: "", whoAffected: "", consequences: "", actionsTaken: "", adviceToPeople: "", causedBy: "", discoveredAt: "", occurredAt: "", peopleAffected: "" };
|
|
const STATUS: Record<Status, { label: string; tone: string }> = {
|
|
open: { label: "Open", tone: "bg-red-50 text-red-700" },
|
|
contained: { label: "Contained", tone: "bg-amber-50 text-amber-800" },
|
|
closed: { label: "Closed", tone: "bg-gray-100 text-gray-600" },
|
|
};
|
|
const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-500";
|
|
const fmt = (d: string | null) => (d ? new Date(d).toLocaleString(undefined, { day: "numeric", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit" }) : "—");
|
|
// <input type="datetime-local"> wants local "YYYY-MM-DDTHH:mm".
|
|
const toLocal = (d: string | null) => {
|
|
if (!d) return "";
|
|
const x = new Date(d);
|
|
return new Date(x.getTime() - x.getTimezoneOffset() * 60000).toISOString().slice(0, 16);
|
|
};
|
|
|
|
function BreachForm({ initial, onSave, onCancel, busy }: { initial: Form; onSave: (f: Form) => void; onCancel: () => void; busy: boolean }) {
|
|
const [f, setF] = useState(initial);
|
|
const field = (k: keyof Form, label: string, opts: { rows?: number; required?: boolean; type?: string; placeholder?: string } = {}) => (
|
|
<label className="block text-sm text-gray-700">
|
|
{label}{opts.required && " *"}
|
|
{opts.rows ? (
|
|
<textarea className={inputCls} rows={opts.rows} value={f[k]} required={opts.required} placeholder={opts.placeholder} onChange={e => setF({ ...f, [k]: e.target.value })} />
|
|
) : (
|
|
<input className={inputCls} type={opts.type || "text"} value={f[k]} required={opts.required} placeholder={opts.placeholder} onChange={e => setF({ ...f, [k]: e.target.value })} />
|
|
)}
|
|
</label>
|
|
);
|
|
return (
|
|
<form onSubmit={e => { e.preventDefault(); onSave(f); }} className="border rounded-xl p-4 bg-gray-50 space-y-3">
|
|
{field("title", "Short title", { required: true, placeholder: "e.g. Attendee list emailed to the wrong group" })}
|
|
<div className="grid sm:grid-cols-3 gap-3">
|
|
{field("discoveredAt", "Discovered", { type: "datetime-local", required: true })}
|
|
{field("occurredAt", "Happened (if known)", { type: "datetime-local" })}
|
|
{field("peopleAffected", "People affected (about)", { type: "number" })}
|
|
</div>
|
|
{field("description", "What happened", { rows: 3, required: true })}
|
|
{field("dataAffected", "What information was affected", { rows: 2, required: true, placeholder: "e.g. names, phone numbers and medical details of 40 camp attendees" })}
|
|
{field("whoAffected", "Who was affected", { rows: 2, placeholder: "e.g. everyone registered for Youth Camp 2026" })}
|
|
{field("consequences", "Possible consequences for them", { rows: 2 })}
|
|
{field("actionsTaken", "What you've done or will do about it", { rows: 2 })}
|
|
{field("adviceToPeople", "What affected people should do", { rows: 2, placeholder: "e.g. be careful of messages asking for payment" })}
|
|
{field("causedBy", "Who caused it (if known)", {})}
|
|
<div className="flex gap-2">
|
|
<button type="button" onClick={onCancel} className="px-3 py-1.5 rounded-lg bg-white border text-sm hover:bg-gray-100">Cancel</button>
|
|
<button type="submit" 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">{busy ? "Saving…" : "Save"}</button>
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|
|
|
|
function CopyBlock({ title, text }: { title: string; text: string }) {
|
|
const [copied, setCopied] = useState(false);
|
|
return (
|
|
<div className="border rounded-lg bg-white">
|
|
<div className="flex items-center justify-between px-3 py-2 border-b">
|
|
<span className="text-sm font-medium text-gray-800">{title}</span>
|
|
<button onClick={() => { navigator.clipboard?.writeText(text); setCopied(true); setTimeout(() => setCopied(false), 1500); }} className="inline-flex items-center gap-1 text-xs text-brand-600 hover:underline">
|
|
<Copy className="w-3.5 h-3.5" /> {copied ? "Copied" : "Copy"}
|
|
</button>
|
|
</div>
|
|
<pre className="p-3 text-xs text-gray-700 whitespace-pre-wrap font-sans">{text}</pre>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Admin → Privacy → Breaches: the register of security compromises (POPIA s22). */
|
|
export function BreachesPanel({ token }: { token: string }) {
|
|
const { settings } = useSiteSettings();
|
|
const [list, setList] = useState<Breach[] | null>(null);
|
|
const [editing, setEditing] = useState<string | "new" | null>(null);
|
|
const [open, setOpen] = useState<string | null>(null);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const load = useCallback(async () => {
|
|
try {
|
|
setList(await (apiV2Data("get", "/privacy/breaches", { authToken: token }) as Promise<Breach[]>));
|
|
} catch (e) {
|
|
setError(errorMessage(e) || "Could not load the register.");
|
|
}
|
|
}, [token]);
|
|
useEffect(() => { load(); }, [load]);
|
|
|
|
const send = async (id: string | "new", body: Record<string, unknown>) => {
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
if (id === "new") await apiV2Data("post", "/privacy/breaches", { authToken: token, body });
|
|
else await apiV2Data("put", "/privacy/breaches/{id}", { params: { id: id }, authToken: token, body });
|
|
setEditing(null);
|
|
await load();
|
|
} catch (e) {
|
|
setError(errorMessage(e) || "Could not save.");
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
const save = (f: Form) => send(editing!, {
|
|
...f,
|
|
discoveredAt: f.discoveredAt ? new Date(f.discoveredAt).toISOString() : "",
|
|
occurredAt: f.occurredAt ? new Date(f.occurredAt).toISOString() : null,
|
|
peopleAffected: f.peopleAffected === "" ? null : f.peopleAffected,
|
|
});
|
|
|
|
const org = settings.org_name || "We";
|
|
const io = [settings.legal_io_name, settings.legal_io_email].filter(Boolean).join(", ") || "our Information Officer";
|
|
|
|
const peopleText = (b: Breach) => [
|
|
`Dear member,`,
|
|
``,
|
|
`We are writing to tell you about a security incident that affected your personal information held by ${org}.`,
|
|
``,
|
|
`What happened: ${b.description}`,
|
|
`Information involved: ${b.dataAffected}`,
|
|
b.consequences ? `What this could mean for you: ${b.consequences}` : "",
|
|
b.actionsTaken ? `What we have done: ${b.actionsTaken}` : "",
|
|
b.adviceToPeople ? `What you can do: ${b.adviceToPeople}` : "",
|
|
b.causedBy ? `Who was responsible: ${b.causedBy}` : "",
|
|
``,
|
|
`We are sorry this happened. If you have questions, contact ${io}. You may also complain to the Information Regulator (inforegulator.org.za).`,
|
|
].filter((l, i, a) => l !== "" || a[i - 1] !== "").join("\n");
|
|
|
|
const regulatorText = (b: Breach) => [
|
|
`Responsible party: ${settings.legal_operator_name || org}`,
|
|
`Information Officer: ${io}`,
|
|
`Discovered: ${fmt(b.discoveredAt)}${b.occurredAt ? ` · Happened: ${fmt(b.occurredAt)}` : ""}`,
|
|
`Description: ${b.description}`,
|
|
`Personal information affected: ${b.dataAffected}`,
|
|
`People affected: ${b.peopleAffected ?? "unknown"}${b.whoAffected ? ` — ${b.whoAffected}` : ""}`,
|
|
b.consequences ? `Possible consequences: ${b.consequences}` : "",
|
|
b.actionsTaken ? `Measures taken: ${b.actionsTaken}` : "",
|
|
b.adviceToPeople ? `Advice given to data subjects: ${b.adviceToPeople}` : "",
|
|
b.causedBy ? `Identity of the unauthorised person (if known): ${b.causedBy}` : "",
|
|
`Data subjects notified: ${b.peopleNotifiedAt ? fmt(b.peopleNotifiedAt) : "not yet"}`,
|
|
].filter(Boolean).join("\n");
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<p className="text-sm text-gray-600">
|
|
Record any security compromise — a lost phone with the attendee list, an email to the wrong people, a hacked account. POPIA (section 22)
|
|
requires you to tell the Information Regulator and the people affected as soon as reasonably possible. Each entry gives you a draft
|
|
for both; report to the Regulator on its website (inforegulator.org.za) and record the date here.
|
|
</p>
|
|
{error && <p className="text-sm text-red-700 bg-red-50 border border-red-200 rounded-lg p-3">{error}</p>}
|
|
|
|
{editing === "new" ? (
|
|
<BreachForm busy={busy} initial={{ ...EMPTY, discoveredAt: toLocal(new Date().toISOString()) }} onCancel={() => setEditing(null)} onSave={save} />
|
|
) : (
|
|
<button onClick={() => setEditing("new")} className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-brand-600 text-white text-sm font-medium hover:bg-brand-700">
|
|
<Plus className="w-4 h-4" /> Record a security compromise
|
|
</button>
|
|
)}
|
|
|
|
{list === null ? <p className="text-sm text-gray-400">Loading…</p> : list.length === 0 ? <p className="text-sm text-gray-500">Nothing recorded.</p> : (
|
|
<ul className="space-y-3">
|
|
{list.map(b => editing === b.id ? (
|
|
<li key={b.id}>
|
|
<BreachForm busy={busy} onCancel={() => setEditing(null)} onSave={save} initial={{
|
|
title: b.title, description: b.description, dataAffected: b.dataAffected, whoAffected: b.whoAffected || "", consequences: b.consequences || "",
|
|
actionsTaken: b.actionsTaken || "", adviceToPeople: b.adviceToPeople || "", causedBy: b.causedBy || "",
|
|
discoveredAt: toLocal(b.discoveredAt), occurredAt: toLocal(b.occurredAt), peopleAffected: b.peopleAffected == null ? "" : String(b.peopleAffected),
|
|
}} />
|
|
</li>
|
|
) : (
|
|
<li key={b.id} className="border rounded-xl bg-white">
|
|
<button onClick={() => setOpen(open === b.id ? null : b.id)} className="w-full flex flex-wrap items-center gap-2 p-3 text-left">
|
|
<span className={`text-xs px-2 py-0.5 rounded-full ${STATUS[b.status].tone}`}>{STATUS[b.status].label}</span>
|
|
<span className="font-medium text-gray-900">{b.title}</span>
|
|
<span className="ml-auto text-xs text-gray-500">
|
|
Discovered {fmt(b.discoveredAt)} · Regulator {b.regulatorNotifiedAt ? "told" : <strong className="text-red-700">not told</strong>} · People {b.peopleNotifiedAt ? "told" : "not told"}
|
|
</span>
|
|
</button>
|
|
{open === b.id && (
|
|
<div className="border-t p-3 space-y-3 text-sm">
|
|
<dl className="grid sm:grid-cols-[10rem_1fr] gap-x-3 gap-y-1 text-gray-700">
|
|
<dt className="text-gray-500">What happened</dt><dd className="whitespace-pre-line">{b.description}</dd>
|
|
<dt className="text-gray-500">Information</dt><dd className="whitespace-pre-line">{b.dataAffected}</dd>
|
|
<dt className="text-gray-500">Who</dt><dd>{b.peopleAffected ?? "?"} people{b.whoAffected ? ` — ${b.whoAffected}` : ""}</dd>
|
|
{b.actionsTaken && <><dt className="text-gray-500">Done</dt><dd className="whitespace-pre-line">{b.actionsTaken}</dd></>}
|
|
<dt className="text-gray-500">Recorded</dt><dd>{fmt(b.createdAt)}{b.createdBy ? ` by ${b.createdBy.name}` : ""}</dd>
|
|
</dl>
|
|
<div className="flex flex-wrap gap-2">
|
|
<button onClick={() => setEditing(b.id)} className="inline-flex items-center gap-1 px-3 py-1.5 rounded-lg border text-sm hover:bg-gray-50"><Pencil className="w-3.5 h-3.5" /> Edit</button>
|
|
{!b.regulatorNotifiedAt && <button disabled={busy} onClick={() => send(b.id, { regulatorNotifiedAt: new Date().toISOString() })} className="px-3 py-1.5 rounded-lg border text-sm hover:bg-gray-50">Regulator told today</button>}
|
|
{!b.peopleNotifiedAt && <button disabled={busy} onClick={() => send(b.id, { peopleNotifiedAt: new Date().toISOString() })} className="px-3 py-1.5 rounded-lg border text-sm hover:bg-gray-50">People told today</button>}
|
|
{b.status !== "contained" && b.status !== "closed" && <button disabled={busy} onClick={() => send(b.id, { status: "contained" })} className="px-3 py-1.5 rounded-lg border text-sm hover:bg-gray-50">Mark contained</button>}
|
|
{b.status !== "closed" ? (
|
|
<button disabled={busy} onClick={() => send(b.id, { status: "closed" })} className="px-3 py-1.5 rounded-lg border text-sm hover:bg-gray-50">Close</button>
|
|
) : (
|
|
<button disabled={busy} onClick={() => send(b.id, { status: "open" })} className="px-3 py-1.5 rounded-lg border text-sm hover:bg-gray-50">Reopen</button>
|
|
)}
|
|
</div>
|
|
<CopyBlock title="Draft message to the people affected" text={peopleText(b)} />
|
|
<CopyBlock title="Summary for the Information Regulator's form" text={regulatorText(b)} />
|
|
<p className="text-xs text-gray-500">These are drafts — check them with your Information Officer before sending.</p>
|
|
</div>
|
|
)}
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|