"use client"; import React, { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { errorMessage } from "@/lib/utils"; import { apiV2Data } from "@/lib/apiV2"; type Policy = { enabled: boolean; months: number }; type Definition = { label: string; description: string; minMonths: number; defaults: Policy }; type State = { policies: Record; definitions: Record; reviewedAt: string | null; reviewedBy: string | null; preview: Record>; lastRun: { at: string; result: Record> } | null; related: { medicalDays: number; backupCount: number; analyticsDays: number }; }; type Msg = { type: "ok" | "err"; text: string } | null; const fmt = (d: string) => new Date(d).toLocaleString(undefined, { day: "numeric", month: "short", year: "numeric", hour: "2-digit", minute: "2-digit" }); function previewText(key: string, p: Record | undefined) { if (!p) return ""; if (key === "inactive_accounts") { if (!p.toWarn && !p.toAnonymise) return "Nobody is affected right now."; return `Next run: ${p.toWarn} to warn, ${p.toAnonymise} to anonymise (warned 30+ days ago).`; } return p.count ? `Next run affects ${p.count} record${p.count === 1 ? "" : "s"}.` : "Nothing is due right now."; } /** Admin → Privacy → Retention: how long personal information is kept. */ export function RetentionPanel({ token }: { token: string }) { const [state, setState] = useState(null); const [policies, setPolicies] = useState>({}); const [busy, setBusy] = useState(false); const [msg, setMsg] = useState(null); const apply = (s: State) => { setState(s); setPolicies(s.policies); }; const load = useCallback(async () => { try { apply(await (apiV2Data("get", "/privacy/retention", { authToken: token }) as Promise)); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not load retention settings." }); } }, [token]); useEffect(() => { load(); }, [load]); const save = async () => { const inactive = policies.inactive_accounts; if (state && inactive?.enabled && !state.policies.inactive_accounts.enabled && !confirm("Switch on deleting inactive accounts? People are warned 30 days before; after that it can't be undone.")) return; setBusy(true); setMsg(null); try { apply(await (apiV2Data("put", "/privacy/retention", { authToken: token, body: { policies } }) as Promise)); setMsg({ type: "ok", text: "Saved. The privacy notice's retention section is updated to match." }); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "Could not save." }); } finally { setBusy(false); } }; const runNow = async () => { if (!confirm("Run the retention job now? It deletes and anonymises everything past its retention period, just like the nightly run.")) return; setBusy(true); setMsg(null); try { const res = await (apiV2Data("post", "/privacy/retention/run", { authToken: token, body: {} }) as Promise<{ state: State }>); apply(res.state); setMsg({ type: "ok", text: "Done — see \"Last run\" below." }); } catch (e) { setMsg({ type: "err", text: errorMessage(e) || "The run failed." }); } finally { setBusy(false); } }; if (!state) return msg ?

{msg.text}

:

Loading…

; const dirty = JSON.stringify(policies) !== JSON.stringify(state.policies); return (

POPIA says personal information mustn't be kept longer than needed. Every night at 03:30 the retention job deletes or anonymises what's past these periods. The defaults are a sensible start — check them with your Information Officer and save, even if you change nothing. {state.reviewedAt ? ` Last saved ${fmt(state.reviewedAt)}${state.reviewedBy ? ` by ${state.reviewedBy}` : ""}.` : " Not reviewed yet."}

    {Object.entries(state.definitions).map(([key, def]) => { const p = policies[key]; return (
  • {def.description}

    {p.enabled ? previewText(key, state.preview[key]) : "Off — kept until someone asks for it to be deleted."} {(p.months !== def.defaults.months || p.enabled !== def.defaults.enabled) && ` (Default: ${def.defaults.enabled ? `${def.defaults.months} months` : "off"}.)`}

  • ); })}
Set elsewhere
Medical details from parental consent: deleted {state.related.medicalDays} days after the event — Site settings → Legal.
Database backups: the last {state.related.backupCount} are kept — Site settings → Backups.
Visit statistics: rolled up into anonymous daily totals after {state.related.analyticsDays} days.
Consent records and privacy notice acceptances are kept as proof (without the IP address once an account is deleted).
{msg &&
{msg.text}
}
{state.lastRun && (
Last run {fmt(state.lastRun.at)}:{" "} {Object.entries(state.lastRun.result).length === 0 ? "nothing switched on." : Object.entries(state.lastRun.result).map(([k, r]) => `${state.definitions[k]?.label || k} — ${Object.entries(r).map(([a, b]) => `${a}: ${b}`).join(", ")}`).join("; ")}
)}
); }