({
open,
onClose,
onConnect,
onSaveHost,
})
| 47 | const FLOW_CONTROL_OPTIONS: SerialFlowControl[] = ['none', 'xon/xoff', 'rts/cts']; |
| 48 | |
| 49 | export const SerialConnectModal: React.FC<SerialConnectModalProps> = ({ |
| 50 | open, |
| 51 | onClose, |
| 52 | onConnect, |
| 53 | onSaveHost, |
| 54 | }) => { |
| 55 | const { t } = useI18n(); |
| 56 | const [ports, setPorts] = useState<SerialPort[]>([]); |
| 57 | const [isLoadingPorts, setIsLoadingPorts] = useState(false); |
| 58 | const [showAdvanced, setShowAdvanced] = useState(false); |
| 59 | |
| 60 | // Form state |
| 61 | const [selectedPort, setSelectedPort] = useState(''); |
| 62 | const [baudRate, setBaudRate] = useState(115200); |
| 63 | const [dataBits, setDataBits] = useState<5 | 6 | 7 | 8>(8); |
| 64 | const [stopBits, setStopBits] = useState<1 | 1.5 | 2>(1); |
| 65 | const [parity, setParity] = useState<SerialParity>('none'); |
| 66 | const [flowControl, setFlowControl] = useState<SerialFlowControl>('none'); |
| 67 | const [localEcho, setLocalEcho] = useState(false); |
| 68 | const [lineMode, setLineMode] = useState(false); |
| 69 | const [charset, setCharset] = useState('UTF-8'); |
| 70 | |
| 71 | // Save configuration state |
| 72 | const [saveConfig, setSaveConfig] = useState(false); |
| 73 | const [configLabel, setConfigLabel] = useState(''); |
| 74 | |
| 75 | const terminalBackend = useTerminalBackend(); |
| 76 | |
| 77 | const loadPorts = useCallback(async () => { |
| 78 | setIsLoadingPorts(true); |
| 79 | try { |
| 80 | const result = await terminalBackend.listSerialPorts(); |
| 81 | setPorts(result); |
| 82 | // Auto-select first port if available and no port is selected |
| 83 | if (result.length > 0) { |
| 84 | setSelectedPort((prev) => prev || result[0].path); |
| 85 | } |
| 86 | } catch (err) { |
| 87 | console.error('[Serial] Failed to list ports:', err); |
| 88 | } finally { |
| 89 | setIsLoadingPorts(false); |
| 90 | } |
| 91 | }, [terminalBackend]); |
| 92 | |
| 93 | useEffect(() => { |
| 94 | if (open) { |
| 95 | loadPorts(); |
| 96 | } |
| 97 | }, [open]); // eslint-disable-line react-hooks/exhaustive-deps |
| 98 | |
| 99 | // Generate a default label when port is selected |
| 100 | useEffect(() => { |
| 101 | if (selectedPort && !configLabel) { |
| 102 | const portName = selectedPort.split('/').pop() || selectedPort; |
| 103 | setConfigLabel(`Serial: ${portName}`); |
| 104 | } |
| 105 | }, [selectedPort, configLabel]); |
| 106 |
nothing calls this directly
no test coverage detected