| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- import { useState, useEffect } from 'react';
- import { useTranslation } from 'react-i18next';
- import { useMutation, useQueryClient, useQuery } from '@tanstack/react-query';
- import {
- Loader2, Check, AlertTriangle, Eye, EyeOff, Info,
- ChevronDown, ChevronRight, ArrowRightLeft, Trash2, ShieldCheck, Copy, Stethoscope,
- } from 'lucide-react';
- import { api, multiVirtualPrinterApi } from '../api/client';
- import type { VirtualPrinterConfig } from '../api/client';
- import { Card, CardContent } from './Card';
- import { Button } from './Button';
- import { ConfirmModal } from './ConfirmModal';
- import { VirtualPrinterDiagnosticModal } from './VirtualPrinterDiagnosticModal';
- import { useToast } from '../contexts/ToastContext';
- import { copyTextToClipboard } from '../utils/clipboard';
- type LocalMode = 'archive' | 'review' | 'queue' | 'proxy';
- const MODE_LABELS: Record<string, string> = {
- archive: 'archive',
- review: 'review',
- queue: 'queue',
- proxy: 'proxy',
- };
- // Legacy wire values (`immediate` → `archive`, `print_queue` → `queue`) shipped
- // before the UI labels were aligned with the wire format. Backend migration
- // flips existing rows but the function tolerates either form so a stale fetch
- // doesn't show an unselected mode (#1429 follow-up).
- function normalizeMode(value: string | undefined): LocalMode {
- if (value === 'immediate') return 'archive';
- if (value === 'print_queue' || value === 'queue') return 'queue';
- if (value === 'archive' || value === 'review' || value === 'proxy') return value;
- return 'archive';
- }
- interface VirtualPrinterCardProps {
- printer: VirtualPrinterConfig;
- models: Record<string, string>;
- }
- export function VirtualPrinterCard({ printer, models }: VirtualPrinterCardProps) {
- const { t } = useTranslation();
- const queryClient = useQueryClient();
- const { showToast } = useToast();
- const [expanded, setExpanded] = useState(true);
- const [localEnabled, setLocalEnabled] = useState(printer.enabled);
- const [localName, setLocalName] = useState(printer.name);
- const [localAccessCode, setLocalAccessCode] = useState('');
- const [localMode, setLocalMode] = useState<LocalMode>(normalizeMode(printer.mode));
- const [localTargetPrinterId, setLocalTargetPrinterId] = useState<number | null>(printer.target_printer_id);
- const [localBindIp, setLocalBindIp] = useState(printer.bind_ip || '');
- const [localRemoteInterfaceIp, setLocalRemoteInterfaceIp] = useState(printer.remote_interface_ip || '');
- const [localModel, setLocalModel] = useState(printer.model || '');
- const [localAutoDispatch, setLocalAutoDispatch] = useState(printer.auto_dispatch ?? true);
- const [localQueueForceColorMatch, setLocalQueueForceColorMatch] = useState(printer.queue_force_color_match ?? false);
- const [localGcodeInjection, setLocalGcodeInjection] = useState(printer.gcode_injection ?? false);
- const [localTailscaleDisabled, setLocalTailscaleDisabled] = useState(printer.tailscale_disabled ?? true);
- const [showAccessCode, setShowAccessCode] = useState(false);
- const [pendingAction, setPendingAction] = useState<string | null>(null);
- const [showDeleteConfirm, setShowDeleteConfirm] = useState(false);
- const [showDiagnostic, setShowDiagnostic] = useState(false);
- const [fqdnCopied, setFqdnCopied] = useState(false);
- // Host-level Tailscale identity (same for every VP) — shown inline on the card when
- // the user has marked this VP as "exposed over Tailscale". Cert handling does NOT
- // depend on this toggle; the slicer trusts the bambuddy CA the user imports once.
- const { data: tailscaleStatus } = useQuery({
- queryKey: ['tailscale-status'],
- queryFn: multiVirtualPrinterApi.getTailscaleStatus,
- enabled: !localTailscaleDisabled,
- staleTime: 60_000,
- });
- const tailscaleFqdn = tailscaleStatus?.available ? tailscaleStatus.fqdn : '';
- const tailscaleIp = tailscaleStatus?.available ? tailscaleStatus.tailscale_ips?.[0] ?? '' : '';
- const handleCopyFqdn = async (e: React.MouseEvent) => {
- e.stopPropagation();
- const fqdn = tailscaleFqdn;
- if (!fqdn) return;
- const ok = await copyTextToClipboard(fqdn);
- if (ok) {
- setFqdnCopied(true);
- showToast(t('printers.copied'));
- setTimeout(() => setFqdnCopied(false), 2000);
- } else {
- showToast(t('virtualPrinter.toast.copyFailed'), 'error');
- }
- };
- // Sync local state when props change (e.g., after backend auto-disable)
- useEffect(() => {
- if (!pendingAction) {
- setLocalEnabled(printer.enabled);
- setLocalMode(normalizeMode(printer.mode));
- setLocalName(printer.name);
- setLocalTargetPrinterId(printer.target_printer_id);
- setLocalBindIp(printer.bind_ip || '');
- setLocalRemoteInterfaceIp(printer.remote_interface_ip || '');
- setLocalModel(printer.model || '');
- setLocalAutoDispatch(printer.auto_dispatch ?? true);
- setLocalQueueForceColorMatch(printer.queue_force_color_match ?? false);
- setLocalGcodeInjection(printer.gcode_injection ?? false);
- setLocalTailscaleDisabled(printer.tailscale_disabled ?? true);
- }
- }, [printer, pendingAction]);
- // Fetch printers for dropdown
- const { data: printers } = useQuery({
- queryKey: ['printers'],
- queryFn: api.getPrinters,
- });
- // Fetch network interfaces
- const { data: networkInterfaces } = useQuery({
- queryKey: ['network-interfaces'],
- queryFn: () => api.getNetworkInterfaces().then(res => res.interfaces),
- });
- const updateMutation = useMutation({
- mutationFn: (data: Parameters<typeof multiVirtualPrinterApi.update>[1]) =>
- multiVirtualPrinterApi.update(printer.id, data),
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: ['virtual-printers'] });
- showToast(t('virtualPrinter.toast.updated'));
- setPendingAction(null);
- },
- onError: (error: Error) => {
- showToast(error.message || t('virtualPrinter.toast.failedToUpdate'), 'error');
- setLocalEnabled(printer.enabled);
- setLocalMode(normalizeMode(printer.mode));
- setLocalTargetPrinterId(printer.target_printer_id);
- setLocalBindIp(printer.bind_ip || '');
- setLocalTailscaleDisabled(printer.tailscale_disabled ?? true);
- setPendingAction(null);
- },
- });
- const deleteMutation = useMutation({
- mutationFn: () => multiVirtualPrinterApi.remove(printer.id),
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: ['virtual-printers'] });
- showToast(t('virtualPrinter.toast.deleted'));
- setShowDeleteConfirm(false);
- },
- onError: (error: Error) => {
- showToast(error.message || t('virtualPrinter.toast.failedToDelete'), 'error');
- setShowDeleteConfirm(false);
- },
- });
- const handleToggleEnabled = (e: React.MouseEvent) => {
- e.stopPropagation();
- const newEnabled = !localEnabled;
- if (newEnabled) {
- if (!localBindIp) {
- showToast(t('virtualPrinter.toast.bindIpRequired'), 'error');
- return;
- }
- if (localMode === 'proxy') {
- if (!localTargetPrinterId) {
- showToast(t('virtualPrinter.toast.targetPrinterRequired'), 'error');
- return;
- }
- } else {
- if (!localAccessCode && !printer.access_code_set) {
- showToast(t('virtualPrinter.toast.accessCodeRequired'), 'error');
- return;
- }
- }
- }
- setLocalEnabled(newEnabled);
- setPendingAction('toggle');
- updateMutation.mutate({ enabled: newEnabled });
- };
- const handleNameChange = () => {
- if (!localName.trim()) return;
- setPendingAction('name');
- updateMutation.mutate({ name: localName.trim() });
- };
- const handleAccessCodeChange = () => {
- if (!localAccessCode) {
- showToast(t('virtualPrinter.toast.accessCodeEmpty'), 'error');
- return;
- }
- if (localAccessCode.length !== 8) {
- showToast(t('virtualPrinter.toast.accessCodeLength'), 'error');
- return;
- }
- setPendingAction('accessCode');
- updateMutation.mutate({ access_code: localAccessCode });
- setLocalAccessCode('');
- };
- const handleModeChange = (mode: LocalMode) => {
- setLocalMode(mode);
- setPendingAction('mode');
- updateMutation.mutate({ mode });
- };
- const handleModelChange = (model: string) => {
- setLocalModel(model);
- setPendingAction('model');
- updateMutation.mutate({ model });
- };
- const handleTargetPrinterChange = (printerId: number) => {
- // The new target's access code becomes this VP's access code on the
- // backend write. If the slicer was already bound with the old code,
- // it has to rebind; flag this so the user doesn't sit there confused.
- const previousCode = targetPrinter?.access_code;
- const nextCode = printers?.find(p => p.id === printerId)?.access_code;
- setLocalTargetPrinterId(printerId);
- setPendingAction('targetPrinter');
- updateMutation.mutate(
- { target_printer_id: printerId },
- {
- onSuccess: () => {
- if (previousCode && nextCode && previousCode !== nextCode) {
- showToast(t('virtualPrinter.toast.targetCodeChangedRebind'), 'info');
- }
- },
- },
- );
- };
- const handleRemoteInterfaceChange = (ip: string) => {
- setLocalRemoteInterfaceIp(ip);
- setPendingAction('remoteInterface');
- updateMutation.mutate({ remote_interface_ip: ip });
- };
- const isRunning = printer.status?.running || false;
- const modeLabel = t(`virtualPrinter.mode.${MODE_LABELS[localMode] || 'archive'}`);
- const targetPrinter = printers?.find(p => p.id === localTargetPrinterId);
- const targetPrinterName = targetPrinter?.name;
- // The bridge in non-proxy modes (and the transparent relay in proxy mode)
- // forwards the slicer's auth bytes to the real printer, so the VP's access
- // code is always the target's. When a target is set, the card surfaces the
- // target's code read-only — the user types it into the slicer, but can't
- // diverge it from the printer.
- const inheritsAccessCodeFromTarget = !!localTargetPrinterId;
- const inheritedAccessCode = inheritsAccessCodeFromTarget ? (targetPrinter?.access_code ?? '') : '';
- return (
- <>
- <Card>
- {/* Collapsed header - always visible, clickable to expand */}
- <div
- className="px-4 py-3 flex items-center gap-3 cursor-pointer select-none"
- onClick={() => setExpanded(!expanded)}
- >
- <button className="text-bambu-gray flex-shrink-0">
- {expanded
- ? <ChevronDown className="w-4 h-4" />
- : <ChevronRight className="w-4 h-4" />
- }
- </button>
- <span className={`w-2 h-2 rounded-full flex-shrink-0 ${isRunning ? 'bg-green-400 animate-pulse' : 'bg-gray-500'}`} />
- <span className="text-white font-medium truncate">{printer.name}</span>
- <span className="text-xs text-bambu-gray flex-shrink-0">{modeLabel}</span>
- {printer.model_name && (
- <span className="text-xs text-bambu-gray flex-shrink-0">{printer.model_name}</span>
- )}
- {targetPrinterName && (
- <span className="text-xs text-bambu-gray flex-shrink-0 truncate">
- {localMode === 'proxy' && <ArrowRightLeft className="w-3 h-3 inline mr-1" />}
- {targetPrinterName}
- </span>
- )}
- {localBindIp && (
- <span className="text-[10px] text-bambu-gray flex-shrink-0 font-mono">{localBindIp}</span>
- )}
- {localRemoteInterfaceIp && (
- <span className="text-[10px] text-bambu-gray flex-shrink-0 font-mono">{localRemoteInterfaceIp}</span>
- )}
- <div className="ml-auto flex items-center gap-2 flex-shrink-0" onClick={(e) => e.stopPropagation()}>
- <button
- onClick={handleToggleEnabled}
- disabled={pendingAction === 'toggle'}
- className={`relative w-10 h-5 rounded-full transition-colors ${
- localEnabled ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
- } ${pendingAction === 'toggle' ? 'opacity-50' : ''}`}
- >
- <span
- className={`absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform ${
- localEnabled ? 'translate-x-5' : ''
- }`}
- />
- </button>
- </div>
- </div>
- {/* Expanded content */}
- {expanded && (
- <CardContent className="pt-0 space-y-4">
- <div className="border-t border-bambu-dark-tertiary" />
- {/* Name + delete */}
- <div className="flex items-center gap-2">
- <input
- type="text"
- value={localName}
- onChange={(e) => setLocalName(e.target.value)}
- onBlur={handleNameChange}
- onKeyDown={(e) => e.key === 'Enter' && handleNameChange()}
- 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"
- />
- <button
- onClick={() => setShowDiagnostic(true)}
- className="p-1.5 text-bambu-gray hover:text-bambu-green transition-colors flex-shrink-0"
- title={t('vpDiagnostic.runButton')}
- >
- <Stethoscope className="w-4 h-4" />
- </button>
- <button
- onClick={() => setShowDeleteConfirm(true)}
- className="p-1.5 text-bambu-gray hover:text-red-600 dark:hover:text-red-400 transition-colors flex-shrink-0"
- title={t('common.delete')}
- >
- <Trash2 className="w-4 h-4" />
- </button>
- </div>
- {/* Tailscale identity (host-level) + serial — compact info row.
- Shown only when this VP is marked Tailscale-exposed AND the daemon is up. */}
- <div className="flex items-center gap-2 -mt-2">
- {tailscaleFqdn && (
- <span className="flex items-center gap-1 text-green-700/80 dark:text-green-400/70 min-w-0">
- <ShieldCheck className="w-3.5 h-3.5 flex-shrink-0" />
- <span className="font-mono text-xs truncate">
- {tailscaleIp ? `${tailscaleIp} (${tailscaleFqdn})` : tailscaleFqdn}
- </span>
- <button
- onClick={handleCopyFqdn}
- className="p-0.5 rounded hover:bg-bambu-dark-tertiary text-bambu-gray hover:text-white transition-colors flex-shrink-0"
- title={fqdnCopied ? t('printers.copied') : t('printers.copyToClipboard')}
- >
- {fqdnCopied ? (
- <Check className="w-3.5 h-3.5 text-bambu-green" />
- ) : (
- <Copy className="w-3.5 h-3.5" />
- )}
- </button>
- </span>
- )}
- <span className="text-xs text-bambu-gray font-mono ml-auto flex-shrink-0">{printer.serial}</span>
- </div>
- {/* Mode */}
- <div>
- <div className="text-white text-sm font-medium mb-2">{t('virtualPrinter.mode.title')}</div>
- <div className="grid grid-cols-2 gap-2">
- {(['archive', 'review', 'queue', 'proxy'] as const).map((mode) => (
- <button
- key={mode}
- onClick={() => handleModeChange(mode)}
- disabled={pendingAction === 'mode'}
- className={`p-2 rounded-lg border text-left transition-colors ${
- localMode === mode
- ? mode === 'proxy'
- ? 'border-blue-500 bg-blue-500/10'
- : 'border-bambu-green bg-bambu-green/10'
- : 'border-bambu-dark-tertiary hover:border-bambu-gray'
- }`}
- >
- <div className="flex items-center gap-1.5 text-white text-xs font-medium">
- {mode === 'proxy' && <ArrowRightLeft className="w-3 h-3" />}
- {t(`virtualPrinter.mode.${MODE_LABELS[mode]}`)}
- </div>
- <div className="text-[10px] text-bambu-gray">
- {t(`virtualPrinter.mode.${MODE_LABELS[mode]}Desc`)}
- </div>
- </button>
- ))}
- </div>
- </div>
- {/* Auto-dispatch toggle - only for queue mode */}
- {localMode === 'queue' && (
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="flex items-center justify-between gap-3">
- <div className="min-w-0">
- <div className="text-white text-sm font-medium">{t('virtualPrinter.autoDispatch.title')}</div>
- <div className="text-[10px] text-bambu-gray">{t('virtualPrinter.autoDispatch.description')}</div>
- </div>
- <button
- onClick={() => {
- const newVal = !localAutoDispatch;
- setLocalAutoDispatch(newVal);
- setPendingAction('autoDispatch');
- updateMutation.mutate({ auto_dispatch: newVal });
- }}
- disabled={pendingAction === 'autoDispatch'}
- className={`relative w-10 h-5 rounded-full transition-colors flex-shrink-0 ${
- localAutoDispatch ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
- } ${pendingAction === 'autoDispatch' ? 'opacity-50' : ''}`}
- >
- <span
- className={`absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform ${
- localAutoDispatch ? 'translate-x-5' : ''
- }`}
- />
- </button>
- </div>
- </div>
- )}
- {/* Force-color-match toggle - only for queue mode (#1188) */}
- {localMode === 'queue' && (
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="flex items-center justify-between gap-3">
- <div className="min-w-0">
- <div className="text-white text-sm font-medium">{t('virtualPrinter.queueForceColorMatch.title')}</div>
- <div className="text-[10px] text-bambu-gray">{t('virtualPrinter.queueForceColorMatch.description')}</div>
- </div>
- <button
- onClick={() => {
- const newVal = !localQueueForceColorMatch;
- setLocalQueueForceColorMatch(newVal);
- setPendingAction('queueForceColorMatch');
- updateMutation.mutate({ queue_force_color_match: newVal });
- }}
- disabled={pendingAction === 'queueForceColorMatch'}
- className={`relative w-10 h-5 rounded-full transition-colors flex-shrink-0 ${
- localQueueForceColorMatch ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
- } ${pendingAction === 'queueForceColorMatch' ? 'opacity-50' : ''}`}
- >
- <span
- className={`absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform ${
- localQueueForceColorMatch ? 'translate-x-5' : ''
- }`}
- />
- </button>
- </div>
- </div>
- )}
- {/* G-code injection toggle - only for queue mode (#1516) */}
- {localMode === 'queue' && (
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="flex items-center justify-between gap-3">
- <div className="min-w-0">
- <div className="text-white text-sm font-medium">{t('virtualPrinter.gcodeInjection.title')}</div>
- <div className="text-[10px] text-bambu-gray">{t('virtualPrinter.gcodeInjection.description')}</div>
- </div>
- <button
- onClick={() => {
- const newVal = !localGcodeInjection;
- setLocalGcodeInjection(newVal);
- setPendingAction('gcodeInjection');
- updateMutation.mutate({ gcode_injection: newVal });
- }}
- disabled={pendingAction === 'gcodeInjection'}
- className={`relative w-10 h-5 rounded-full transition-colors flex-shrink-0 ${
- localGcodeInjection ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
- } ${pendingAction === 'gcodeInjection' ? 'opacity-50' : ''}`}
- >
- <span
- className={`absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform ${
- localGcodeInjection ? 'translate-x-5' : ''
- }`}
- />
- </button>
- </div>
- </div>
- )}
- {/* Tailscale toggle */}
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="flex items-center justify-between gap-3">
- <div className="min-w-0">
- <div className="text-white text-sm font-medium">{t('virtualPrinter.tailscaleDisabled.title')}</div>
- <div className="text-[10px] text-bambu-gray">{t('virtualPrinter.tailscaleDisabled.description')}</div>
- </div>
- <button
- onClick={() => {
- const newVal = !localTailscaleDisabled;
- setLocalTailscaleDisabled(newVal);
- setPendingAction('tailscaleDisabled');
- updateMutation.mutate({ tailscale_disabled: newVal });
- }}
- disabled={pendingAction === 'tailscaleDisabled'}
- className={`relative w-10 h-5 rounded-full transition-colors shrink-0 ${
- !localTailscaleDisabled ? 'bg-bambu-green' : 'bg-bambu-dark-tertiary'
- } ${pendingAction === 'tailscaleDisabled' ? 'opacity-50' : ''}`}
- >
- <span
- className={`absolute top-0.5 left-0.5 w-4 h-4 bg-white rounded-full transition-transform ${
- !localTailscaleDisabled ? 'translate-x-5' : ''
- }`}
- />
- </button>
- </div>
- </div>
- {/* Printer Model - for non-proxy modes */}
- {localMode !== 'proxy' && (
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="text-white text-sm font-medium mb-1">{t('virtualPrinter.model.title')}</div>
- <p className="text-xs text-bambu-gray mb-2">{t('virtualPrinter.model.description')}</p>
- <div className="relative">
- <select
- value={localModel}
- onChange={(e) => handleModelChange(e.target.value)}
- disabled={pendingAction === 'model'}
- 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"
- >
- {Object.entries(models).map(([code, name]) => (
- <option key={code} value={code}>{name} ({code})</option>
- ))}
- </select>
- <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
- </div>
- </div>
- )}
- {/* Proxy mode: hint about using target printer's access code */}
- {localMode === 'proxy' && (
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="flex items-start gap-2 p-2 rounded bg-blue-50 border border-blue-300 dark:bg-blue-500/10 dark:border-blue-500/30">
- <Info className="w-4 h-4 text-blue-600 dark:text-blue-400 flex-shrink-0 mt-0.5" />
- <p className="text-xs text-bambu-gray">
- {t('virtualPrinter.proxy.accessCodeHint')}
- </p>
- </div>
- </div>
- )}
- {/* Access Code - only for non-proxy modes */}
- {localMode !== 'proxy' && (
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="flex items-center gap-2 mb-2">
- <div className="text-white text-sm font-medium">{t('virtualPrinter.accessCode.title')}</div>
- {inheritsAccessCodeFromTarget ? (
- <span className="flex items-center gap-1 text-xs text-blue-700 dark:text-blue-400">
- <Info className="w-3 h-3" />
- {t('virtualPrinter.accessCode.inheritedFromTarget')}
- </span>
- ) : printer.access_code_set ? (
- <span className="flex items-center gap-1 text-xs text-green-700 dark:text-green-400">
- <Check className="w-3 h-3" />
- {t('virtualPrinter.accessCode.isSet')}
- </span>
- ) : (
- <span className="flex items-center gap-1 text-xs text-yellow-700 dark:text-yellow-400">
- <AlertTriangle className="w-3 h-3" />
- {t('virtualPrinter.accessCode.notSet')}
- </span>
- )}
- </div>
- {inheritsAccessCodeFromTarget ? (
- <>
- <div className="relative">
- <input
- type={showAccessCode ? 'text' : 'password'}
- value={inheritedAccessCode}
- readOnly
- aria-label={t('virtualPrinter.accessCode.title')}
- className="w-full bg-bambu-dark-secondary border border-bambu-dark-tertiary rounded-md px-3 py-1.5 text-white text-sm pr-10 font-mono opacity-90 cursor-default"
- />
- <button
- onClick={() => setShowAccessCode(!showAccessCode)}
- className="absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white"
- aria-label={showAccessCode ? t('virtualPrinter.accessCode.hide') : t('virtualPrinter.accessCode.reveal')}
- >
- {showAccessCode ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
- </button>
- </div>
- <p className="text-xs text-bambu-gray mt-1">
- {t('virtualPrinter.accessCode.derivedFromTargetHint')}
- </p>
- </>
- ) : (
- <>
- <div className="flex gap-2">
- <div className="relative flex-1">
- <input
- type={showAccessCode ? 'text' : 'password'}
- value={localAccessCode}
- onChange={(e) => setLocalAccessCode(e.target.value)}
- placeholder={printer.access_code_set ? t('virtualPrinter.accessCode.placeholderChange') : t('virtualPrinter.accessCode.placeholder')}
- maxLength={8}
- 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"
- />
- <button
- onClick={() => setShowAccessCode(!showAccessCode)}
- className="absolute right-2 top-1/2 -translate-y-1/2 text-bambu-gray hover:text-white"
- >
- {showAccessCode ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
- </button>
- </div>
- <Button
- onClick={handleAccessCodeChange}
- disabled={!localAccessCode || pendingAction === 'accessCode'}
- variant="primary"
- >
- {pendingAction === 'accessCode' ? <Loader2 className="w-4 h-4 animate-spin" /> : t('common.save')}
- </Button>
- </div>
- {localAccessCode && (
- <p className="text-xs text-bambu-gray mt-1">
- <span className={localAccessCode.length === 8 ? 'text-green-700 dark:text-green-400' : 'text-yellow-700 dark:text-yellow-400'}>
- {t('virtualPrinter.accessCode.charCount', { count: localAccessCode.length })}
- </span>
- </p>
- )}
- </>
- )}
- </div>
- )}
- {/* Target Printer */}
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="text-white text-sm font-medium mb-2">{t('virtualPrinter.targetPrinter.title')}</div>
- <div className="relative">
- <select
- value={localTargetPrinterId ?? ''}
- onChange={(e) => {
- const id = parseInt(e.target.value, 10);
- if (!isNaN(id)) handleTargetPrinterChange(id);
- }}
- disabled={pendingAction === 'targetPrinter'}
- 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"
- >
- <option value="">{t('virtualPrinter.targetPrinter.placeholder')}</option>
- {printers?.map((p) => (
- <option key={p.id} value={p.id}>{p.name} ({p.ip_address})</option>
- ))}
- </select>
- <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
- </div>
- </div>
- {/* Bind Interface */}
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="text-white text-sm font-medium mb-1">{t('virtualPrinter.bindIp.title')}</div>
- <div className="relative">
- <select
- value={localBindIp}
- onChange={(e) => {
- setLocalBindIp(e.target.value);
- setPendingAction('bindIp');
- updateMutation.mutate({ bind_ip: e.target.value });
- }}
- disabled={pendingAction === 'bindIp'}
- 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"
- >
- <option value="">{t('virtualPrinter.bindIp.placeholder')}</option>
- {networkInterfaces?.map((iface) => (
- <option key={iface.ip} value={iface.ip}>
- {iface.name} ({iface.ip}){iface.is_alias ? ' [alias]' : ''} - {iface.subnet}
- </option>
- ))}
- </select>
- <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
- </div>
- <p className="text-xs text-bambu-gray mt-1">{t('virtualPrinter.bindIp.hint')}</p>
- </div>
- {/* Remote Interface - always visible for configuration */}
- <div className="pt-2 border-t border-bambu-dark-tertiary">
- <div className="flex items-center gap-2 mb-1">
- <div className="text-white text-sm font-medium">{t('virtualPrinter.remoteInterface.title')}</div>
- {localRemoteInterfaceIp ? (
- <span className="flex items-center gap-1 text-xs text-green-700 dark:text-green-400"><Check className="w-3 h-3" /></span>
- ) : (
- <span className="flex items-center gap-1 text-xs text-bambu-gray" title={t('virtualPrinter.remoteInterface.optional')}><Info className="w-3 h-3" /></span>
- )}
- </div>
- <div className="relative">
- <select
- value={localRemoteInterfaceIp}
- onChange={(e) => handleRemoteInterfaceChange(e.target.value)}
- disabled={pendingAction === 'remoteInterface'}
- 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"
- >
- <option value="">{t('virtualPrinter.remoteInterface.placeholder')}</option>
- {networkInterfaces?.map((iface) => (
- <option key={iface.ip} value={iface.ip}>
- {iface.name} ({iface.ip}) - {iface.subnet}
- </option>
- ))}
- </select>
- <ChevronDown className="absolute right-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
- </div>
- </div>
- </CardContent>
- )}
- </Card>
- {showDeleteConfirm && (
- <ConfirmModal
- title={t('virtualPrinter.deleteConfirm.title')}
- message={t('virtualPrinter.deleteConfirm.message', { name: printer.name })}
- variant="danger"
- confirmText={t('common.delete')}
- isLoading={deleteMutation.isPending}
- onConfirm={() => deleteMutation.mutate()}
- onCancel={() => setShowDeleteConfirm(false)}
- />
- )}
- {showDiagnostic && (
- <VirtualPrinterDiagnosticModal
- vpId={printer.id}
- vpName={printer.name}
- onClose={() => setShowDiagnostic(false)}
- />
- )}
- </>
- );
- }
|