import React, { useEffect, useState } from 'react'; import { __ } from '@wordpress/i18n'; import { api } from '../api'; import { Card, Button, Badge, Notice } from './ui'; /** * The review queue for suggest/approve autopilot runs. * * Every parked proposal, newest first, with the current value beside it and * an editable copy of the proposal. Apply writes what is in the box; Reject * discards. Paged on the server; a ten-thousand-page run does not land in * the browser at once. */ export default function ProposalQueue({ refreshKey }) { const [data, setData] = useState(null); const [page, setPage] = useState(1); const [drafts, setDrafts] = useState({}); const [busy, setBusy] = useState(''); const [error, setError] = useState(null); const load = () => api .get(`/autopilot/proposals?page=${page}&per_page=25`) .then((r) => { setData(r); setDrafts(Object.fromEntries(r.items.map((it) => [`${it.post_id}:${it.task}`, it.task === 'faq' ? JSON.stringify(it.proposal, null, 1) : it.proposal]))); }) .catch((e) => setError(e.message)); useEffect(() => { load(); }, [page, refreshKey]); // eslint-disable-line react-hooks/exhaustive-deps if (!data) return null; if (data.total === 0) return null; const act = async (it, kind) => { const key = `${it.post_id}:${it.task}`; setBusy(key + kind); setError(null); try { if (kind === 'apply') { let value = drafts[key]; if (it.task === 'faq') { try { value = JSON.parse(value); } catch (e) { throw new Error(__('The FAQ list is not valid JSON — fix it or reject.', 'ai-rank-suite')); } } await api.post('/autopilot/proposals/apply', { post_id: it.post_id, task: it.task, value }); } else { await api.post('/autopilot/proposals/reject', { post_id: it.post_id, task: it.task }); } await load(); } catch (e) { setError(e.message); } finally { setBusy(''); } }; const pages = Math.max(1, Math.ceil(data.total / 25)); return ( {data.total.toLocaleString()} {__('open', 'ai-rank-suite')}} > {error && {error}}
{data.items.map((it) => { const key = `${it.post_id}:${it.task}`; return (
{it.task} {it.title || `#${it.post_id}`}
{__('Now', 'ai-rank-suite')}:{' '} {it.task === 'faq' ? `${Array.isArray(it.current) ? it.current.length : 0} ${__('FAQ items', 'ai-rank-suite')}` : it.current || {__('empty', 'ai-rank-suite')}}