import { Spinner } from '@wordpress/components'; import { useEffect, useState } from '@wordpress/element'; import { api } from '../api'; import type { ProtectionSummary, ProtectionSummaryResponse } from '../types'; /** How often to re-poll the summary while AUTO jobs are still running. */ const POLL_INTERVAL_MS = 5000; /** * Dashboard banner summarizing auto-protect progress (an X-of-Y rollup of AUTO * jobs since auto-protect was enabled). Renders nothing until there's queued * AUTO work to report, and polls while jobs are active. */ export default function AutoProtectBanner(): JSX.Element | null { const [summary, setSummary] = useState(null); useEffect(() => { loadSummary(); }, []); // Keep the counts fresh while the backend is still working AUTO jobs. useEffect(() => { if (!summary || summary.active_jobs <= 0) return; const timer = setInterval(loadSummary, POLL_INTERVAL_MS); return () => clearInterval(timer); }, [summary]); const loadSummary = async (): Promise => { try { const data = await api.get('/protection/summary'); setSummary(data.auto_protect ? data.summary : null); } catch { setSummary(null); } }; // Nothing worth showing until auto-protect has queued some work. if (!summary || summary.total_queued <= 0) return null; const { total_queued, total_done, total_failed, active_jobs } = summary; const percent = Math.min(100, Math.round((total_done / total_queued) * 100)); const done = active_jobs <= 0 && total_done + total_failed >= total_queued; return (

Auto-protect

{active_jobs > 0 && }

{total_done} of {total_queued} images protected {active_jobs > 0 ? ` · ${active_jobs} in progress` : done ? ' · complete' : ''} {total_failed > 0 && · {total_failed} need attention}

); }