CameraDiagnoseModal.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import { useEffect } from 'react';
  2. import { useMutation } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import { X, Stethoscope, CheckCircle2, XCircle, MinusCircle, Loader2 } from 'lucide-react';
  5. import { api, type CameraDiagnoseResult, type CameraDiagnoseStage } from '../api/client';
  6. interface CameraDiagnoseModalProps {
  7. printerId: number;
  8. printerName: string | null;
  9. onClose: () => void;
  10. }
  11. function StageIcon({ status }: { status: CameraDiagnoseStage['status'] }) {
  12. if (status === 'ok') return <CheckCircle2 className="w-5 h-5 text-bambu-green flex-shrink-0" />;
  13. if (status === 'failed') return <XCircle className="w-5 h-5 text-red-600 dark:text-red-400 flex-shrink-0" />;
  14. return <MinusCircle className="w-5 h-5 text-bambu-gray flex-shrink-0" />;
  15. }
  16. export function CameraDiagnoseModal({ printerId, printerName, onClose }: CameraDiagnoseModalProps) {
  17. const { t } = useTranslation();
  18. // Kick the diagnostic off as soon as the modal mounts. There's no
  19. // "Start" button — opening the modal IS the test. The mutation
  20. // shape is right here: we want a one-shot POST with isPending /
  21. // data / error, not a cached query.
  22. const diagnose = useMutation({
  23. mutationFn: () => api.diagnoseCamera(printerId),
  24. });
  25. useEffect(() => {
  26. diagnose.mutate();
  27. // Intentionally only on mount — re-running needs the user to click "Retry".
  28. // eslint-disable-next-line react-hooks/exhaustive-deps
  29. }, []);
  30. useEffect(() => {
  31. const handleKeyDown = (e: KeyboardEvent) => {
  32. if (e.key === 'Escape') onClose();
  33. };
  34. window.addEventListener('keydown', handleKeyDown);
  35. return () => window.removeEventListener('keydown', handleKeyDown);
  36. }, [onClose]);
  37. const result = diagnose.data as CameraDiagnoseResult | undefined;
  38. return (
  39. <div
  40. className="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4"
  41. onClick={onClose}
  42. >
  43. <div
  44. className="bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-lg flex flex-col"
  45. onClick={(e) => e.stopPropagation()}
  46. >
  47. <div className="flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary">
  48. <div className="flex items-center gap-2 min-w-0">
  49. <Stethoscope className="w-5 h-5 text-bambu-green flex-shrink-0" />
  50. <h2 className="text-lg font-semibold text-white truncate">
  51. {t('camera.diagnose.modalTitle', { name: printerName || '' })}
  52. </h2>
  53. </div>
  54. <button
  55. onClick={onClose}
  56. className="text-bambu-gray hover:text-white transition-colors"
  57. title={t('common.close')}
  58. >
  59. <X className="w-5 h-5" />
  60. </button>
  61. </div>
  62. <div className="p-6 space-y-4">
  63. {diagnose.isPending && (
  64. <div className="flex items-center gap-2 text-bambu-gray">
  65. <Loader2 className="w-4 h-4 animate-spin" />
  66. <span>{t('camera.diagnose.running')}</span>
  67. </div>
  68. )}
  69. {diagnose.isError && (
  70. <div className="rounded-lg bg-red-50 dark:bg-red-500/10 border border-red-300 dark:border-red-500/30 px-4 py-3 text-sm text-red-700 dark:text-red-300">
  71. {t('camera.diagnose.runFailed', { error: (diagnose.error as Error).message })}
  72. </div>
  73. )}
  74. {result && (
  75. <>
  76. {/* Per-stage results */}
  77. <ol className="space-y-2">
  78. {result.stages.map((stage) => (
  79. <li
  80. key={stage.name}
  81. className="flex items-center gap-3 bg-bambu-dark rounded-lg px-4 py-2.5"
  82. >
  83. <StageIcon status={stage.status} />
  84. <div className="flex-1 min-w-0">
  85. <div className="text-sm text-white">
  86. {t(`camera.diagnose.stage.${stage.name}`)}
  87. </div>
  88. {stage.code && (
  89. <div className="text-xs text-bambu-gray font-mono">{stage.code}</div>
  90. )}
  91. </div>
  92. <div className="text-xs text-bambu-gray tabular-nums flex-shrink-0">
  93. {stage.duration_ms} ms
  94. </div>
  95. </li>
  96. ))}
  97. </ol>
  98. {/* Summary + remediation */}
  99. <div
  100. className={
  101. result.overall_status === 'ok'
  102. ? 'rounded-lg bg-bambu-green/10 border border-bambu-green/30 px-4 py-3 text-sm text-bambu-green'
  103. : 'rounded-lg bg-red-50 dark:bg-red-500/10 border border-red-300 dark:border-red-500/30 px-4 py-3 text-sm text-red-700 dark:text-red-300'
  104. }
  105. >
  106. {t(`camera.diagnose.summary.${result.summary_code}`, {
  107. defaultValue: t('camera.diagnose.summary.unknown_failure'),
  108. })}
  109. </div>
  110. {/* Metadata for support triage */}
  111. <div className="text-xs text-bambu-gray space-y-0.5">
  112. <div>
  113. <span className="text-bambu-gray/60">{t('camera.diagnose.meta.protocol')}: </span>
  114. <span className="font-mono">{result.protocol}</span>
  115. {' • '}
  116. <span className="text-bambu-gray/60">{t('camera.diagnose.meta.port')}: </span>
  117. <span className="font-mono">{result.port}</span>
  118. {' • '}
  119. <span className="text-bambu-gray/60">{t('camera.diagnose.meta.profile')}: </span>
  120. <span className="font-mono">{result.profile}</span>
  121. </div>
  122. </div>
  123. </>
  124. )}
  125. </div>
  126. <div className="px-6 py-4 border-t border-bambu-dark-tertiary flex justify-end gap-2">
  127. <button
  128. onClick={() => diagnose.mutate()}
  129. disabled={diagnose.isPending}
  130. className="px-4 py-2 bg-bambu-dark hover:bg-bambu-dark-tertiary disabled:opacity-50 text-white text-sm rounded-lg transition-colors"
  131. >
  132. {t('camera.diagnose.retry')}
  133. </button>
  134. <button
  135. onClick={onClose}
  136. className="px-4 py-2 bg-bambu-green hover:bg-bambu-green/90 text-white text-sm rounded-lg transition-colors"
  137. >
  138. {t('common.close')}
  139. </button>
  140. </div>
  141. </div>
  142. </div>
  143. );
  144. }