import React, { useState } from 'react'; import { __ } from '@wordpress/i18n'; import { api } from '../api'; import { Card, Button, Badge, Notice } from './ui'; import { IconSparkle } from './icons'; /** * The site-audit copilot. * * The deterministic checks are the truth; the copilot may only group them, * explain the likely cause and the impact, estimate effort and risk, and put * them in an order worth working. Ids that do not exist in the run are * dropped server-side, so nothing here can invent a finding. */ const tone = (v) => (v === 'high' ? 'serious' : v === 'medium' ? 'warning' : 'good'); export default function AuditCopilot({ hasRun }) { const [data, setData] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const run = async () => { setBusy(true); setError(null); try { setData(await api.post('/ai/audit/explain', {})); } catch (e) { setError(e.message); } finally { setBusy(false); } }; return ( } action={ } > {!hasRun && {__('Run the audit first; the copilot explains its results.', 'ai-rank-suite')}} {hasRun && !data && !error && !busy && (
{__('Nothing explained yet. The AI reads every finding of the last run, groups them by cause, and writes the steps in the order worth working — with effort and risk for each group.', 'ai-rank-suite')}
)} {busy && !data && {__('Reading the findings…', 'ai-rank-suite')}} {error && {error}} {data && data.groups.length === 0 && {__('Nothing to explain — the run has no findings.', 'ai-rank-suite')}} {data && data.groups.length > 0 && (
{data.groups.map((g, i) => { const rows = data.findings.filter((f) => g.finding_ids.includes(f.id)); const pages = rows.reduce((n, f) => n + f.count, 0); return (
{g.priority} {g.title}
{__('effort', 'ai-rank-suite')} {g.effort} {__('risk', 'ai-rank-suite')} {g.risk} {pages.toLocaleString()} {__('pages', 'ai-rank-suite')}
{g.cause && (

{__('Cause', 'ai-rank-suite')}: {g.cause}

)} {g.impact && (

{__('Impact', 'ai-rank-suite')}: {g.impact}

)}
    {g.steps.map((s, k) => (
  1. {s}
  2. ))}
{rows.map((f) => ( {f.label} · {f.count} ))}
); })} {data.receipt && data.receipt.model && ( {data.receipt.provider} · {data.receipt.model} · {data.receipt.cost} · {data.receipt.latency_ms} ms )}
)}
); }