import React, { useState, useEffect } from 'react'; import { ConnectionStatus } from '../types'; import { Wifi, WifiOff, Activity } from 'lucide-react'; interface Props { status: ConnectionStatus; onConnect: () => void; onDisconnect: () => void; baudRate: number; setBaudRate: (rate: number) => void; address: number; setAddress: (addr: number) => void; } export const ConnectionPanel: React.FC = ({ status, onConnect, onDisconnect, baudRate, setBaudRate, address, setAddress }) => { const isConnected = status === ConnectionStatus.CONNECTED; // Local state for address input to allow smooth typing without auto-formatting interference const [localAddress, setLocalAddress] = useState(address.toString(16).toUpperCase().padStart(2, '0')); const [isFocused, setIsFocused] = useState(false); // Sync from props only when not editing useEffect(() => { if (!isFocused) { setLocalAddress(address.toString(16).toUpperCase().padStart(2, '0')); } }, [address, isFocused]); const handleAddressChange = (e: React.ChangeEvent) => { const val = e.target.value.toUpperCase(); // Validate Hex and Length if (/^[0-9A-F]*$/.test(val) && val.length <= 2) { setLocalAddress(val); // Update parent state if valid if (val !== '') { setAddress(parseInt(val, 16)); } } }; const handleBlur = () => { setIsFocused(false); // On blur, ensure the display matches the formatted prop value (pads single digits, etc.) setLocalAddress(address.toString(16).toUpperCase().padStart(2, '0')); }; return (
{isConnected ? : }

Connection

setIsFocused(true)} onBlur={handleBlur} onChange={handleAddressChange} className="w-full bg-slate-50 border border-slate-300 text-slate-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 font-mono" maxLength={2} placeholder="FF" />
{status}
); };