/** * Serial Port Connect Modal * Allows users to configure and connect to a serial port */ import { Cpu, Save, Usb } from 'lucide-react'; import React, { useEffect, useState } from 'react'; import { useI18n } from '../application/i18n/I18nProvider'; import type { Host, SerialConfig, SerialFlowControl, SerialParity } from '../domain/models'; import { prepareSerialConfigForSavedHost } from '../domain/serialBackspace'; import { isSerialEndpointPath, isWindowsNamedPipePath } from '../domain/serialEndpoint'; import { SerialAdvancedOptionsSection, SerialBaudRateField, } from './SerialAdvancedOptions'; import { SerialPortField } from './serial/SerialPortField'; import { useSerialPortDiscovery } from './serial/useSerialPortDiscovery'; import { Button } from './ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from './ui/dialog'; import { Input } from './ui/input'; import { Label } from './ui/label'; interface SerialConnectModalProps { open: boolean; onClose: () => void; onConnect: (config: SerialConfig, options?: { charset?: string }) => void; onSaveHost?: (host: Host) => void; } export const SerialConnectModal: React.FC = ({ open, onClose, onConnect, onSaveHost, }) => { const { t } = useI18n(); 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 [backspaceBehavior, setBackspaceBehavior] = useState('default'); const [byteOrientedBackspace, setByteOrientedBackspace] = useState(false); const [charset, setCharset] = useState('UTF-8'); // Save configuration state const [saveConfig, setSaveConfig] = useState(false); const [configLabel, setConfigLabel] = useState(''); const { portOptions, isLoadingPorts, loadPorts } = useSerialPortDiscovery({ enabled: open, onPortsLoaded: (result) => { // Auto-select first port if available and no port is selected if (result.length > 0) { setSelectedPort((prev) => prev || result[0].path); } }, }); // Generate a default label when port is selected useEffect(() => { if (selectedPort && !configLabel) { const portName = selectedPort.split('/').pop() || selectedPort; setConfigLabel(`Serial: ${portName}`); } }, [selectedPort, configLabel]); const handleConnect = () => { if (!selectedPort) return; const config: SerialConfig = { path: selectedPort.trim(), baudRate, dataBits, stopBits, parity, flowControl, localEcho, lineMode, backspaceBehavior, byteOrientedBackspace, }; // Save as host if checkbox is checked and onSaveHost is provided if (saveConfig && onSaveHost) { const portName = selectedPort.split('/').pop() || selectedPort; const host: Host = { id: `serial-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`, label: configLabel.trim() || `Serial: ${portName}`, hostname: selectedPort.trim(), // For serial hosts, port field stores baud rate as a numeric identifier. // The full configuration is stored in serialConfig for actual connection. port: baudRate, username: '', os: 'linux', tags: ['serial'], protocol: 'serial', createdAt: Date.now(), charset, serialConfig: prepareSerialConfigForSavedHost(config), }; onSaveHost(host); } onConnect(config, { charset }); onClose(); }; const trimmedPort = selectedPort.trim(); const isNamedPipe = isWindowsNamedPipePath(trimmedPort); const isPortValid = isSerialEndpointPath(trimmedPort); // Allow custom baud rates as long as they are positive integers const isBaudRateValid = Number.isInteger(baudRate) && baudRate > 0; const isValid = isPortValid && (isNamedPipe || isBaudRateValid); return ( !isOpen && onClose()}> {t('serial.modal.title')} {t('serial.modal.desc')}
{/* Serial Port Selection */} {/* Baud Rate (not used by named pipes) */} {!isNamedPipe && ( )} {/* Advanced Options */} {/* Save Configuration */} {onSaveHost && (

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

setSaveConfig(e.target.checked)} className="h-4 w-4 rounded border-input" />
{saveConfig && (
setConfigLabel(e.target.value)} placeholder={t('serial.field.configLabelPlaceholder')} />
)}
)}
); }; export default SerialConnectModal;