/** * Serial Port Connect Modal * Allows users to configure and connect to a serial port */ import { ChevronDown, ChevronUp, Cpu, RefreshCw, Usb } from 'lucide-react'; import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useI18n } from '../application/i18n/I18nProvider'; import { useTerminalBackend } from '../application/state/useTerminalBackend'; import type { SerialConfig, SerialFlowControl, SerialParity } from '../domain/models'; import { cn } from '../lib/utils'; import { Button } from './ui/button'; import { Combobox, type ComboboxOption } from './ui/combobox'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from './ui/dialog'; import { Label } from './ui/label'; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from './ui/collapsible'; interface SerialPort { path: string; manufacturer: string; serialNumber: string; vendorId: string; productId: string; pnpId: string; type?: 'hardware' | 'pseudo' | 'custom'; } interface SerialConnectModalProps { open: boolean; onClose: () => void; onConnect: (config: SerialConfig) => void; } const BAUD_RATES = [300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600]; const DATA_BITS: Array<5 | 6 | 7 | 8> = [5, 6, 7, 8]; const STOP_BITS: Array<1 | 1.5 | 2> = [1, 1.5, 2]; const PARITY_OPTIONS: SerialParity[] = ['none', 'even', 'odd', 'mark', 'space']; const FLOW_CONTROL_OPTIONS: SerialFlowControl[] = ['none', 'xon/xoff', 'rts/cts']; export const SerialConnectModal: React.FC = ({ open, onClose, onConnect, }) => { const { t } = useI18n(); const [ports, setPorts] = useState([]); const [isLoadingPorts, setIsLoadingPorts] = useState(false); const [showAdvanced, setShowAdvanced] = useState(false); // Form state const [selectedPort, setSelectedPort] = useState(''); const [baudRate, setBaudRate] = useState(115200); const [dataBits, setDataBits] = useState<5 | 6 | 7 | 8>(8); const [stopBits, setStopBits] = useState<1 | 1.5 | 2>(1); const [parity, setParity] = useState('none'); const [flowControl, setFlowControl] = useState('none'); const [localEcho, setLocalEcho] = useState(false); const [lineMode, setLineMode] = useState(false); const terminalBackend = useTerminalBackend(); const loadPorts = useCallback(async () => { setIsLoadingPorts(true); try { const result = await terminalBackend.listSerialPorts(); setPorts(result); // Auto-select first port if available and no port is selected if (result.length > 0) { setSelectedPort((prev) => prev || result[0].path); } } catch (err) { console.error('[Serial] Failed to list ports:', err); } finally { setIsLoadingPorts(false); } }, [terminalBackend]); useEffect(() => { if (open) { loadPorts(); } }, [open]); // eslint-disable-line react-hooks/exhaustive-deps const handleConnect = () => { if (!selectedPort) return; const config: SerialConfig = { path: selectedPort, baudRate, dataBits, stopBits, parity, flowControl, localEcho, lineMode, }; onConnect(config); onClose(); }; // Convert ports to Combobox options const portOptions: ComboboxOption[] = useMemo(() => { return ports.map((port) => ({ value: port.path, label: port.path, sublabel: port.manufacturer || undefined, })); }, [ports]); // Validate: port path must start with /dev/ const isPortValid = selectedPort.trim().startsWith('/dev/'); const isBaudRateValid = BAUD_RATES.includes(baudRate); const isValid = isPortValid && isBaudRateValid; return ( !isOpen && onClose()}> {t('serial.modal.title')} {t('serial.modal.desc')}
{/* Serial Port Selection */}
{/* Combobox for port selection with manual input support */} } /> {!isPortValid && selectedPort && (

{t('serial.field.customPortPlaceholder')}

)}
{/* Baud Rate */}
{/* Advanced Options */} {/* Data Bits */}
{/* Stop Bits */}
{/* Parity */}
{/* Flow Control */}
{/* Terminal Options */}

{t('serial.field.localEchoDesc')}

setLocalEcho(e.target.checked)} className="h-4 w-4 rounded border-input" />

{t('serial.field.lineModeDesc')}

setLineMode(e.target.checked)} className="h-4 w-4 rounded border-input" />
); }; export default SerialConnectModal;