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/reports/AiReportSummary.tsx
T
joshuaandClaude Opus 5.5 89eab66145 Summarise this report on every report (2.0.1)
The eight reports that list people get the AI summary too, sent as
anonymised totals built on the screen (aiReportData.ts): no names,
contact details, notes or comments; consent and medical only as counts.
The server refuses personal columns on those reports and logs each
summary in the access log (ai_report; sensitive for consent and medical).

Roadmap §18: 2.0.1, then crosscode.co.za, then 2.1.0 (AI helpers on the
Consent log and Access log).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 18:42:27 +02:00

62 lines
2.8 KiB
TypeScript

"use client";
import React, { useState } from "react";
import { AlertTriangle } from "lucide-react";
import { useAuth } from "@/hooks/useAuth";
import { useAiStatus } from "@/hooks/useAiStatus";
import { runAi } from "@/lib/ai";
import type { V2Body, V2Result } from "@/lib/apiV2";
import type { AiReportData } from "./aiReportData";
import { errorMessage } from "@/lib/utils";
import { AiButton, AiNotice } from "@/components/shared/AiButton";
type Summary = V2Result<"/ai/summarise-report", "post">["data"]["output"];
type ReportKey = V2Body<"/ai/summarise-report", "post">["reportKey"];
/**
* "Summarise this report": a headline, a few insights and anything to act on, from the report as
* it is on screen. Reports that list people are sent as anonymised totals (see aiReportData). Hidden
* on sites without AI.
*/
export function AiReportSummary({ reportKey, getPayload }: { reportKey: ReportKey; getPayload: () => AiReportData | null }) {
const { token } = useAuth();
const status = useAiStatus();
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string | null>(null);
const [summary, setSummary] = useState<Summary | null>(null);
if (!status?.enabled) return null;
const run = async () => {
const p = getPayload();
if (!token || !p?.rows.length) { setError("This report has no figures to summarise yet."); return; }
setBusy(true);
setError(null);
try {
setSummary(await runAi<Summary>("summarise_report", { reportKey, ...p }, token));
} catch (e) { setError(errorMessage(e) || "The AI couldn't summarise this report."); }
setBusy(false);
};
return (
<div className="mb-4 space-y-2" data-testid="ai-report-summary">
{!summary && <AiButton helper="summarise_report" busy={busy} onClick={run}>{busy ? "Summarising…" : "Summarise this report"}</AiButton>}
{error && <p className="text-sm text-red-700" role="alert">{error}</p>}
{summary && (
<div className="space-y-2 rounded-lg border border-violet-200 bg-violet-50/60 p-4 text-sm">
<AiNotice />
<p className="font-medium text-gray-900">{summary.headline}</p>
<ul className="space-y-1.5">
{summary.insights.map(i => <li key={i.title}><span className="font-medium">{i.title}.</span> {i.detail}</li>)}
</ul>
{summary.watchOut.length > 0 && (
<ul className="space-y-1 rounded border border-amber-200 bg-amber-50 p-2 text-amber-900" aria-label="Needs attention">
{summary.watchOut.map(w => <li key={w} className="flex gap-1.5"><AlertTriangle className="mt-0.5 h-3.5 w-3.5 shrink-0" aria-hidden />{w}</li>)}
</ul>
)}
<button type="button" className="text-xs text-gray-600 underline" onClick={() => setSummary(null)}>Hide the summary</button>
</div>
)}
</div>
);
}