Skip to content

Commit 15dca89

Browse files
author
Ritika Mishra
committed
move state updates to effects
1 parent 6fda4ca commit 15dca89

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

src/app/npg-lite/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,6 @@ const NPG_Ble = () => {
786786

787787
const toggleChannel = (channelIndex: number) => {
788788
setSelectedChannels((prevSelected) => {
789-
setManuallySelected(true);
790789
const updatedChannels = prevSelected.includes(channelIndex)
791790
? prevSelected.filter((ch) => ch !== channelIndex)
792791
: [...prevSelected, channelIndex];
@@ -799,6 +798,8 @@ const NPG_Ble = () => {
799798

800799
return sortedChannels;
801800
});
801+
802+
setManuallySelected(true);
802803
};
803804

804805

src/components/Connection.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ const Connection: React.FC<ConnectionProps> = ({
280280

281281

282282
const toggleChannel = (channelIndex: number) => {
283+
// Mark as manually selected and update selectedChannels
283284
setSelectedChannels((prevSelected) => {
284-
setManuallySelected(true);
285285
const updatedChannels = prevSelected.includes(channelIndex)
286286
? prevSelected.filter((ch) => ch !== channelIndex)
287287
: [...prevSelected, channelIndex];
@@ -292,24 +292,29 @@ const Connection: React.FC<ConnectionProps> = ({
292292
sortedChannels.push(1);
293293
}
294294

295-
// Retrieve saved devices from localStorage
296-
const savedPorts = JSON.parse(localStorage.getItem('savedDevices') || '[]');
297-
const portInfo = portRef.current?.getInfo();
298-
299-
if (portInfo) {
300-
const deviceIndex = savedPorts.findIndex(
301-
(saved: SavedDevice) => saved.deviceName === devicenameref.current
302-
);
295+
// Retrieve saved devices from localStorage and persist selection
296+
try {
297+
const savedPorts = JSON.parse(localStorage.getItem('savedDevices') || '[]');
298+
const portInfo = portRef.current?.getInfo();
303299

304-
if (deviceIndex !== -1) {
305-
savedPorts[deviceIndex].selectedChannels = sortedChannels;
306-
localStorage.setItem('savedDevices', JSON.stringify(savedPorts));
300+
if (portInfo) {
301+
const deviceIndex = savedPorts.findIndex(
302+
(saved: SavedDevice) => saved.deviceName === devicenameref.current
303+
);
307304

305+
if (deviceIndex !== -1) {
306+
savedPorts[deviceIndex].selectedChannels = sortedChannels;
307+
localStorage.setItem('savedDevices', JSON.stringify(savedPorts));
308+
}
308309
}
310+
} catch (e) {
311+
console.warn('Failed to persist selected channels:', e);
309312
}
310313

311314
return sortedChannels;
312315
});
316+
317+
setManuallySelected(true);
313318
};
314319

315320
// Handle right arrow click (reset count and disable button if needed)

0 commit comments

Comments
 (0)