"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(null); const [summary, setSummary] = useState(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("summarise_report", { reportKey, ...p }, token)); } catch (e) { setError(errorMessage(e) || "The AI couldn't summarise this report."); } setBusy(false); }; return (
{!summary && {busy ? "Summarising…" : "Summarise this report"}} {error &&

{error}

} {summary && (

{summary.headline}

    {summary.insights.map(i =>
  • {i.title}. {i.detail}
  • )}
{summary.watchOut.length > 0 && (
    {summary.watchOut.map(w =>
  • {w}
  • )}
)}
)}
); }