Skip to content

Commit 2e8afd5

Browse files
author
Ritika Mishra
committed
improved logic of channel selection to handle last button
1 parent 52dd5e3 commit 2e8afd5

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/components/Connection.tsx

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ const Connection: React.FC<ConnectionProps> = ({
9797
const [datasets, setDatasets] = useState<any[]>([]);
9898
const currentFilenameRef = useRef<string>("");
9999
const [isRecordButtonDisabled, setIsRecordButtonDisabled] = useState(false);
100+
const [isSelectAllDisabled, setIsSelectAllDisabled] = useState(false);
100101
const [recordingElapsedTime, setRecordingElapsedTime] = useState<number>(0); // State to store the recording duration
101102
const recordingStartTime = useRef<number>(0);
102103
const [customTime, setCustomTime] = useState<string>(""); // State to store the custom stop time input
@@ -112,7 +113,7 @@ const Connection: React.FC<ConnectionProps> = ({
112113
const maxCanvasCountRef = useRef<number>(1);
113114
// Inside your component
114115
const [isAllEnabledSelected, setIsAllEnabledSelected] = useState(false);
115-
const [initialSelection, setInitialSelection] = useState<number[]>([1, 3, 5]);
116+
116117

117118
const readerRef = useRef<
118119
ReadableStreamDefaultReader<Uint8Array> | null | undefined
@@ -151,15 +152,15 @@ const Connection: React.FC<ConnectionProps> = ({
151152
SetCurrentSnapshot(currentSnapshot + 1);
152153
}
153154
};
154-
// UseEffect to load the initial state when the page is loaded or refreshed
155155
useEffect(() => {
156156
const enabledChannels = Array.from({ length: maxCanvasCountRef.current }, (_, i) => i + 1);
157157

158-
// Check if there is saved data in localStorage to load previously selected channels
158+
// Check localStorage for saved data
159159
const savedPorts = JSON.parse(localStorage.getItem('savedDevices') || '[]');
160160
const portInfo = portRef.current?.getInfo();
161161

162-
// Default selected channel to 1 if no saved channels are found in localStorage
162+
let initialSelectedChannels = [1]; // Default to channel 1 if no saved channels are found
163+
163164
if (portInfo) {
164165
const { usbVendorId, usbProductId } = portInfo;
165166
const deviceIndex = savedPorts.findIndex(
@@ -170,14 +171,37 @@ const Connection: React.FC<ConnectionProps> = ({
170171

171172
if (deviceIndex !== -1) {
172173
const savedChannels = savedPorts[deviceIndex].selectedChannels;
173-
setSelectedChannels(savedChannels.length > 0 ? savedChannels : [1]); // Default to [1] if no channels are saved
174+
initialSelectedChannels = savedChannels.length > 0 ? savedChannels : [1]; // Load saved channels or default to [1]
174175
}
175176
}
176177

177-
// Set the state for "Select All" button based on whether all channels are selected
178-
setIsAllEnabledSelected(selectedChannels.length === enabledChannels.length);
178+
// Set initial selected channels
179+
setSelectedChannels(initialSelectedChannels);
180+
181+
// Update the "Select All" button state based on the loaded channels
182+
const allSelected = initialSelectedChannels.length === enabledChannels.length;
183+
const selectAllDisabled = initialSelectedChannels.length === enabledChannels.length - 1;
184+
185+
setIsAllEnabledSelected(allSelected);
186+
setIsSelectAllDisabled(selectAllDisabled);
179187
}, []); // Runs only on component mount
180188

189+
190+
useEffect(() => {
191+
setSelectedChannels(selectedChannels);
192+
}, [selectedChannels]);
193+
194+
// UseEffect to track changes in selectedChannels and enabledChannels
195+
useEffect(() => {
196+
const enabledChannels = Array.from({ length: maxCanvasCountRef.current }, (_, i) => i + 1);
197+
198+
// Disable "Select All" button if the difference is exactly 1
199+
setIsSelectAllDisabled(selectedChannels.length === enabledChannels.length - 1);
200+
201+
// Update the "Select All" button state
202+
setIsAllEnabledSelected(selectedChannels.length === enabledChannels.length);
203+
}, [selectedChannels]); // Trigger whenever selectedChannels changes
204+
181205
const handleSelectAllToggle = () => {
182206
const enabledChannels = Array.from({ length: maxCanvasCountRef.current }, (_, i) => i + 1);
183207
const remainingChannels = enabledChannels.filter(channel => !selectedChannels.includes(channel));
@@ -248,14 +272,6 @@ const Connection: React.FC<ConnectionProps> = ({
248272
}, [selectedChannels]); // Trigger on any update to selectedChannels
249273

250274

251-
252-
// Disable "Select All" button when selectedChannels count is less than enabledChannels count
253-
const isSelectAllDisabled = selectedChannels.length >= maxCanvasCountRef.current - 1;
254-
255-
useEffect(() => {
256-
setSelectedChannels(selectedChannels);
257-
}, [selectedChannels]);
258-
259275
// Handle right arrow click (reset count and disable button if needed)
260276
const handleNextSnapshot = () => {
261277
if (clickCount > 0) {
@@ -1584,10 +1600,15 @@ const Connection: React.FC<ConnectionProps> = ({
15841600
</h3>
15851601
<button
15861602
onClick={handleSelectAllToggle}
1587-
className="px-4 py-1 text-xs font-light text-white bg-black dark:text-black rounded-lg hover:bg-gray-700 dark:bg-white dark:border dark:border-gray-500 dark:hover:bg-primary/70 transition"
1603+
className={`px-4 py-1 text-xs font-light rounded-lg transition ${isSelectAllDisabled
1604+
? "text-gray-400 bg-gray-200 dark:bg-gray-700 dark:text-gray-500 cursor-not-allowed"
1605+
: "text-white bg-black hover:bg-gray-700 dark:bg-white dark:text-black dark:border dark:border-gray-500 dark:hover:bg-primary/70"
1606+
}`}
1607+
disabled={isSelectAllDisabled}
15881608
>
1589-
{isAllEnabledSelected ? "RESET" : "Select All "}
1609+
{isAllEnabledSelected ? "RESET" : "Select All"}
15901610
</button>
1611+
15911612
</div>
15921613

15931614
{/* Button Grid */}

0 commit comments

Comments
 (0)