import React, { useEffect, useState } from 'react'; import { __ } from '@wordpress/i18n'; import { api } from '../api'; import { Button, Badge, Notice, Segmented } from './ui'; import { IconSparkle } from './icons'; /** * The per-page AI tools that used to be routing entries with no caller: * passage rewrites, a schema proposal, alt text, internal links, translation. * * Same contract as the title/description card: every tool proposes, shows the * receipt (model, tokens, cost), and only an explicit Apply writes — one item * at a time, so a reviewer can take three of five rewrites and leave two. * Content-changing applies keep a snapshot in the page's AI history for undo. */ const Receipt = ({ r }) => r && r.model ? ( {r.provider} · {r.model} · {(r.prompt_tokens || 0) + (r.completion_tokens || 0)} {__('tokens', 'ai-rank-suite')} · {r.cost} · {r.latency_ms} ms ) : null; function useRun() { const [busy, setBusy] = useState(''); const [error, setError] = useState(null); const run = async (name, fn) => { setBusy(name); setError(null); try { return await fn(); } catch (e) { setError(e.message); return null; } finally { setBusy(''); } }; return { busy, error, run, setError }; } /* ---------- Rewrite ---------- */ function Rewrite({ postId, onServerEdit }) { const { busy, error, run } = useRun(); const [data, setData] = useState(null); const [focus, setFocus] = useState(''); const [drafts, setDrafts] = useState({}); const [applied, setApplied] = useState({}); const [canUndo, setCanUndo] = useState(false); const propose = () => run('propose', async () => { const r = await api.post(`/ai/rewrite/${postId}`, { focus }); setData(r); setDrafts(Object.fromEntries((r.edits || []).map((e, i) => [i, e.rewritten]))); setApplied({}); }); const apply = (i) => run(`apply${i}`, async () => { const e = data.edits[i]; await api.post(`/ai/rewrite/${postId}/apply`, { original: e.original, rewritten: drafts[i] }); setApplied((a) => ({ ...a, [i]: true })); setCanUndo(true); // The body of the page just changed, so the word count, the scores // and every check are now about the previous version. if (onServerEdit) onServerEdit(); }); const undo = () => run('undo', async () => { await api.post(`/ai/rewrite/${postId}/undo`, {}); setCanUndo(false); setApplied({}); if (onServerEdit) onServerEdit(); }); return (

{__('The AI picks the passages where a change most improves quotability or clarity and rewrites only those — no new facts, same language and register. Apply the ones you agree with; each apply is undoable.', 'ai-rank-suite')}

setFocus(e.target.value)} style={{ flex: '1 1 260px' }} /> {canUndo && ( )}
{error && {error}} {data && data.edits.length === 0 && {__('The AI found nothing worth changing.', 'ai-rank-suite')}} {data && data.edits.map((e, i) => (
{e.kind.replace('_', ' ')} {typeof e.confidence === 'number' && = 70 ? 'good' : 'warning'}>{e.confidence}%} {!e.locatable && {__('passage not found verbatim — copy by hand', 'ai-rank-suite')}} {applied[i] && ✓ {__('applied', 'ai-rank-suite')}} {e.locatable && !applied[i] && ( )}
{__('Now', 'ai-rank-suite')}: {e.original}