ConnectSlicerPage.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { useState } from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { useTranslation } from 'react-i18next';
  4. import { useQuery } from '@tanstack/react-query';
  5. import { Check, Copy, Info, Loader2, MonitorSmartphone, Printer } from 'lucide-react';
  6. import { multiVirtualPrinterApi, type VPConnectPrinter } from '../api/client';
  7. import { Card, CardContent } from '../components/Card';
  8. import { MarkdownContent } from '../components/MarkdownContent';
  9. import { SlicerAccessCodeCard } from '../components/SlicerAccessCodeCard';
  10. import { SlicerCaCertCard } from '../components/SlicerCaCertCard';
  11. import { useAuth } from '../contexts/AuthContext';
  12. import { useToast } from '../contexts/ToastContext';
  13. import { copyTextToClipboard } from '../utils/clipboard';
  14. /**
  15. * Connect slicer (#3602): everything a user enters in Bambu Studio or
  16. * OrcaSlicer to send to Bambuddy's virtual printers, for users without
  17. * Settings access. Their personal access code, the virtual printers they may
  18. * send to (the sign-in's own rule), the CA certificate and the admin's notes.
  19. * Reached from the account menu; its URL can be sent to a new user.
  20. */
  21. export function ConnectSlicerPage() {
  22. const { t } = useTranslation();
  23. const { user, hasPermission } = useAuth();
  24. const { data, isLoading, isError } = useQuery({
  25. // Per user: what they may send to is theirs.
  26. queryKey: ['vp-connect-info', user?.id ?? null],
  27. queryFn: multiVirtualPrinterApi.getConnectInfo,
  28. });
  29. return (
  30. <div className="p-4 md:p-8 space-y-4 max-w-5xl">
  31. <div className="flex items-start gap-3">
  32. <MonitorSmartphone className="w-6 h-6 text-bambu-green flex-shrink-0 mt-1" />
  33. <div>
  34. <h1 className="text-2xl font-bold text-white">{t('connectSlicer.title')}</h1>
  35. <p className="text-sm text-bambu-gray mt-1">{t('connectSlicer.subtitle')}</p>
  36. </div>
  37. </div>
  38. {isLoading ? (
  39. <div className="flex justify-center py-8">
  40. <Loader2 className="w-6 h-6 animate-spin text-bambu-green" />
  41. </div>
  42. ) : isError || !data ? (
  43. <Card>
  44. <CardContent className="py-4 text-sm text-red-600 dark:text-red-400">{t('connectSlicer.loadFailed')}</CardContent>
  45. </Card>
  46. ) : (
  47. <>
  48. {data.instructions.trim() && (
  49. <Card>
  50. <CardContent className="py-3 px-4 text-sm text-bambu-gray-light leading-relaxed">
  51. <div data-testid="connect-slicer-instructions">
  52. <MarkdownContent>{data.instructions}</MarkdownContent>
  53. </div>
  54. </CardContent>
  55. </Card>
  56. )}
  57. {!data.auth_enabled ? (
  58. <Card>
  59. <CardContent className="py-3 px-4">
  60. <div className="flex items-start gap-2 text-sm">
  61. <Info className="w-4 h-4 text-blue-600 dark:text-blue-400 flex-shrink-0 mt-0.5" />
  62. <div>
  63. <p className="text-bambu-gray">{t('connectSlicer.authOff')}</p>
  64. {hasPermission('settings:read') && (
  65. <Link to="/settings?tab=virtual-printer" className="inline-block mt-1 text-bambu-green hover:underline">
  66. {t('connectSlicer.openSettings')}
  67. </Link>
  68. )}
  69. </div>
  70. </div>
  71. </CardContent>
  72. </Card>
  73. ) : (
  74. <>
  75. <ol className="text-sm text-bambu-gray list-decimal pl-5 space-y-1">
  76. <li>{t('connectSlicer.steps.certificate')}</li>
  77. <li>{t('connectSlicer.steps.addPrinter')}</li>
  78. <li>{t('connectSlicer.steps.accessCode')}</li>
  79. </ol>
  80. <SlicerAccessCodeCard />
  81. <div className="flex items-center gap-2 pt-2">
  82. <Printer className="w-5 h-5 text-bambu-green" />
  83. <h2 className="text-lg font-semibold text-white">{t('connectSlicer.printers')}</h2>
  84. </div>
  85. {data.printers.length === 0 ? (
  86. <Card>
  87. <CardContent className="py-6 text-center text-sm text-bambu-gray">{t('connectSlicer.noPrinters')}</CardContent>
  88. </Card>
  89. ) : (
  90. <div className="grid grid-cols-1 md:grid-cols-2 gap-4 items-start">
  91. {data.printers.map((printer) => (
  92. <ConnectPrinterCard key={printer.id} printer={printer} />
  93. ))}
  94. </div>
  95. )}
  96. </>
  97. )}
  98. <SlicerCaCertCard />
  99. </>
  100. )}
  101. </div>
  102. );
  103. }
  104. function ConnectPrinterCard({ printer }: { printer: VPConnectPrinter }) {
  105. const { t } = useTranslation();
  106. const destination =
  107. printer.mode === 'library'
  108. ? t('connectSlicer.destination.library')
  109. : printer.mode === 'review'
  110. ? t('connectSlicer.destination.review')
  111. : printer.target_printer_name
  112. ? t('connectSlicer.destination.queuePrinter', { printer: printer.target_printer_name })
  113. : t('connectSlicer.destination.queueModel', { model: printer.model_name });
  114. return (
  115. <Card>
  116. <CardContent className="py-3 px-4 space-y-2">
  117. <div className="flex flex-wrap items-center gap-2">
  118. <span className="text-white font-medium">{printer.name}</span>
  119. {!printer.running && (
  120. <span className="text-[10px] px-1.5 py-0.5 rounded bg-amber-100 dark:bg-amber-500/20 text-amber-700 dark:text-amber-400">
  121. {t('connectSlicer.notRunning')}
  122. </span>
  123. )}
  124. </div>
  125. <dl className="grid grid-cols-[auto_1fr] gap-x-3 gap-y-1 text-xs">
  126. <CopyRow label={t('connectSlicer.field.address')} value={printer.address} />
  127. <CopyRow label={t('connectSlicer.field.serial')} value={printer.serial} />
  128. <dt className="text-bambu-gray">{t('connectSlicer.field.model')}</dt>
  129. <dd className="text-white">{printer.model_name}</dd>
  130. </dl>
  131. <p className="text-xs text-bambu-gray">{destination}</p>
  132. <p className="text-xs text-bambu-gray">
  133. {printer.target_printer_name
  134. ? t('connectSlicer.liveStatus', { printer: printer.target_printer_name })
  135. : t('connectSlicer.noLiveStatus')}
  136. </p>
  137. </CardContent>
  138. </Card>
  139. );
  140. }
  141. function CopyRow({ label, value }: { label: string; value: string }) {
  142. const { t } = useTranslation();
  143. const { showToast } = useToast();
  144. const [copied, setCopied] = useState(false);
  145. const copy = async () => {
  146. if (await copyTextToClipboard(value)) {
  147. setCopied(true);
  148. setTimeout(() => setCopied(false), 2000);
  149. } else {
  150. showToast(t('slicerAccessCode.copyFailed'), 'error');
  151. }
  152. };
  153. return (
  154. <>
  155. <dt className="text-bambu-gray">{label}</dt>
  156. <dd className="flex items-center gap-1.5 min-w-0">
  157. <code className="font-mono text-white truncate">{value || '-'}</code>
  158. {value && (
  159. <button
  160. onClick={copy}
  161. aria-label={`${t('slicerAccessCode.copy')}: ${label}`}
  162. className="p-0.5 text-bambu-gray hover:text-white flex-shrink-0"
  163. >
  164. {copied ? <Check className="w-3.5 h-3.5 text-bambu-green" /> : <Copy className="w-3.5 h-3.5" />}
  165. </button>
  166. )}
  167. </dd>
  168. </>
  169. );
  170. }