This repository has been archived on 2026-11-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
hope-events/frontend/src/components/admin/privacy/ReadinessPanel.tsx
T

74 lines
3.3 KiB
TypeScript

"use client";
import React, { useCallback, useEffect, useState } from "react";
import Link from "next/link";
import { CheckCircle2, Circle, ArrowRight } from "lucide-react";
import { errorMessage } from "@/lib/utils";
import { apiV2Data } from "@/lib/apiV2";
type Check = { key: string; label: string; ok: boolean; detail: string; href: string | null; applies: boolean };
type Readiness = { score: number; passed: number; total: number; checks: Check[] };
/** Admin → Privacy → Readiness: a checklist of what's set up for POPIA, with a score. */
export function ReadinessPanel({ token }: { token: string }) {
const [data, setData] = useState<Readiness | null>(null);
const [error, setError] = useState<string | null>(null);
const load = useCallback(async () => {
try {
setData(await (apiV2Data("get", "/privacy/readiness", { authToken: token }) as Promise<Readiness>));
} catch (e) {
setError(errorMessage(e) || "Could not load the checklist.");
}
}, [token]);
useEffect(() => { load(); }, [load]);
if (error) return <p className="text-sm text-red-700">{error}</p>;
if (!data) return <p className="text-sm text-gray-400">Loading…</p>;
const tone = data.score >= 90 ? "text-green-700 bg-green-50 border-green-200" : data.score >= 60 ? "text-amber-800 bg-amber-50 border-amber-200" : "text-red-700 bg-red-50 border-red-200";
const todo = data.checks.filter(c => c.applies && !c.ok);
const done = data.checks.filter(c => c.applies && c.ok);
const na = data.checks.filter(c => !c.applies);
const Row = ({ c }: { c: Check }) => (
<li className="flex items-start gap-3 border rounded-xl p-3 bg-white">
{c.ok ? <CheckCircle2 className="w-5 h-5 text-green-600 shrink-0 mt-0.5" /> : <Circle className="w-5 h-5 text-amber-500 shrink-0 mt-0.5" />}
<div className="min-w-0 flex-1 text-sm">
<div className="font-medium text-gray-900">{c.label}</div>
<div className="text-gray-600 break-words">{c.detail}</div>
</div>
{!c.ok && c.href && (
<Link href={c.href} className="shrink-0 inline-flex items-center gap-1 text-sm text-brand-600 hover:underline">
Fix <ArrowRight className="w-3.5 h-3.5" />
</Link>
)}
</li>
);
return (
<div className="space-y-5">
<div className={`flex items-center gap-4 border rounded-xl p-4 ${tone}`}>
<div className="text-4xl font-bold tabular-nums">{data.score}%</div>
<div className="text-sm">
<div className="font-semibold">POPIA readiness</div>
<div>{data.passed} of {data.total} checks done. This helps you comply; it isn&apos;t legal advice — your Information Officer stays responsible.</div>
</div>
</div>
{todo.length > 0 && (
<div>
<h3 className="text-sm font-semibold text-gray-900 mb-2">To do</h3>
<ul className="space-y-2">{todo.map(c => <Row key={c.key} c={c} />)}</ul>
</div>
)}
{done.length > 0 && (
<div>
<h3 className="text-sm font-semibold text-gray-900 mb-2">Done</h3>
<ul className="space-y-2">{done.map(c => <Row key={c.key} c={c} />)}</ul>
</div>
)}
{na.length > 0 && <p className="text-xs text-gray-500">Not counted (doesn&apos;t apply here): {na.map(c => c.label).join(", ")}.</p>}
</div>
);
}