Skip to content

Commit 160a4fa

Browse files
author
Ritika Mishra
committed
added loading while connecting and improved channel selection logic
1 parent 08e2a70 commit 160a4fa

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

src/components/Canvas.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ const Canvas = forwardRef(
340340
// Calculate the current position
341341
const currentPos = sweepPositions.current[i] % line.numPoints;
342342

343-
if (isNaN(currentPos)) {
343+
if (Number.isNaN(currentPos)) {
344344
console.error(`Invalid currentPos at index ${i}. sweepPositions.current[i]:`, sweepPositions.current[i]);
345345
return;
346346
}

src/components/Connection.tsx

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const Connection: React.FC<ConnectionProps> = ({
112112
const maxCanvasCountRef = useRef<number>(1);
113113
// Inside your component
114114
const [isAllEnabledSelected, setIsAllEnabledSelected] = useState(false);
115-
const [initialSelection, setInitialSelection] = useState<number[]>([1, 3, 5]); // Example of initial selected channels
115+
const [initialSelection, setInitialSelection] = useState<number[]>([1, 3, 5]);
116116

117117
const readerRef = useRef<
118118
ReadableStreamDefaultReader<Uint8Array> | null | undefined
@@ -139,15 +139,6 @@ const Connection: React.FC<ConnectionProps> = ({
139139

140140
};
141141

142-
const handleButtonClick = async () => {
143-
setIsLoading(true); // Set loading state to true
144-
try {
145-
await handleClick(); // Attempt to connect or disconnect
146-
} finally {
147-
setIsLoading(false); // Reset loading state after operation
148-
}
149-
};
150-
151142
const enabledClicks = (snapShotRef.current?.filter(Boolean).length ?? 0) - 1;
152143

153144
// Enable/Disable left arrow button
@@ -171,25 +162,31 @@ const Connection: React.FC<ConnectionProps> = ({
171162
toggleChannel(channel);
172163
}
173164
});
165+
166+
// Set initialSelection to the current selected channels when "Select All" is clicked
167+
setInitialSelection([...selectedChannels]);
174168
} else {
175169
// Deselect all enabled channels and return to the initial state
176170
selectedChannels.forEach((channel) => {
177171
if (!initialSelection.includes(channel)) {
178172
toggleChannel(channel);
179173
}
180174
});
175+
176+
// Restore the initial selection (previously selected channels)
177+
initialSelection.forEach((channel) => {
178+
if (!selectedChannels.includes(channel)) {
179+
toggleChannel(channel);
180+
}
181+
});
181182
}
182183

183184
// Toggle the state to indicate whether all channels are selected
184185
setIsAllEnabledSelected(!isAllEnabledSelected);
185186
};
187+
186188
const toggleChannel = (channelIndex: number) => {
187189
setSelectedChannels((prevSelected) => {
188-
// Ensure at least one channel remains selected
189-
if (prevSelected.length === 1 && prevSelected.includes(channelIndex)) {
190-
return prevSelected; // Do not remove the only element
191-
}
192-
193190
// Toggle the selection of the channel
194191
const updatedChannels = prevSelected.includes(channelIndex)
195192
? prevSelected.filter((ch) => ch !== channelIndex) // Remove channel
@@ -428,22 +425,14 @@ const Connection: React.FC<ConnectionProps> = ({
428425
[]
429426
);
430427

431-
const handleClick = () => {
432-
// Function to handle toggle for connect/disconnect button
433-
if (isConnected) {
434-
disconnectDevice();
435-
} else {
436-
connectToDevice();
437-
}
438-
};
439-
440428
interface SavedDevice {
441429
usbVendorId: number;
442430
usbProductId: number;
443431
baudRate: number;
444432
}
445433

446434
const connectToDevice = async () => {
435+
setIsLoading(true); // Set loading state to true
447436
try {
448437
// Disconnect any previous connection if port is open
449438
if (portRef.current && portRef.current.readable) {
@@ -604,6 +593,7 @@ const Connection: React.FC<ConnectionProps> = ({
604593
console.error("Error connecting to device:", error);
605594
toast.error("Failed to connect to device.");
606595
}
596+
setIsLoading(false); // Always stop loading
607597
};
608598

609599

@@ -667,7 +657,7 @@ const Connection: React.FC<ConnectionProps> = ({
667657
await portRef.current.close();
668658
}
669659
portRef.current = null;
670-
660+
setIsConnected(false); // Update connection state
671661
toast("Disconnected from device", {
672662
action: {
673663
label: "Reconnect",
@@ -1136,12 +1126,12 @@ const Connection: React.FC<ConnectionProps> = ({
11361126
<TooltipTrigger asChild>
11371127
<Button
11381128
className="flex items-center justify-center gap-1 py-2 px-2 sm:py-3 sm:px-4 rounded-xl font-semibold"
1139-
onClick={handleButtonClick}
1129+
onClick={isConnected ? disconnectDevice : connectToDevice}
11401130
disabled={isLoading} // Disable button during loading
11411131
>
11421132
{isLoading ? (
11431133
<>
1144-
<Loader size={17} className="animate-spin" /> {/* Show spinning loader */}
1134+
<Loader size={17} className="animate-spin" /> {/* Spinning loader */}
11451135
Connecting...
11461136
</>
11471137
) : isConnected ? (
@@ -1162,6 +1152,8 @@ const Connection: React.FC<ConnectionProps> = ({
11621152
</TooltipContent>
11631153
</Tooltip>
11641154
</TooltipProvider>
1155+
1156+
11651157
{/* Display (Play/Pause) button with tooltip */}
11661158
{isConnected && (
11671159
<div className="flex items-center gap-0.5 mx-0 px-0">
@@ -1556,7 +1548,7 @@ const Connection: React.FC<ConnectionProps> = ({
15561548
{/* Heading and Select All Button */}
15571549
<div className="flex items-center justify-between mb-4">
15581550
<h3 className="text-sm font-semibold text-gray-500">
1559-
<span className="font-bold text-gray-600">Channels Count:</span> {selectedChannels.length}
1551+
<span className="font-bold text-gray-600">Channels Count:</span> {selectedChannels.length}
15601552
</h3>
15611553
<button
15621554
onClick={handleSelectAllToggle}
@@ -1610,7 +1602,7 @@ const Connection: React.FC<ConnectionProps> = ({
16101602
onClick={() => !isChannelDisabled && toggleChannel(index + 1)}
16111603
disabled={isChannelDisabled || isRecordButtonDisabled}
16121604
className={`w-full h-8 text-xs font-medium py-1 border-[0.05px] border-gray-300 dark:border-gray-600 transition-colors duration-200 ${buttonClass} ${roundedClass}`}
1613-
>
1605+
>
16141606
{`CH${index + 1}`}
16151607
</button>
16161608
);
@@ -1622,7 +1614,6 @@ const Connection: React.FC<ConnectionProps> = ({
16221614
</div>
16231615
</div>
16241616

1625-
16261617
{/* Zoom Controls */}
16271618
<div className="relative w-full flex flex-col items-start mt-3">
16281619
<p className="absolute top-[-1.2rem] left-0 text-[0.50rem] font-semibold text-gray-500">

0 commit comments

Comments
 (0)