Skip to content

Commit 3393be1

Browse files
committed
Added loading button
1 parent a5ac45d commit 3393be1

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/components/Connection.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ const Connection: React.FC<ConnectionProps> = ({
160160

161161
// Loading State
162162
const [isLoading, setIsLoading] = useState(false); // Track loading state for asynchronous operations
163+
const [isfftLoading, setIsfftLoading] = useState(false); // Track loading state for asynchronous operations
163164

164165

165166
let activeBufferIndex = 0;
@@ -776,7 +777,9 @@ const Connection: React.FC<ConnectionProps> = ({
776777
baudRate = board ? board.baud_Rate : 0;
777778
serialTimeout = board ? board.serial_timeout : 0;
778779
await port.open({ baudRate });
780+
setIsfftLoading(true);
779781
} else {
782+
setIsfftLoading(true);
780783
const info = port.getInfo();
781784
const savedDevice = savedPorts.find(
782785
(saved: SavedDevice) => saved.usbProductId === info.usbProductId
@@ -894,6 +897,8 @@ const Connection: React.FC<ConnectionProps> = ({
894897
console.error("Error connecting to device:", error);
895898
toast.error("Failed to connect to device.");
896899
}
900+
setIsfftLoading(false);
901+
897902
};
898903

899904

@@ -1340,13 +1345,28 @@ const Connection: React.FC<ConnectionProps> = ({
13401345
</Button>
13411346
)}
13421347
{!isDeviceConnected && (
1343-
<Button
1344-
className="py-2 px-4 rounded-xl font-semibold"
1345-
onClick={() => (isDeviceConnected ? disconnectDevice() : connectToDevicefft())}
1346-
1347-
>
1348+
<Button
1349+
className="flex items-center gap-1 py-2 px-4 rounded-xl font-semibold"
1350+
onClick={() => (isDeviceConnected ? disconnectDevice() : connectToDevicefft())}
1351+
disabled={isfftLoading}
1352+
>
1353+
{isfftLoading ? (
1354+
<>
1355+
<Loader size={17} className="animate-spin" />
1356+
Connecting...
1357+
</>
1358+
) : isDeviceConnected ? (
1359+
<>
1360+
Disconnect
1361+
<CircleX size={17} />
1362+
</>
1363+
) : (
1364+
<>
13481365
FFT Visualizer
1349-
</Button>
1366+
<Cable size={17} />
1367+
</>
1368+
)}
1369+
</Button>
13501370
)}
13511371
</Popover>
13521372
</TooltipTrigger>

0 commit comments

Comments
 (0)