import React, { useEffect, useRef, useState } from 'react'; import { __, _n, sprintf } from '@wordpress/i18n'; import { api } from '../api'; import { Card, Button, Badge, Notice, Progress } from '../components/ui'; import { IconSparkle, IconRefresh } from '../components/icons'; const ACTIONS = [ { id: 'analyze', label: __('Analyze all pages', 'ai-rank-suite'), desc: __('Recalculate GEO + SEO scores for every published page. Fast, free, no AI needed.', 'ai-rank-suite'), ai: false, }, { id: 'describe', label: __('Write meta descriptions', 'ai-rank-suite'), desc: __('Generate a meta description and an AI summary per page. Uses your AI key.', 'ai-rank-suite'), ai: true, }, { id: 'faq', label: __('Generate FAQs', 'ai-rank-suite'), desc: __('Create grounded Q&A per page — feeds FAQPage schema. Uses your AI key.', 'ai-rank-suite'), ai: true, }, ]; export default function Bulk() { const [status, setStatus] = useState(null); const [scope, setScope] = useState('missing'); const [error, setError] = useState(null); const [starting, setStarting] = useState(null); const running = useRef(false); const pump = async () => { if (running.current) return; running.current = true; try { let s = await api.post('/bulk/tick'); setStatus(s); while (s.state === 'running') { if (s.busy || s.error) await new Promise((r) => setTimeout(r, 2000)); s = await api.post('/bulk/tick'); setStatus(s); } } catch (e) { setError(e.message); } finally { running.current = false; } }; useEffect(() => { api .get('/bulk/status') .then((s) => { setStatus(s); if (s.state === 'running') pump(); }) .catch(() => {}); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const start = async (action) => { setStarting(action); setError(null); try { setStatus(await api.post('/bulk/start', { action, scope })); pump(); } catch (e) { setError(e.message); } finally { setStarting(null); } }; const cancel = async () => { try { setStatus(await api.post('/bulk/cancel')); } catch (e) { setError(e.message); } }; const isRunning = status?.state === 'running'; return (
{error && {error}} {status && status.state !== 'idle' && ( {__('Cancel', 'ai-rank-suite')} ) : ( {status.failed ? sprintf(__('%d failed', 'ai-rank-suite'), status.failed) : '✓ ' + __('Complete', 'ai-rank-suite')} ) } >
{status.errors?.length > 0 && (
    {status.errors.map((e, i) => (
  • {e}
  • ))}
)}
)} {[ { id: 'missing', label: __('Only missing', 'ai-rank-suite') }, { id: 'all', label: __('All pages', 'ai-rank-suite') }, ].map((s) => ( ))}
} >
{ACTIONS.map((a) => (
{a.ai && } {a.label}
{a.desc}
))}

{__('AI actions send one small request per page to your provider. Keep this tab open while a job runs — progress is saved, so you can close it and resume later.', 'ai-rank-suite')}

); }