import { useEffect } from 'react'; import { useMutation } from '@tanstack/react-query'; import { useTranslation } from 'react-i18next'; import { X, Stethoscope, CheckCircle2, XCircle, AlertTriangle, MinusCircle, Loader2, } from 'lucide-react'; import { api, type DiagnosticCheck, type DiagnosticStatus, type PrinterDiagnosticResult, } from '../api/client'; function StatusIcon({ status }: { status: DiagnosticStatus }) { if (status === 'pass') return ; if (status === 'fail') return ; if (status === 'warn') return ; return ; } /** * Presentational checklist — renders one row per diagnostic check plus an * overall banner. Shared by the modal and the bug-report panel. The title * and per-status detail text are localized via `diagnostic.check..*`. */ export function DiagnosticChecklist({ result }: { result: PrinterDiagnosticResult }) { const { t } = useTranslation(); const overallClass = result.overall === 'ok' ? 'bg-bambu-green/10 border-bambu-green/30 text-bambu-green' : result.overall === 'warnings' ? 'bg-amber-500/10 border-amber-500/30 text-amber-300' : 'bg-red-500/10 border-red-500/30 text-red-300'; const renderCheck = (check: DiagnosticCheck) => { const detail = t(`diagnostic.check.${check.id}.${check.status}`, { ...check.params, defaultValue: '', }); return (
  • {t(`diagnostic.check.${check.id}.title`)}
    {detail &&
    {detail}
    }
  • ); }; return (
      {result.checks.map(renderCheck)}
    {t(`diagnostic.overall.${result.overall}`)}
    ); } type Connection = { ip_address: string; serial_number?: string; access_code?: string; }; type ConnectionDiagnosticModalProps = { onClose: () => void; printerName?: string | null; } & ({ printerId: number } | { connection: Connection }); /** * Connection diagnostic modal. Opens straight into the test — used from the * printer card, the System page, and the Add-Printer flow on failure. */ export function ConnectionDiagnosticModal(props: ConnectionDiagnosticModalProps) { const { onClose, printerName } = props; const { t } = useTranslation(); const printerId = 'printerId' in props ? props.printerId : undefined; const connection = 'connection' in props ? props.connection : undefined; const diagnose = useMutation({ mutationFn: (): Promise => printerId !== undefined ? api.diagnosePrinter(printerId) : api.diagnoseConnection(connection as Connection), }); useEffect(() => { diagnose.mutate(); // Run once on mount — re-running is the explicit "Retry" button. // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose(); }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [onClose]); const result = diagnose.data as PrinterDiagnosticResult | undefined; return (
    e.stopPropagation()} >

    {t('diagnostic.modalTitle', { name: printerName || '' })}

    {diagnose.isPending && (
    {t('diagnostic.running')}
    )} {diagnose.isError && (
    {t('diagnostic.runFailed', { error: (diagnose.error as Error).message })}
    )} {result && }
    ); }