import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useSearchParams } from 'react-router-dom'; import { CircleHelpIcon as HelpCircle, CirclePlusIcon as PlusCircle, HistoryIcon as History, MailIcon as Mail, RefreshCwIcon, ScanLineIcon as ScanLine, SparklesIcon as Sparkles, } from 'lucide-react'; import { useAppStateContext } from '../context/user.data.context'; import { useCopilotDataChanged } from '../hooks/useCopilotDataChanged'; import { dispatchContentCheck, getContentCheckJob, getLatestContentCheck, listContentCheckJobs, listContentCheckProducts, rerunContentCheck, } from '../service/catalog-quality/catalog-quality.service'; import { getActiveContentGenJob } from '../service/content-generator/content-generator.service'; import { Button } from '../components/ui/button'; import { Select, SelectContent, SelectItem, SelectTrigger, } from '../components/ui/select'; import BulkImproveModal from '../components/catalog-quality/BulkImproveModal'; import ProductActionModal from '../components/catalog-quality/ProductActionModal'; import ScanProductsModal from '../components/catalog-quality/ScanProductsModal'; import { newCheckCost, rerunCost, } from '../components/catalog-quality/check-cost'; import { CREDITS_PER_PRODUCT_SCAN, MAX_SCAN_PRODUCTS, } from '../service/catalog-quality/catalog-quality.constants'; import type { ContentCheckSeverity, IContentCheckJob, IContentCheckProductRow, } from '../service/catalog-quality/catalog-quality.interface'; const SEVERITY_LABELS: Record = { critical: 'Critical', warning: 'Warnings', ok: 'OK', }; const SEVERITY_BADGE: Record< ContentCheckSeverity, { label: string; badge: string } > = { critical: { label: 'Critical', badge: 'bg-red-500/10 text-red-600 dark:text-red-400', }, warning: { label: 'Warning', badge: 'bg-amber-500/10 text-amber-700 dark:text-amber-300', }, ok: { label: 'OK', badge: 'bg-emerald-500/10 text-emerald-700 dark:text-emerald-300', }, }; const TAB_STYLES: Record< 'all' | ContentCheckSeverity, { active: string; inactive: string } > = { all: { active: 'bg-muted text-foreground', inactive: 'text-muted-foreground hover:bg-muted hover:text-foreground', }, critical: { active: 'bg-red-500/10 text-red-700 dark:text-red-300', inactive: 'text-red-600 dark:text-red-400 hover:bg-red-500/10 hover:text-red-700', }, warning: { active: 'bg-amber-500/10 text-amber-700 dark:text-amber-300', inactive: 'text-amber-600 dark:text-amber-400 hover:bg-amber-500/10 hover:text-amber-700', }, ok: { active: 'bg-emerald-500/10 text-emerald-700 dark:text-emerald-300', inactive: 'text-emerald-600 dark:text-emerald-400 hover:bg-emerald-500/10 hover:text-emerald-700', }, }; /** * Compact date + time for a history entry (e.g. "Jul 28, 10:42 AM"). * * @param {string} iso - ISO timestamp from the job row. * @return {string} Localized short date-time, or the raw value when invalid. */ const formatHistoryDate = (iso: string): string => { const parsed = new Date(iso); if (isNaN(parsed.getTime())) return iso; return parsed.toLocaleDateString(undefined, { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit', }); }; /** * Compact dropdown label for one historical scan. * * @param {IContentCheckJob} historyJob - Job row from the history list. * @return {string} Date plus product count and outcome. */ const historyLabel = (historyJob: IContentCheckJob): string => { const when = historyJob.created_at ? formatHistoryDate(historyJob.created_at) : historyJob.job_id; if (historyJob.state === 'completed') { return `${when} · ${historyJob.total_products} products · score ${historyJob.overall_score}`; } if (historyJob.state === 'failed') { return `${when} · failed`; } return `${when} · running`; }; const CatalogQuality = (): JSX.Element => { const { token, clientId } = useAppStateContext(); const [searchParams, setSearchParams] = useSearchParams(); const jobIdParam: string | null = searchParams.get('job'); const severityParam: ContentCheckSeverity | null = (() => { const raw = searchParams.get('severity'); if (raw === 'critical' || raw === 'warning' || raw === 'ok') return raw; return null; })(); const [job, setJob] = useState(null); const [products, setProducts] = useState([]); const [dispatching, setDispatching] = useState(false); const [rerunBusy, setRerunBusy] = useState(false); // True while a content-gen job (e.g. a bulk improve) runs. The backend // serialises these against the audit, so every catalog action is disabled // while one is in flight. const [bulkRunning, setBulkRunning] = useState(false); // bulkScope === null → closed; {} → full catalog; {sku, productName} → one row. const [bulkScope, setBulkScope] = useState<{ sku?: string; productName?: string; } | null>(null); const [scanPickerOpen, setScanPickerOpen] = useState(false); // Recent scans (newest first) for the history dropdown. const [history, setHistory] = useState([]); const refreshJob = useCallback(async (): Promise => { if (!token) return; const fresh: IContentCheckJob | null = jobIdParam ? await getContentCheckJob(jobIdParam).catch(() => null) : await getLatestContentCheck().catch(() => null); setJob(fresh); if (fresh) { const page = await listContentCheckProducts(fresh.job_id, { limit: 50, severity: severityParam, }).catch(() => ({ products: [], next_cursor: null })); setProducts(page.products); } else { setProducts([]); } const jobs = await listContentCheckJobs().catch( () => [] as IContentCheckJob[] ); setHistory(jobs); }, [token, jobIdParam, severityParam]); useEffect(() => { refreshJob(); }, [refreshJob]); // When the copilot runs a content check (or a bulk improve) it dispatches // ``copilot:data-changed``; re-pull the latest job + per-product results so // the audit score and rows reflect the new run without a manual reload. The // in-flight poll below then continues advancing a still-running job. useCopilotDataChanged(() => { void refreshJob(); }); // Lightweight poll for an in-flight content-gen job (e.g. a bulk improve). // Runs continuously while the page is open - decoupled from the audit poll // - so a bulk started from this page disables the buttons within one tick, // and re-enables them once it finishes. const refreshBulkRunning = useCallback(async (): Promise => { if (!clientId) return; const active = await getActiveContentGenJob(clientId).catch(() => null); setBulkRunning(!!active?.data?.active); }, [clientId]); useEffect(() => { refreshBulkRunning(); const handle = window.setInterval(refreshBulkRunning, 4000); return () => window.clearInterval(handle); }, [refreshBulkRunning]); const isRunning: boolean = !!job && (job.state === 'pending' || job.state === 'processing'); // Gate every action on the audit OR a bulk-improve job being in flight. const actionsBusy: boolean = isRunning || bulkRunning; useEffect(() => { if (!isRunning) return; const handle = window.setInterval(() => { refreshJob(); }, 4000); return () => window.clearInterval(handle); }, [isRunning, refreshJob]); const handleDispatch = async (): Promise => { if (!token || dispatching) return; setDispatching(true); try { const fresh = await dispatchContentCheck(); if (fresh) { const next = new URLSearchParams(); next.set('job', fresh.job_id); setSearchParams(next, { replace: true }); } } finally { setDispatching(false); } }; const handleRerun = async (): Promise => { if (!token || !job || rerunBusy) return; setRerunBusy(true); try { const fresh = await rerunContentCheck(job.job_id); if (fresh) { const next = new URLSearchParams(); next.set('job', fresh.job_id); setSearchParams(next, { replace: true }); } } finally { setRerunBusy(false); } }; /** * Close the scan picker and follow the freshly dispatched job: pinning * ``?job=`` makes the poll above track that job's placeholder row * immediately, mirroring how dispatch / rerun hand off. * * @param {string} startedJobId - Job id returned by the scan-selected call. */ const handleScanStarted = (startedJobId: string): void => { setScanPickerOpen(false); const next = new URLSearchParams(); next.set('job', startedJobId); setSearchParams(next, { replace: true }); }; /** * Load a historical scan picked from the header dropdown. * * @param {string} selectedJobId - Job id chosen in the history list. */ const handleHistorySelect = (selectedJobId: string): void => { if (!selectedJobId || selectedJobId === job?.job_id) return; const next = new URLSearchParams(); next.set('job', selectedJobId); setSearchParams(next); }; if (!token) { return (
); } return (

Catalog Quality

Title and description AEO/GEO readiness, scored per SKU.

{job && (

* Scan products: hand-pick products from your catalog (search by name or SKU) and audit only those · 1 credit per product.

)}
{history.length > 1 && ( )} {job && ( )} {job && ( )} {job && ( )}
{!job && (

No content check has been run yet

Click{' '} Run new check{' '} above to audit {MAX_SCAN_PRODUCTS} random products from your catalog, {CREDITS_PER_PRODUCT_SCAN} credit each. Each title and description is scored individually against AEO and GEO criteria.

)} {job && } {job && (job.csv_url || job.pdf_url) && (
Download report: {job.csv_url && ( CSV )} {job.pdf_url && ( PDF )}
)} {job && ( setBulkScope({ sku, productName })} /> )} {job && ( !next && setBulkScope(null)} jobId={job.job_id} language={job.language_detected || 'en'} productCount={job.total_products} sku={bulkScope?.sku} productName={bulkScope?.productName} /> )} {job && ( )}
); }; function JobOverview({ job }: { job: IContentCheckJob }): JSX.Element { const isCompleted: boolean = job.state === 'completed'; const isRunning: boolean = job.state === 'pending' || job.state === 'processing'; const isFailed: boolean = job.state === 'failed'; const scoreColor: string = job.overall_score >= 80 ? 'text-emerald-600 dark:text-emerald-400' : job.overall_score >= 65 ? 'text-lime-600 dark:text-lime-400' : job.overall_score >= 50 ? 'text-amber-600 dark:text-amber-400' : job.overall_score >= 35 ? 'text-orange-600 dark:text-orange-400' : 'text-red-600 dark:text-red-400'; const totalProducts: number = Math.max(1, job.total_products); const progressPercent: number = Math.max( 0, Math.min(100, Math.round((job.processed_products / totalProducts) * 100)) ); return (
{isRunning ? 'Audit in progress' : isFailed ? 'Audit failed' : 'Latest audit'} {job.language_detected && ( <> · {job.language_detected.toUpperCase()} )}
{isCompleted ? ( <>
{job.overall_score} /100

Audited {job.processed_products} of {job.total_products}{' '} products

) : isFailed ? (

{job.error ?? 'Something went wrong while running this audit.'}

) : (
{job.processed_products} / {job.total_products} products
{progressPercent}%
)}
); } function SeverityChip({ label, count, accent, dotClass, }: { label: string; count: number; accent: string; dotClass: string; }): JSX.Element { return (
{label}
{count}
); } function ProductsTable({ jobId, products, severity, language, disabled, onOpenBulk, }: { jobId: string; products: IContentCheckProductRow[]; severity: ContentCheckSeverity | null; language: string; disabled: boolean; onOpenBulk: (sku?: string, productName?: string) => void; }): JSX.Element { const [searchParams, setSearchParams] = useSearchParams(); const filterTabs = useMemo( () => [null, 'critical', 'warning', 'ok'] as Array, [] ); const handleSelectSeverity = (tab: ContentCheckSeverity | null): void => { const nextParams = new URLSearchParams(searchParams); nextParams.set('job', jobId); if (tab) nextParams.set('severity', tab); else nextParams.delete('severity'); setSearchParams(nextParams); }; return (

Per-product results

{products.length === 0 ? (
No products to show for this filter.
) : (
    {products.map(product => ( ))}
)}
); } function ProductRow({ jobId, product, language, disabled, onOpenBulk, }: { jobId: string; product: IContentCheckProductRow; language: string; disabled: boolean; onOpenBulk: (sku?: string, productName?: string) => void; }): JSX.Element { const [expanded, setExpanded] = useState(false); const [actionMode, setActionMode] = useState<'gap' | 'faq' | null>(null); const issueCount: number = (product.title_issues?.length ?? 0) + (product.description_issues?.length ?? 0); const severityBadge = SEVERITY_BADGE[product.severity]; return (
  • {severityBadge.label} {product.product_name || product.sku}

    SKU {product.sku} {product.detected_product_type && ` · ${product.detected_product_type}`}

    T{' '} {product.title_score} D{' '} {product.description_score} {issueCount > 0 && ( )}
    {expanded && issueCount > 0 && (
    )} !next && setActionMode(null)} mode={actionMode ?? 'gap'} jobId={jobId} sku={product.sku} productName={product.product_name} language={language} titleIssues={product.title_issues ?? []} descriptionIssues={product.description_issues ?? []} />
  • ); } function IssueList({ title, issues, }: { title: string; issues: string[]; }): JSX.Element { if (!issues || issues.length === 0) { return (

    {title}

    No issues found.

    ); } return (

    {title}

      {issues.map((issue, issueIndex) => (
    • · {issue}
    • ))}
    ); } export default CatalogQuality;