VirtualPrinterCard.tsx 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. import { useState, useEffect } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
  4. import {
  5. Loader2, Check, AlertTriangle, Eye, EyeOff, Info,
  6. ChevronDown, ChevronRight, ArrowRightLeft, Trash2, ShieldCheck, Copy, Stethoscope,
  7. } from 'lucide-react';
  8. import { api, multiVirtualPrinterApi } from '../api/client';
  9. import type { VirtualPrinterConfig } from '../api/client';
  10. import { Card, CardContent } from './Card';
  11. import { Button } from './Button';
  12. import { ConfirmModal } from './ConfirmModal';
  13. import { VirtualPrinterDiagnosticModal } from './VirtualPrinterDiagnosticModal';
  14. import { useToast } from '../contexts/ToastContext';
  15. import { copyTextToClipboard } from '../utils/clipboard';
  16. type LocalMode = 'immediate' | 'review' | 'print_queue' | 'proxy';
  17. const MODE_LABELS: Record<string, string> = {
  18. immediate: 'archive',
  19. review: 'review',
  20. print_queue: 'queue',
  21. proxy: 'proxy',
  22. };
  23. interface VirtualPrinterCardProps {
  24. printer: VirtualPrinterConfig;
  25. models: Record<string, string>;
  26. }
  27. export function VirtualPrinterCard({ printer, models }: VirtualPrinterCardProps) {
  28. const { t } = useTranslation();
  29. const queryClient = useQueryClient();
  30. const { showToast } = useToast();
  31. const [expanded, setExpanded] = useState(true);
  32. const [localEnabled, setLocalEnabled] = useState(printer.enabled);
  33. const [localName, setLocalName] = useState(printer.name);
  34. const [localAccessCode, setLocalAccessCode] = useState('');
  35. const [localMode, setLocalMode] = useState<LocalMode>(
  36. (printer.mode === 'queue' ? 'review' : printer.mode) as LocalMode
  37. );
  38. const [localTargetPrinterId, setLocalTargetPrinterId] = useState<number | null>(printer.target_printer_id);
  39. const [localBindIp, setLocalBindIp] = useState(printer.bind_ip || '');
  40. const [localRemoteInterfaceIp, setLocalRemoteInterfaceIp] = useState(printer.remote_interface_ip || '');
  41. const [localModel, setLocalModel] = useState(printer.model || '');
  42. const [localAutoDispatch, setLocalAutoDispatch] = useState(printer.auto_dispatch ?? true);
  43. const [localQueueForceColorMatch, setLocalQueueForceColorMatch] = useState(printer.queue_force_color_match ?? false);
  44. const [localTailscaleDisabled, setLocalTailscaleDisabled] = useState(printer.tailscale_disabled ?? true);
  45. const [showAccessCode, setShowAccessCode] = useState(false);
  46. const [pendingAction, setPendingAction] = useState<string | null>(null);
  47. const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
  48. const [showDiagnostic, setShowDiagnostic] = useState(false);
  49. const [fqdnCopied, setFqdnCopied] = useState(false);
  50. // Host-level Tailscale identity (same for every VP) — shown inline on the card when
  51. // the user has marked this VP as "exposed over Tailscale". Cert handling does NOT
  52. // depend on this toggle; the slicer trusts the bambuddy CA the user imports once.
  53. const { data: tailscaleStatus } = useQuery({
  54. queryKey: ['tailscale-status'],
  55. queryFn: multiVirtualPrinterApi.getTailscaleStatus,
  56. enabled: !localTailscaleDisabled,
  57. staleTime: 60_000,
  58. });
  59. const tailscaleFqdn = tailscaleStatus?.available ? tailscaleStatus.fqdn : '';
  60. const tailscaleIp = tailscaleStatus?.available ? tailscaleStatus.tailscale_ips?.[0] ?? '' : '';
  61. const handleCopyFqdn = async (e: React.MouseEvent) => {
  62. e.stopPropagation();
  63. const fqdn = tailscaleFqdn;
  64. if (!fqdn) return;
  65. const ok = await copyTextToClipboard(fqdn);
  66. if (ok) {
  67. setFqdnCopied(true);
  68. showToast(t('printers.copied'));
  69. setTimeout(() => setFqdnCopied(false), 2000);
  70. } else {
  71. showToast(t('virtualPrinter.toast.copyFailed'), 'error');
  72. }
  73. };
  74. // Sync local state when props change (e.g., after backend auto-disable)
  75. useEffect(() => {
  76. if (!pendingAction) {
  77. setLocalEnabled(printer.enabled);
  78. setLocalMode((printer.mode === 'queue' ? 'review' : printer.mode) as LocalMode);
  79. setLocalName(printer.name);
  80. setLocalTargetPrinterId(printer.target_printer_id);
  81. setLocalBindIp(printer.bind_ip || '');
  82. setLocalRemoteInterfaceIp(printer.remote_interface_ip || '');
  83. setLocalModel(printer.model || '');
  84. setLocalAutoDispatch(printer.auto_dispatch ?? true);
  85. setLocalQueueForceColorMatch(printer.queue_force_color_match ?? false);
  86. setLocalTailscaleDisabled(printer.tailscale_disabled ?? true);
  87. }
  88. }, [printer, pendingAction]);
  89. // Fetch printers for dropdown
  90. const { data: printers } = useQuery({
  91. queryKey: ['printers'],
  92. queryFn: api.getPrinters,
  93. });
  94. // Fetch network interfaces
  95. const { data: networkInterfaces } = useQuery({
  96. queryKey: ['network-interfaces'],
  97. queryFn: () => api.getNetworkInterfaces().then(res => res.interfaces),
  98. });
  99. const updateMutation = useMutation({
  100. mutationFn: (data: Parameters<typeof multiVirtualPrinterApi.update>[1]) =>
  101. multiVirtualPrinterApi.update(printer.id, data),
  102. onSuccess: () => {
  103. queryClient.invalidateQueries({ queryKey: ['virtual-printers'] });
  104. showToast(t('virtualPrinter.toast.updated'));
  105. setPendingAction(null);
  106. },
  107. onError: (error: Error) => {
  108. showToast(error.message || t('virtualPrinter.toast.failedToUpdate'), 'error');
  109. setLocalEnabled(printer.enabled);
  110. setLocalMode((printer.mode === 'queue' ? 'review' : printer.mode) as LocalMode);
  111. setLocalTargetPrinterId(printer.target_printer_id);
  112. setLocalBindIp(printer.bind_ip || '');
  113. setLocalTailscaleDisabled(printer.tailscale_disabled ?? true);
  114. setPendingAction(null);
  115. },
  116. });
  117. const deleteMutation = useMutation({
  118. mutationFn: () => multiVirtualPrinterApi.remove(printer.id),
  119. onSuccess: () => {
  120. queryClient.invalidateQueries({ queryKey: ['virtual-printers'] });
  121. showToast(t('virtualPrinter.toast.deleted'));
  122. setShowDeleteConfirm(false);
  123. },
  124. onError: (error: Error) => {
  125. showToast(error.message || t('virtualPrinter.toast.failedToDelete'), 'error');
  126. setShowDeleteConfirm(false);
  127. },
  128. });
  129. const handleToggleEnabled = (e: React.MouseEvent) => {
  130. e.stopPropagation();
  131. const newEnabled = !localEnabled;
  132. if (newEnabled) {
  133. if (!localBindIp) {
  134. showToast(t('virtualPrinter.toast.bindIpRequired'), 'error');
  135. return;
  136. }
  137. if (localMode === 'proxy') {
  138. if (!localTargetPrinterId) {
  139. showToast(t('virtualPrinter.toast.targetPrinterRequired'), 'error');
  140. return;
  141. }
  142. } else {
  143. if (!localAccessCode && !printer.access_code_set) {
  144. showToast(t('virtualPrinter.toast.accessCodeRequired'), 'error');
  145. return;
  146. }
  147. }
  148. }
  149. setLocalEnabled(newEnabled);
  150. setPendingAction('toggle');
  151. updateMutation.mutate({ enabled: newEnabled });
  152. };
  153. const handleNameChange = () => {
  154. if (!localName.trim()) return;
  155. setPendingAction('name');
  156. updateMutation.mutate({ name: localName.trim() });
  157. };
  158. const handleAccessCodeChange = () => {
  159. if (!localAccessCode) {
  160. showToast(t('virtualPrinter.toast.accessCodeEmpty'), 'error');
  161. return;
  162. }
  163. if (localAccessCode.length !== 8) {
  164. showToast(t('virtualPrinter.toast.accessCodeLength'), 'error');
  165. return;
  166. }
  167. setPendingAction('accessCode');
  168. updateMutation.mutate({ access_code: localAccessCode });
  169. setLocalAccessCode('');
  170. };
  171. const handleModeChange = (mode: LocalMode) => {
  172. setLocalMode(mode);
  173. setPendingAction('mode');
  174. updateMutation.mutate({ mode });
  175. };
  176. const handleModelChange = (model: string) => {
  177. setLocalModel(model);
  178. setPendingAction('model');
  179. updateMutation.mutate({ model });
  180. };
  181. const handleTargetPrinterChange = (printerId: number) => {
  182. setLocalTargetPrinterId(printerId);
  183. setPendingAction('targetPrinter');
  184. updateMutation.mutate({ target_printer_id: printerId });
  185. };
  186. const handleRemoteInterfaceChange = (ip: string) => {
  187. setLocalRemoteInterfaceIp(ip);
  188. setPendingAction('remoteInterface');
  189. updateMutation.mutate({ remote_interface_ip: ip });
  190. };
  191. const isRunning = printer.status?.running || false;
  192. const modeLabel = t(`virtualPrinter.mode.${MODE_LABELS[localMode] || 'archive'}`);
  193. const targetPrinterName = printers?.find(p => p.id === localTargetPrinterId)?.name;
  194. return (
  195. <>
  196. <Card>
  197. {/* Collapsed header - always visible, clickable to expand */}
  198. <div
  199. className="px-4 py-3 flex items-center gap-3 cursor-pointer select-none"
  200. onClick={() => setExpanded(!expanded)}
  201. >
  202. <button className="text-bambu-gray flex-shrink-0">
  203. {expanded
  204. ? <ChevronDown className="w-4 h-4" />
  205. : <ChevronRight className="w-4 h-4" />
  206. }
  207. </button>
  208. <span className={`w-2 h-2 rounded-full flex-shrink-0 ${isRunning ? 'bg-green-400 animate-pulse' : 'bg-gray-500'}`} />
  209. <span className="text-white font-medium truncate">{printer.name}</span>
  210. <span className="text-xs text-bambu-gray flex-shrink-0">{modeLabel}</span>
  211. {printer.model_name && (
  212. <span className="text-xs text-bambu-gray flex-shrink-0">{printer.model_name}</span>
  213. )}
  214. {targetPrinterName && (
  215. <span className="text-xs text-bambu-gray flex-shrink-0 truncate">
  216. {localMode === 'proxy' && <ArrowRightLeft className="w-3 h-3 inline mr-1" />}
  217. {targetPrinterName}
  218. </span>
  219. )}
  220. {localBindIp && (
  221. <span className="text-[10px] text-bambu-gray flex-shrink-0 font-mono">{localBindIp}</span>
  222. )}
  223. {localRemoteInterfaceIp && (
  224. <span className="text-[10px] text-bambu-gray flex-shrink-0 font-mono">{localRemoteInterfaceIp}</span>
  225. )}
  226. <div className="ml-auto flex items-center gap-2 flex-shrink-0" onClick={(e) => e.stopPropagation()}>
  227. <button
  228. onClick={handleToggleEnabled}
  229. disabled={pendingAction === 'toggle'}
  230. className={`relative w-10 h-5 rounded-full transition-colors ${
  231. localEnabled ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
  232. } ${pendingAction === 'toggle' ? 'opacity-50' : ''}`}
  233. >
  234. <span
  235. className={`absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform ${
  236. localEnabled ? 'translate-x-5' : ''
  237. }`}
  238. />
  239. </button>
  240. </div>
  241. </div>
  242. {/* Expanded content */}
  243. {expanded && (
  244. <CardContent className="pt-0 space-y-4">
  245. <div className="border-t border-bambu-dark-tertiary" />
  246. {/* Name + delete */}
  247. <div className="flex items-center gap-2">
  248. <input
  249. type="text"
  250. value={localName}
  251. onChange={(e) => setLocalName(e.target.value)}
  252. onBlur={handleNameChange}
  253. onKeyDown={(e) => e.key === 'Enter' && handleNameChange()}
  254. className="flex-1 text-sm text-white bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-1.5 focus:border-bambu-green focus:outline-none"
  255. />
  256. <button
  257. onClick={() => setShowDiagnostic(true)}
  258. className="p-1.5 text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0"
  259. title={t('vpDiagnostic.runButton')}
  260. >
  261. <Stethoscope className="w-4 h-4" />
  262. </button>
  263. <button
  264. onClick={() => setShowDeleteConfirm(true)}
  265. className="p-1.5 text-bambu-gray hover:text-red-400 transition-colors flex-shrink-0"
  266. title={t('common.delete')}
  267. >
  268. <Trash2 className="w-4 h-4" />
  269. </button>
  270. </div>
  271. {/* Tailscale identity (host-level) + serial — compact info row.
  272. Shown only when this VP is marked Tailscale-exposed AND the daemon is up. */}
  273. <div className="flex items-center gap-2 -mt-2">
  274. {tailscaleFqdn && (
  275. <span className="flex items-center gap-1 text-green-400/70 min-w-0">
  276. <ShieldCheck className="w-3.5 h-3.5 flex-shrink-0" />
  277. <span className="font-mono text-xs truncate">
  278. {tailscaleIp ? `${tailscaleIp} (${tailscaleFqdn})` : tailscaleFqdn}
  279. </span>
  280. <button
  281. onClick={handleCopyFqdn}
  282. className="p-0.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white transition-colors flex-shrink-0"
  283. title={fqdnCopied ? t('printers.copied') : t('printers.copyToClipboard')}
  284. >
  285. {fqdnCopied ? (
  286. <Check className="w-3.5 h-3.5 text-bambu-green" />
  287. ) : (
  288. <Copy className="w-3.5 h-3.5" />
  289. )}
  290. </button>
  291. </span>
  292. )}
  293. <span className="text-xs text-bambu-gray font-mono ml-auto flex-shrink-0">{printer.serial}</span>
  294. </div>
  295. {/* Mode */}
  296. <div>
  297. <div className="text-white text-sm font-medium mb-2">{t('virtualPrinter.mode.title')}</div>
  298. <div className="grid grid-cols-2 gap-2">
  299. {(['immediate', 'review', 'print_queue', 'proxy'] as const).map((mode) => (
  300. <button
  301. key={mode}
  302. onClick={() => handleModeChange(mode)}
  303. disabled={pendingAction === 'mode'}
  304. className={`p-2 rounded-lg border text-left transition-colors ${
  305. localMode === mode
  306. ? mode === 'proxy'
  307. ? 'border-blue-500 bg-blue-500/10'
  308. : 'border-bambu-green bg-bambu-green/10'
  309. : 'border-bambu-dark-tertiary hover:border-bambu-gray'
  310. }`}
  311. >
  312. <div className="flex items-center gap-1.5 text-white text-xs font-medium">
  313. {mode === 'proxy' && <ArrowRightLeft className="w-3 h-3" />}
  314. {t(`virtualPrinter.mode.${MODE_LABELS[mode]}`)}
  315. </div>
  316. <div className="text-[10px] text-bambu-gray">
  317. {t(`virtualPrinter.mode.${MODE_LABELS[mode]}Desc`)}
  318. </div>
  319. </button>
  320. ))}
  321. </div>
  322. </div>
  323. {/* Auto-dispatch toggle - only for print_queue mode */}
  324. {localMode === 'print_queue' && (
  325. <div className="pt-2 border-t border-bambu-dark-tertiary">
  326. <div className="flex items-center justify-between gap-3">
  327. <div className="min-w-0">
  328. <div className="text-white text-sm font-medium">{t('virtualPrinter.autoDispatch.title')}</div>
  329. <div className="text-[10px] text-bambu-gray">{t('virtualPrinter.autoDispatch.description')}</div>
  330. </div>
  331. <button
  332. onClick={() => {
  333. const newVal = !localAutoDispatch;
  334. setLocalAutoDispatch(newVal);
  335. setPendingAction('autoDispatch');
  336. updateMutation.mutate({ auto_dispatch: newVal });
  337. }}
  338. disabled={pendingAction === 'autoDispatch'}
  339. className={`relative w-10 h-5 rounded-full transition-colors flex-shrink-0 ${
  340. localAutoDispatch ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
  341. } ${pendingAction === 'autoDispatch' ? 'opacity-50' : ''}`}
  342. >
  343. <span
  344. className={`absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform ${
  345. localAutoDispatch ? 'translate-x-5' : ''
  346. }`}
  347. />
  348. </button>
  349. </div>
  350. </div>
  351. )}
  352. {/* Force-color-match toggle - only for print_queue mode (#1188) */}
  353. {localMode === 'print_queue' && (
  354. <div className="pt-2 border-t border-bambu-dark-tertiary">
  355. <div className="flex items-center justify-between gap-3">
  356. <div className="min-w-0">
  357. <div className="text-white text-sm font-medium">{t('virtualPrinter.queueForceColorMatch.title')}</div>
  358. <div className="text-[10px] text-bambu-gray">{t('virtualPrinter.queueForceColorMatch.description')}</div>
  359. </div>
  360. <button
  361. onClick={() => {
  362. const newVal = !localQueueForceColorMatch;
  363. setLocalQueueForceColorMatch(newVal);
  364. setPendingAction('queueForceColorMatch');
  365. updateMutation.mutate({ queue_force_color_match: newVal });
  366. }}
  367. disabled={pendingAction === 'queueForceColorMatch'}
  368. className={`relative w-10 h-5 rounded-full transition-colors flex-shrink-0 ${
  369. localQueueForceColorMatch ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
  370. } ${pendingAction === 'queueForceColorMatch' ? 'opacity-50' : ''}`}
  371. >
  372. <span
  373. className={`absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform ${
  374. localQueueForceColorMatch ? 'translate-x-5' : ''
  375. }`}
  376. />
  377. </button>
  378. </div>
  379. </div>
  380. )}
  381. {/* Tailscale toggle */}
  382. <div className="pt-2 border-t border-bambu-dark-tertiary">
  383. <div className="flex items-center justify-between gap-3">
  384. <div className="min-w-0">
  385. <div className="text-white text-sm font-medium">{t('virtualPrinter.tailscaleDisabled.title')}</div>
  386. <div className="text-[10px] text-bambu-gray">{t('virtualPrinter.tailscaleDisabled.description')}</div>
  387. </div>
  388. <button
  389. onClick={() => {
  390. const newVal = !localTailscaleDisabled;
  391. setLocalTailscaleDisabled(newVal);
  392. setPendingAction('tailscaleDisabled');
  393. updateMutation.mutate({ tailscale_disabled: newVal });
  394. }}
  395. disabled={pendingAction === 'tailscaleDisabled'}
  396. className={`relative w-10 h-5 rounded-full transition-colors shrink-0 ${
  397. !localTailscaleDisabled ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
  398. } ${pendingAction === 'tailscaleDisabled' ? 'opacity-50' : ''}`}
  399. >
  400. <span
  401. className={`absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform ${
  402. !localTailscaleDisabled ? 'translate-x-5' : ''
  403. }`}
  404. />
  405. </button>
  406. </div>
  407. </div>
  408. {/* Printer Model - for non-proxy modes */}
  409. {localMode !== 'proxy' && (
  410. <div className="pt-2 border-t border-bambu-dark-tertiary">
  411. <div className="text-white text-sm font-medium mb-1">{t('virtualPrinter.model.title')}</div>
  412. <p className="text-xs text-bambu-gray mb-2">{t('virtualPrinter.model.description')}</p>
  413. <div className="relative">
  414. <select
  415. value={localModel}
  416. onChange={(e) => handleModelChange(e.target.value)}
  417. disabled={pendingAction === 'model'}
  418. className="w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-1.5 text-white text-sm appearance-none cursor-pointer disabled:opacity-50 pr-10"
  419. >
  420. {Object.entries(models).map(([code, name]) => (
  421. <option key={code} value={code}>{name} ({code})</option>
  422. ))}
  423. </select>
  424. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  425. </div>
  426. </div>
  427. )}
  428. {/* Proxy mode: hint about using target printer's access code */}
  429. {localMode === 'proxy' && (
  430. <div className="pt-2 border-t border-bambu-dark-tertiary">
  431. <div className="flex items-start gap-2 p-2 rounded bg-blue-500/10 border border-blue-500/30">
  432. <Info className="w-4 h-4 text-blue-400 flex-shrink-0 mt-0.5" />
  433. <p className="text-xs text-bambu-gray">
  434. {t('virtualPrinter.proxy.accessCodeHint')}
  435. </p>
  436. </div>
  437. </div>
  438. )}
  439. {/* Access Code - only for non-proxy modes */}
  440. {localMode !== 'proxy' && (
  441. <div className="pt-2 border-t border-bambu-dark-tertiary">
  442. <div className="flex items-center gap-2 mb-2">
  443. <div className="text-white text-sm font-medium">{t('virtualPrinter.accessCode.title')}</div>
  444. {printer.access_code_set ? (
  445. <span className="flex items-center gap-1 text-xs text-green-400">
  446. <Check className="w-3 h-3" />
  447. {t('virtualPrinter.accessCode.isSet')}
  448. </span>
  449. ) : (
  450. <span className="flex items-center gap-1 text-xs text-yellow-400">
  451. <AlertTriangle className="w-3 h-3" />
  452. {t('virtualPrinter.accessCode.notSet')}
  453. </span>
  454. )}
  455. </div>
  456. <div className="flex gap-2">
  457. <div className="relative flex-1">
  458. <input
  459. type={showAccessCode ? 'text' : 'password'}
  460. value={localAccessCode}
  461. onChange={(e) => setLocalAccessCode(e.target.value)}
  462. placeholder={printer.access_code_set ? t('virtualPrinter.accessCode.placeholderChange') : t('virtualPrinter.accessCode.placeholder')}
  463. maxLength={8}
  464. className="w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-1.5 text-white text-sm placeholder-bambu-gray pr-10 font-mono"
  465. />
  466. <button
  467. onClick={() => setShowAccessCode(!showAccessCode)}
  468. className="absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white"
  469. >
  470. {showAccessCode ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
  471. </button>
  472. </div>
  473. <Button
  474. onClick={handleAccessCodeChange}
  475. disabled={!localAccessCode || pendingAction === 'accessCode'}
  476. variant="primary"
  477. >
  478. {pendingAction === 'accessCode' ? <Loader2 className="w-4 h-4 animate-spin" /> : t('common.save')}
  479. </Button>
  480. </div>
  481. {localAccessCode && (
  482. <p className="text-xs text-bambu-gray mt-1">
  483. <span className={localAccessCode.length === 8 ? 'text-green-400' : 'text-yellow-400'}>
  484. {t('virtualPrinter.accessCode.charCount', { count: localAccessCode.length })}
  485. </span>
  486. </p>
  487. )}
  488. </div>
  489. )}
  490. {/* Target Printer */}
  491. <div className="pt-2 border-t border-bambu-dark-tertiary">
  492. <div className="text-white text-sm font-medium mb-2">{t('virtualPrinter.targetPrinter.title')}</div>
  493. <div className="relative">
  494. <select
  495. value={localTargetPrinterId ?? ''}
  496. onChange={(e) => {
  497. const id = parseInt(e.target.value, 10);
  498. if (!isNaN(id)) handleTargetPrinterChange(id);
  499. }}
  500. disabled={pendingAction === 'targetPrinter'}
  501. className="w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-1.5 text-white text-sm appearance-none cursor-pointer disabled:opacity-50 pr-10"
  502. >
  503. <option value="">{t('virtualPrinter.targetPrinter.placeholder')}</option>
  504. {printers?.map((p) => (
  505. <option key={p.id} value={p.id}>{p.name} ({p.ip_address})</option>
  506. ))}
  507. </select>
  508. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  509. </div>
  510. </div>
  511. {/* Bind Interface */}
  512. <div className="pt-2 border-t border-bambu-dark-tertiary">
  513. <div className="text-white text-sm font-medium mb-1">{t('virtualPrinter.bindIp.title')}</div>
  514. <div className="relative">
  515. <select
  516. value={localBindIp}
  517. onChange={(e) => {
  518. setLocalBindIp(e.target.value);
  519. setPendingAction('bindIp');
  520. updateMutation.mutate({ bind_ip: e.target.value });
  521. }}
  522. disabled={pendingAction === 'bindIp'}
  523. className="w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-1.5 text-white text-sm appearance-none cursor-pointer disabled:opacity-50 pr-10"
  524. >
  525. <option value="">{t('virtualPrinter.bindIp.placeholder')}</option>
  526. {networkInterfaces?.map((iface) => (
  527. <option key={iface.ip} value={iface.ip}>
  528. {iface.name} ({iface.ip}){iface.is_alias ? ' [alias]' : ''} - {iface.subnet}
  529. </option>
  530. ))}
  531. </select>
  532. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  533. </div>
  534. <p className="text-xs text-bambu-gray mt-1">{t('virtualPrinter.bindIp.hint')}</p>
  535. </div>
  536. {/* Remote Interface - always visible for configuration */}
  537. <div className="pt-2 border-t border-bambu-dark-tertiary">
  538. <div className="flex items-center gap-2 mb-1">
  539. <div className="text-white text-sm font-medium">{t('virtualPrinter.remoteInterface.title')}</div>
  540. {localRemoteInterfaceIp ? (
  541. <span className="flex items-center gap-1 text-xs text-green-400"><Check className="w-3 h-3" /></span>
  542. ) : (
  543. <span className="flex items-center gap-1 text-xs text-bambu-gray" title={t('virtualPrinter.remoteInterface.optional')}><Info className="w-3 h-3" /></span>
  544. )}
  545. </div>
  546. <div className="relative">
  547. <select
  548. value={localRemoteInterfaceIp}
  549. onChange={(e) => handleRemoteInterfaceChange(e.target.value)}
  550. disabled={pendingAction === 'remoteInterface'}
  551. className="w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-1.5 text-white text-sm appearance-none cursor-pointer disabled:opacity-50 pr-10"
  552. >
  553. <option value="">{t('virtualPrinter.remoteInterface.placeholder')}</option>
  554. {networkInterfaces?.map((iface) => (
  555. <option key={iface.ip} value={iface.ip}>
  556. {iface.name} ({iface.ip}) - {iface.subnet}
  557. </option>
  558. ))}
  559. </select>
  560. <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  561. </div>
  562. </div>
  563. </CardContent>
  564. )}
  565. </Card>
  566. {showDeleteConfirm && (
  567. <ConfirmModal
  568. title={t('virtualPrinter.deleteConfirm.title')}
  569. message={t('virtualPrinter.deleteConfirm.message', { name: printer.name })}
  570. variant="danger"
  571. confirmText={t('common.delete')}
  572. isLoading={deleteMutation.isPending}
  573. onConfirm={() => deleteMutation.mutate()}
  574. onCancel={() => setShowDeleteConfirm(false)}
  575. />
  576. )}
  577. {showDiagnostic && (
  578. <VirtualPrinterDiagnosticModal
  579. vpId={printer.id}
  580. vpName={printer.name}
  581. onClose={() => setShowDiagnostic(false)}
  582. />
  583. )}
  584. </>
  585. );
  586. }