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 { multiVirtualPrinterApi, type VPDiagnosticCheck, type VPDiagnosticStatus, type VPDiagnosticResult, } from '../api/client'; function StatusIcon({ status }: { status: VPDiagnosticStatus }) { if (status === 'pass') return ; if (status === 'fail') return ; if (status === 'warn') return ; return ; } /** * Setup-check modal for a single virtual printer. Opens straight into the * check (run on mount); "Run again" re-runs it. Each row's title and fix * text are localized via `vpDiagnostic.check..*`. */ export function VirtualPrinterDiagnosticModal({ vpId, vpName, onClose, }: { vpId: number; vpName: string; onClose: () => void; }) { const { t } = useTranslation(); const diagnose = useMutation({ mutationFn: (): Promise => multiVirtualPrinterApi.diagnose(vpId), }); useEffect(() => { diagnose.mutate(); // Run once on mount — re-running is the explicit "Run again" 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; 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: VPDiagnosticCheck) => { const detail = t(`vpDiagnostic.check.${check.id}.${check.status}`, { ...check.params, defaultValue: '', }); return (
  • {t(`vpDiagnostic.check.${check.id}.title`, check.params)}
    {detail &&
    {detail}
    }
  • ); }; return (
    e.stopPropagation()} >

    {t('vpDiagnostic.title', { name: vpName })}

    {diagnose.isPending && (
    {t('vpDiagnostic.running')}
    )} {diagnose.isError && (
    {t('vpDiagnostic.runFailed', { error: (diagnose.error as Error).message })}
    )} {result && (
      {result.checks.map(renderCheck)}
    {t(`vpDiagnostic.overall.${result.overall}`)}
    )}
    ); }