@@ -151,34 +151,52 @@ const Connection: React.FC<ConnectionProps> = ({
151151 SetCurrentSnapshot ( currentSnapshot + 1 ) ;
152152 }
153153 } ;
154+ // UseEffect to load the initial state when the page is loaded or refreshed
155+ useEffect ( ( ) => {
156+ const enabledChannels = Array . from ( { length : maxCanvasCountRef . current } , ( _ , i ) => i + 1 ) ;
157+
158+ // Check if there is saved data in localStorage to load previously selected channels
159+ const savedPorts = JSON . parse ( localStorage . getItem ( 'savedDevices' ) || '[]' ) ;
160+ const portInfo = portRef . current ?. getInfo ( ) ;
161+
162+ // Default selected channel to 1 if no saved channels are found in localStorage
163+ if ( portInfo ) {
164+ const { usbVendorId, usbProductId } = portInfo ;
165+ const deviceIndex = savedPorts . findIndex (
166+ ( saved : SavedDevice ) =>
167+ saved . usbVendorId === ( usbVendorId ?? 0 ) &&
168+ saved . usbProductId === ( usbProductId ?? 0 )
169+ ) ;
170+
171+ if ( deviceIndex !== - 1 ) {
172+ const savedChannels = savedPorts [ deviceIndex ] . selectedChannels ;
173+ setSelectedChannels ( savedChannels . length > 0 ? savedChannels : [ 1 ] ) ; // Default to [1] if no channels are saved
174+ }
175+ }
176+
177+ // Set the state for "Select All" button based on whether all channels are selected
178+ setIsAllEnabledSelected ( selectedChannels . length === enabledChannels . length ) ;
179+ } , [ ] ) ; // Runs only on component mount
154180
155181 const handleSelectAllToggle = ( ) => {
156182 const enabledChannels = Array . from ( { length : maxCanvasCountRef . current } , ( _ , i ) => i + 1 ) ;
183+ const remainingChannels = enabledChannels . filter ( channel => ! selectedChannels . includes ( channel ) ) ;
157184
158185 if ( ! isAllEnabledSelected ) {
159- // Select all enabled channels
160- enabledChannels . forEach ( ( channel ) => {
161- if ( ! selectedChannels . includes ( channel ) ) {
162- toggleChannel ( channel ) ;
163- }
164- } ) ;
165-
166- // Set initialSelection to the current selected channels when "Select All" is clicked
167- setInitialSelection ( [ ...selectedChannels ] ) ;
186+ // If all channels are not selected, and only one channel is remaining, simulate selecting it
187+ if ( remainingChannels . length === 1 ) {
188+ toggleChannel ( remainingChannels [ 0 ] ) ;
189+ } else {
190+ // Select all enabled channels
191+ enabledChannels . forEach ( ( channel ) => {
192+ if ( ! selectedChannels . includes ( channel ) ) {
193+ toggleChannel ( channel ) ;
194+ }
195+ } ) ;
196+ }
168197 } else {
169- // Deselect all enabled channels and return to the initial state
170- selectedChannels . forEach ( ( channel ) => {
171- if ( ! initialSelection . includes ( channel ) ) {
172- toggleChannel ( channel ) ;
173- }
174- } ) ;
175-
176- // Restore the initial selection (previously selected channels)
177- initialSelection . forEach ( ( channel ) => {
178- if ( ! selectedChannels . includes ( channel ) ) {
179- toggleChannel ( channel ) ;
180- }
181- } ) ;
198+ // If "RESET" is clicked, reset to channel 1 or remove all selected channels
199+ setSelectedChannels ( [ 1 ] ) ;
182200 }
183201
184202 // Toggle the state to indicate whether all channels are selected
@@ -221,6 +239,19 @@ const Connection: React.FC<ConnectionProps> = ({
221239 } ) ;
222240 } ;
223241
242+ // Use effect to track when all channels are selected manually
243+ useEffect ( ( ) => {
244+ const enabledChannels = Array . from ( { length : maxCanvasCountRef . current } , ( _ , i ) => i + 1 ) ;
245+
246+ // Check if all enabled channels are selected to update the "Select All" state
247+ setIsAllEnabledSelected ( selectedChannels . length === enabledChannels . length ) ;
248+ } , [ selectedChannels ] ) ; // Trigger on any update to selectedChannels
249+
250+
251+
252+ // Disable "Select All" button when selectedChannels count is less than enabledChannels count
253+ const isSelectAllDisabled = selectedChannels . length >= maxCanvasCountRef . current - 1 ;
254+
224255 useEffect ( ( ) => {
225256 setSelectedChannels ( selectedChannels ) ;
226257 } , [ selectedChannels ] ) ;
0 commit comments