import React, { useEffect, useState } from 'react'; import { __ } from '@wordpress/i18n'; import { api } from '../api'; import { Button, Badge, Notice, Spinner, Bar, ScoreRing } from '../components/ui'; import { IconSparkle } from '../components/icons'; import KeywordSuggest from '../components/KeywordSuggest'; /** * The AI setup wizard. * * The standard wizard gets a site ready for search: licence, import, profile, * one key, discovery files, crawlers, first analysis. This one picks up where * it stops and puts the AI to work — in the order that pays off soonest, with * the AI doing the typing at each step and the person only choosing. * * Every step is backed by an endpoint that exists today, and every step is * skippable, so the wizard can never strand someone on a stage their site does * not need. It writes nothing until a step's own button is pressed. */ const STEPS = [ { id: 'engine', label: __('Engine', 'ai-rank-suite') }, { id: 'brief', label: __('Brief the AI', 'ai-rank-suite') }, { id: 'score', label: __('Score', 'ai-rank-suite') }, { id: 'describe', label: __('Descriptions', 'ai-rank-suite') }, { id: 'faq', label: __('FAQs', 'ai-rank-suite') }, { id: 'keywords', label: __('Keywords', 'ai-rank-suite') }, { id: 'prompts', label: __('AI prompts', 'ai-rank-suite') }, { id: 'routing', label: __('Routing', 'ai-rank-suite') }, { id: 'done', label: __('Done', 'ai-rank-suite') }, ]; function Shell({ title, lede, children }) { return ( <>

{title}

{lede && (

{lede}

)} {children} ); } /** Drive one bulk job to completion from here, with a live bar. */ function BulkStep({ action, label, openCount, onDone }) { const [status, setStatus] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const run = async () => { setBusy(true); setError(null); try { let s = await api.post('/bulk/start', { action, scope: 'missing' }); setStatus(s); s = await api.post('/bulk/tick'); setStatus(s); while (s.state === 'running') { s = await api.post('/bulk/tick'); setStatus(s); } onDone(s); } catch (e) { // "Nothing to process" is the server saying this step is already // done — a success in a 400's clothing. if (/nothing to process/i.test(e.message)) { setStatus({ state: 'done', done: 0, total: 0, progress: 100 }); onDone({ state: 'done', done: 0 }); } else { setError(e.message); } } finally { setBusy(false); } }; const finished = status?.state === 'done'; return (
{error && {error}} {status && (
{finished ? __('Finished', 'ai-rank-suite') : __('Working…', 'ai-rank-suite')} {(status.done || 0).toLocaleString()} {status.total ? ` ${__('of', 'ai-rank-suite')} ${status.total.toLocaleString()}` : ''} {status.failed ? ` · ${status.failed} ${__('failed', 'ai-rank-suite')}` : ''} {status.progress ?? 0}%
)} {!finished && (
{typeof openCount === 'number' && ( {openCount.toLocaleString()} {__('pages waiting', 'ai-rank-suite')} )}
)} {finished && ✓ {__('Done', 'ai-rank-suite')}}
); } export default function AiWizard({ onDone }) { const [index, setIndex] = useState(0); const [ready, setReady] = useState(false); const [error, setError] = useState(null); const [busy, setBusy] = useState(false); const [ai, setAi] = useState(null); // /settings/ai const [board, setBoard] = useState(null); // /ai/capabilities const [knowledge, setKnowledge] = useState(null); // /ai/knowledge const [notes, setNotes] = useState(''); const [notesSaved, setNotesSaved] = useState(false); const [verify, setVerify] = useState(null); const [score, setScore] = useState(null); const [tracked, setTracked] = useState([]); const [vis, setVis] = useState(null); const [prompts, setPrompts] = useState([]); const [suggested, setSuggested] = useState(null); const [routing, setRouting] = useState(null); const [routeSaved, setRouteSaved] = useState(false); const [did, setDid] = useState({}); useEffect(() => { Promise.allSettled([ api.get('/settings/ai'), api.get('/ai/capabilities'), api.get('/ai/knowledge'), api.get('/rank/keywords'), api.get('/settings/ai-visibility'), api.get('/ai/routing'), ]).then(([a, b, k, r, v, ro]) => { if (a.status === 'fulfilled') setAi(a.value); if (b.status === 'fulfilled') setBoard(b.value); if (k.status === 'fulfilled') { setKnowledge(k.value); setNotes(k.value.custom || ''); } if (r.status === 'fulfilled') setTracked((r.value.keywords || []).map((x) => x.keyword)); if (v.status === 'fulfilled') { setVis(v.value); setPrompts(v.value.settings?.prompts || v.value.prompts || []); } if (ro.status === 'fulfilled') setRouting(ro.value); setReady(true); }); }, []); const step = STEPS[index].id; const next = () => { setError(null); setIndex((i) => Math.min(STEPS.length - 1, i + 1)); }; const back = () => setIndex((i) => Math.max(0, i - 1)); const guard = async (fn) => { setBusy(true); setError(null); try { await fn(); } catch (e) { setError(e.message); } finally { setBusy(false); } }; const rowOpen = (id) => { const r = (board?.rows || []).find((x) => x.id === id); return r && typeof r.open === 'number' ? r.open : null; }; const refreshBoard = () => api.get('/ai/capabilities').then(setBoard).catch(() => {}); if (!ready) { return (
); } const connected = !!ai?.has_key; return (
{__('AI setup', 'ai-rank-suite')}
{__('Put the AI to work on this site — it types, you choose. Every step can be skipped.', 'ai-rank-suite')}
{STEPS.map((s, i) => (
))}
{__('Step', 'ai-rank-suite')} {index + 1} {__('of', 'ai-rank-suite')} {STEPS.length} · {STEPS[index].label}
{step === 'engine' && ( {connected ? (
{verify && {verify.message}}
) : (
)}
)} {step === 'brief' && (