ConnectionDiagnostic.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import { useEffect } from 'react';
  2. import { useMutation } from '@tanstack/react-query';
  3. import { useTranslation } from 'react-i18next';
  4. import {
  5. X,
  6. Stethoscope,
  7. CheckCircle2,
  8. XCircle,
  9. AlertTriangle,
  10. MinusCircle,
  11. Loader2,
  12. } from 'lucide-react';
  13. import {
  14. api,
  15. type DiagnosticCheck,
  16. type DiagnosticStatus,
  17. type PrinterDiagnosticResult,
  18. } from '../api/client';
  19. function StatusIcon({ status }: { status: DiagnosticStatus }) {
  20. if (status === 'pass') return <CheckCircle2 className="w-5 h-5 text-bambu-green flex-shrink-0" />;
  21. if (status === 'fail') return <XCircle className="w-5 h-5 text-red-400 flex-shrink-0" />;
  22. if (status === 'warn') return <AlertTriangle className="w-5 h-5 text-amber-400 flex-shrink-0" />;
  23. return <MinusCircle className="w-5 h-5 text-bambu-gray flex-shrink-0" />;
  24. }
  25. /**
  26. * Presentational checklist — renders one row per diagnostic check plus an
  27. * overall banner. Shared by the modal and the bug-report panel. The title
  28. * and per-status detail text are localized via `diagnostic.check.<id>.*`.
  29. */
  30. export function DiagnosticChecklist({ result }: { result: PrinterDiagnosticResult }) {
  31. const { t } = useTranslation();
  32. const overallClass =
  33. result.overall === 'ok'
  34. ? 'bg-bambu-green/10 border-bambu-green/30 text-bambu-green'
  35. : result.overall === 'warnings'
  36. ? 'bg-amber-500/10 border-amber-500/30 text-amber-300'
  37. : 'bg-red-500/10 border-red-500/30 text-red-300';
  38. const renderCheck = (check: DiagnosticCheck) => {
  39. const detail = t(`diagnostic.check.${check.id}.${check.status}`, {
  40. ...check.params,
  41. defaultValue: '',
  42. });
  43. return (
  44. <li
  45. key={check.id}
  46. className={`flex items-start gap-3 bg-bambu-dark rounded-lg px-4 py-2.5 ${
  47. check.status === 'skip' ? 'opacity-60' : ''
  48. }`}
  49. >
  50. <div className="mt-0.5">
  51. <StatusIcon status={check.status} />
  52. </div>
  53. <div className="flex-1 min-w-0">
  54. <div className="text-sm text-white">{t(`diagnostic.check.${check.id}.title`)}</div>
  55. {detail && <div className="text-xs text-bambu-gray mt-0.5">{detail}</div>}
  56. </div>
  57. </li>
  58. );
  59. };
  60. return (
  61. <div className="space-y-4">
  62. <ol className="space-y-2">{result.checks.map(renderCheck)}</ol>
  63. <div className={`rounded-lg border px-4 py-3 text-sm ${overallClass}`}>
  64. {t(`diagnostic.overall.${result.overall}`)}
  65. </div>
  66. </div>
  67. );
  68. }
  69. type Connection = {
  70. ip_address: string;
  71. serial_number?: string;
  72. access_code?: string;
  73. };
  74. type ConnectionDiagnosticModalProps = {
  75. onClose: () => void;
  76. printerName?: string | null;
  77. } & ({ printerId: number } | { connection: Connection });
  78. /**
  79. * Connection diagnostic modal. Opens straight into the test — used from the
  80. * printer card, the System page, and the Add-Printer flow on failure.
  81. */
  82. export function ConnectionDiagnosticModal(props: ConnectionDiagnosticModalProps) {
  83. const { onClose, printerName } = props;
  84. const { t } = useTranslation();
  85. const printerId = 'printerId' in props ? props.printerId : undefined;
  86. const connection = 'connection' in props ? props.connection : undefined;
  87. const diagnose = useMutation({
  88. mutationFn: (): Promise<PrinterDiagnosticResult> =>
  89. printerId !== undefined
  90. ? api.diagnosePrinter(printerId)
  91. : api.diagnoseConnection(connection as Connection),
  92. });
  93. useEffect(() => {
  94. diagnose.mutate();
  95. // Run once on mount — re-running is the explicit "Retry" button.
  96. // eslint-disable-next-line react-hooks/exhaustive-deps
  97. }, []);
  98. useEffect(() => {
  99. const handleKeyDown = (e: KeyboardEvent) => {
  100. if (e.key === 'Escape') onClose();
  101. };
  102. window.addEventListener('keydown', handleKeyDown);
  103. return () => window.removeEventListener('keydown', handleKeyDown);
  104. }, [onClose]);
  105. const result = diagnose.data as PrinterDiagnosticResult | undefined;
  106. return (
  107. <div className="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4" onClick={onClose}>
  108. <div
  109. className="bg-bambu-dark-secondary rounded-xl border border-bambu-dark-tertiary w-full max-w-lg flex flex-col max-h-[85vh]"
  110. onClick={(e) => e.stopPropagation()}
  111. >
  112. <div className="flex items-center justify-between px-6 py-4 border-b border-bambu-dark-tertiary">
  113. <div className="flex items-center gap-2 min-w-0">
  114. <Stethoscope className="w-5 h-5 text-bambu-green flex-shrink-0" />
  115. <h2 className="text-lg font-semibold text-white truncate">
  116. {t('diagnostic.modalTitle', { name: printerName || '' })}
  117. </h2>
  118. </div>
  119. <button
  120. onClick={onClose}
  121. className="text-bambu-gray hover:text-white transition-colors"
  122. title={t('common.close')}
  123. >
  124. <X className="w-5 h-5" />
  125. </button>
  126. </div>
  127. <div className="p-6 space-y-4 overflow-y-auto">
  128. {diagnose.isPending && (
  129. <div className="flex items-center gap-2 text-bambu-gray">
  130. <Loader2 className="w-4 h-4 animate-spin" />
  131. <span>{t('diagnostic.running')}</span>
  132. </div>
  133. )}
  134. {diagnose.isError && (
  135. <div className="rounded-lg bg-red-500/10 border border-red-500/30 px-4 py-3 text-sm text-red-300">
  136. {t('diagnostic.runFailed', { error: (diagnose.error as Error).message })}
  137. </div>
  138. )}
  139. {result && <DiagnosticChecklist result={result} />}
  140. </div>
  141. <div className="px-6 py-4 border-t border-bambu-dark-tertiary flex justify-end gap-2">
  142. <button
  143. onClick={() => diagnose.mutate()}
  144. disabled={diagnose.isPending}
  145. className="px-4 py-2 bg-bambu-dark hover:bg-bambu-dark-tertiary disabled:opacity-50 text-white text-sm rounded-lg transition-colors"
  146. >
  147. {t('diagnostic.retry')}
  148. </button>
  149. <button
  150. onClick={onClose}
  151. className="px-4 py-2 bg-bambu-green hover:bg-bambu-green/90 text-white text-sm rounded-lg transition-colors"
  152. >
  153. {t('common.close')}
  154. </button>
  155. </div>
  156. </div>
  157. </div>
  158. );
  159. }