@@ -112,6 +112,8 @@ const Connection: React.FC<ConnectionProps> = ({
112112 const [ popoverVisible , setPopoverVisible ] = useState ( false ) ;
113113 const [ selectedBitsValue , setSelectedBitsValue ] = useState < BitSelection > ( 10 ) ;
114114 const existingRecordRef = useRef < any | undefined > ( undefined ) ;
115+ const devicenameref = useRef < string > ( "" ) ;
116+ const [ deviceReady , setDeviceReady ] = useState ( false ) ;
115117
116118 // UI Themes & Modes
117119 const { theme } = useTheme ( ) ; // Current theme of the app
@@ -174,46 +176,43 @@ const Connection: React.FC<ConnectionProps> = ({
174176 } ;
175177
176178 useEffect ( ( ) => {
179+ if ( ! deviceReady || ! devicenameref . current || maxCanvasElementCountRef . current === undefined ) return ;
180+
177181 const enabledChannels = Array . from ( { length : maxCanvasElementCountRef . current } , ( _ , i ) => i + 1 ) ;
178182
179183 // Retrieve saved devices from localStorage
180- const savedPorts = JSON . parse ( localStorage . getItem ( 'savedDevices' ) || '[]' ) ;
181- const portInfo = portRef . current ?. getInfo ( ) ;
184+ const savedPorts = JSON . parse ( localStorage . getItem ( "savedDevices" ) || "[]" ) ;
182185
183- let initialSelectedChannelsRefs = [ 1 ] ; // Default to channel 1
186+ let initialSelectedChannelsRefs : number [ ] = [ 1 ] ; // Default to channel 1
184187
188+ // Ensure port info is available
189+ const portInfo = portRef . current ?. getInfo ( ) ;
185190 if ( portInfo ) {
186191 const { usbVendorId, usbProductId } = portInfo ;
187-
188- // Find the device based on usbVendorId and usbProductId
189192 const deviceIndex = savedPorts . findIndex (
190193 ( saved : SavedDevice ) =>
191194 saved . usbVendorId === usbVendorId && saved . usbProductId === usbProductId
192195 ) ;
193196
194197 if ( deviceIndex !== - 1 ) {
195- // Get the device name from savedDevices
196- const deviceName = savedPorts [ deviceIndex ] . deviceName ;
197- const savedChannels = savedPorts [ deviceIndex ] . selectedChannels ;
198-
199- // Set the selected channels from localStorage or default to [1]
200- initialSelectedChannelsRefs = savedChannels . length > 0 ? savedChannels : [ 1 ] ;
201-
202- // Use deviceName for any further operations if needed
198+ // Load saved channels from localStorage
199+ const savedChannels = savedPorts [ deviceIndex ] ?. selectedChannels || [ ] ;
200+ initialSelectedChannelsRefs = savedChannels . length > 0 ? savedChannels : enabledChannels ;
203201 }
204202 }
205203
206- // Set the selected channels in state
207- setSelectedChannels ( initialSelectedChannelsRefs ) ;
204+ setSelectedChannels ( ( prev ) => {
205+ const updated = initialSelectedChannelsRefs ;
206+ return updated ;
207+ } ) ;
208208
209- // Determine if "Select All" is enabled
210- const allSelected = initialSelectedChannelsRefs . length === enabledChannels . length ;
211- const selectAllDisabled = initialSelectedChannelsRefs . length === enabledChannels . length - 1 ;
212209
213- // Set the state based on the selection
210+ // Determine "Select All" state
211+ const allSelected = initialSelectedChannelsRefs . length === enabledChannels . length ;
214212 setIsAllEnabledChannelSelected ( allSelected ) ;
215- setIsSelectAllDisabled ( selectAllDisabled ) ;
216- } , [ ] ) ; // Runs only on component mount
213+ setIsSelectAllDisabled ( initialSelectedChannelsRefs . length === enabledChannels . length - 1 ) ;
214+
215+ } , [ deviceReady , maxCanvasElementCountRef . current , portRef . current ] ) ;
217216
218217
219218 useEffect ( ( ) => {
@@ -247,8 +246,7 @@ const Connection: React.FC<ConnectionProps> = ({
247246 // Find the device by usbVendorId and usbProductId in savedPorts
248247 const deviceIndex = savedPorts . findIndex (
249248 ( saved : SavedDevice ) =>
250- saved . usbVendorId === ( usbVendorId ?? 0 ) &&
251- saved . usbProductId === ( usbProductId ?? 0 )
249+ saved . deviceName === devicenameref . current
252250 ) ;
253251
254252 if ( deviceIndex !== - 1 ) {
@@ -266,7 +264,6 @@ const Connection: React.FC<ConnectionProps> = ({
266264 setIsAllEnabledChannelSelected ( ( prevState ) => ! prevState ) ;
267265 } ;
268266
269-
270267 const toggleChannel = ( channelIndex : number ) => {
271268 setSelectedChannels ( ( prevSelected ) => {
272269 setManuallySelected ( true ) ;
@@ -286,17 +283,16 @@ const Connection: React.FC<ConnectionProps> = ({
286283
287284 if ( portInfo ) {
288285 // Get the deviceName from portInfo (if saved previously)
289- const deviceName = savedPorts . find ( ( saved : SavedDevice ) => saved . usbVendorId === portInfo . usbVendorId && saved . usbProductId === portInfo . usbProductId ) ?. deviceName ;
286+ // const deviceName = savedPorts.find((saved: SavedDevice) => saved.usbVendorId === portInfo.usbVendorId && saved.usbProductId === portInfo.usbProductId )?.deviceName;
290287
291- if ( deviceName ) {
292- const deviceIndex = savedPorts . findIndex (
293- ( saved : SavedDevice ) => saved . deviceName === deviceName
294- ) ;
288+ const deviceIndex = savedPorts . findIndex (
289+ ( saved : SavedDevice ) => saved . deviceName === devicenameref . current
290+ ) ;
291+
292+ if ( deviceIndex !== - 1 ) {
293+ savedPorts [ deviceIndex ] . selectedChannels = sortedChannels ;
294+ localStorage . setItem ( 'savedDevices' , JSON . stringify ( savedPorts ) ) ;
295295
296- if ( deviceIndex !== - 1 ) {
297- savedPorts [ deviceIndex ] . selectedChannels = sortedChannels ;
298- localStorage . setItem ( 'savedDevices' , JSON . stringify ( savedPorts ) ) ;
299- }
300296 }
301297 }
302298
@@ -624,18 +620,6 @@ const Connection: React.FC<ConnectionProps> = ({
624620 const board = BoardsList . find ( ( b ) => b . field_pid === usbProductId ) ;
625621 baudRate = board ? board . baud_Rate : 0 ;
626622 serialTimeout = board ? board . serial_timeout : 0 ;
627-
628- savedPorts . push ( {
629- deviceName : '' ,
630- usbVendorId,
631- usbProductId,
632- baudRate,
633- serialTimeout,
634- selectedChannels : [ 1 ] , // Default to channel 1
635- } ) ;
636-
637- localStorage . setItem ( 'savedDevices' , JSON . stringify ( savedPorts ) ) ;
638- setSelectedChannels ( [ 1 ] ) ;
639623 await port . open ( { baudRate } ) ;
640624 setIsLoading ( true ) ;
641625 } else {
@@ -658,11 +642,9 @@ const Connection: React.FC<ConnectionProps> = ({
658642 serialTimeout = savedDevice ?. serialTimeout || 2000 ;
659643
660644 await port . open ( { baudRate } ) ;
661-
662- const lastSelectedChannels = savedDevice ?. selectedChannels || [ 1 ] ;
663- setSelectedChannels ( lastSelectedChannels ) ;
664645 }
665646
647+
666648 if ( port . readable ) {
667649 const reader = port . readable . getReader ( ) ;
668650 readerRef . current = reader ;
@@ -681,17 +663,18 @@ const Connection: React.FC<ConnectionProps> = ({
681663 }
682664 }
683665 const response = buffer . trim ( ) . split ( "\n" ) . pop ( ) ;
684- const extractedName = response ?. match ( / [ A - Z a - z 0 - 9 \- ] + $ / ) ?. [ 0 ] ?? "Unknown Device" ;
685-
666+ const extractedName = response ?. match ( / [ A - Z a - z 0 - 9 \- _ \s ] + $ / ) ?. [ 0 ] ?. trim ( ) || "Unknown Device" ;
667+ devicenameref . current = extractedName ;
686668 const currentPortInfo = port . getInfo ( ) ;
687669 const usbProductId = currentPortInfo . usbProductId ?? 0 ;
688670
689671 const existingDeviceIndex = savedPorts . findIndex (
690- ( saved : SavedDevice ) => saved . usbVendorId === currentPortInfo . usbVendorId && saved . usbProductId === usbProductId
672+ ( saved : SavedDevice ) => saved . deviceName === extractedName
691673 ) ;
692674
693675 if ( existingDeviceIndex !== - 1 ) {
694- savedPorts [ existingDeviceIndex ] . deviceName = extractedName ;
676+ const lastSelectedChannels = savedPorts ?. selectedChannels || [ 1 ] ;
677+ setSelectedChannels ( lastSelectedChannels ) ;
695678 } else {
696679 savedPorts . push ( {
697680 deviceName : extractedName ,
@@ -701,7 +684,9 @@ const Connection: React.FC<ConnectionProps> = ({
701684 serialTimeout,
702685 selectedChannels : [ 1 ] ,
703686 } ) ;
687+
704688 }
689+
705690 localStorage . setItem ( 'savedDevices' , JSON . stringify ( savedPorts ) ) ;
706691
707692 const { formattedInfo, adcResolution, channelCount, baudRate : extractedBaudRate , serialTimeout : extractedSerialTimeout } = formatPortInfo ( currentPortInfo , extractedName , usbProductId ) ;
0 commit comments