22import React , { useState , useRef , useCallback , useEffect } from "react" ;
33import { Button } from "./ui/button" ;
44import { Input } from "./ui/input" ;
5- import { EXGFilter , Notch , HighPassFilter } from './filters' ;
5+ import { EXGFilter , Notch , HighPassFilter } from './filters' ;
66import { useTheme } from "next-themes" ;
77import { useRouter } from "next/navigation" ; // Import useRouter
88import { getCustomColor , lightThemeColors } from './Colors' ;
@@ -99,7 +99,7 @@ const Connection: React.FC<ConnectionProps> = ({
9999
100100 // States and Refs for Connection & Recording
101101 const [ isDeviceConnected , setIsDeviceConnected ] = useState < boolean > ( false ) ; // Track if the device is connected
102- const [ isserial , setIsserial ] = useState ( false ) ; // Track if the device is connected
102+ const [ isSerial , setIsSerial ] = useState ( false ) ; // Track if the device is connected
103103
104104 const [ FFTDeviceConnected , setFFTDeviceConnected ] = useState < boolean > ( false ) ; // Track if the device is connected
105105 const isDeviceConnectedRef = useRef < boolean > ( false ) ; // Ref to track if the device is connected
@@ -120,7 +120,6 @@ const Connection: React.FC<ConnectionProps> = ({
120120 const [ recordingElapsedTime , setRecordingElapsedTime ] = useState < number > ( 0 ) ; // State to store the recording duration
121121 const [ customTimeInput , setCustomTimeInput ] = useState < string > ( "" ) ; // State to store the custom stop time input
122122 const [ leftArrowClickCount , setLeftArrowClickCount ] = useState ( 0 ) ; // Track how many times the left arrow is clicked
123- const [ popoverVisible , setPopoverVisible ] = useState ( false ) ;
124123 const [ selectedBitsValue , setSelectedBitsValue ] = useState < BitSelection > ( 10 ) ;
125124 const existingRecordRef = useRef < any | undefined > ( undefined ) ;
126125 const devicenameref = useRef < string > ( "" ) ;
@@ -346,10 +345,7 @@ const Connection: React.FC<ConnectionProps> = ({
346345 }
347346 }
348347 } ;
349-
350- //////////////////////////////////
351348 const workerRef = useRef < Worker | null > ( null ) ;
352-
353349 const initializeWorker = ( ) => {
354350 if ( ! workerRef . current ) {
355351 workerRef . current = new Worker ( new URL ( '../../workers/indexedDBWorker.ts' , import . meta. url ) , {
@@ -379,13 +375,11 @@ const Connection: React.FC<ConnectionProps> = ({
379375 } ) ;
380376
381377 } ;
382- if ( FFTDeviceConnected ) {
383- // If FFT device is connected, send only the selected channel
384- setSelectedChannelsInWorker ( [ selectedChannel ] ) ;
385- } else {
386- // Otherwise, send all selected channels
387- setSelectedChannelsInWorker ( selectedChannels ) ;
388- }
378+ useEffect ( ( ) => {
379+ const channels = FFTDeviceConnected ? [ selectedChannel ] : selectedChannels ;
380+ setSelectedChannelsInWorker ( channels ) ;
381+ } , [ FFTDeviceConnected , selectedChannel , selectedChannels ] ) ;
382+
389383 const processBuffer = async ( bufferIndex : number , canvasCount : number , selectChannel : number [ ] ) => {
390384 if ( ! workerRef . current ) {
391385 initializeWorker ( ) ;
@@ -736,7 +730,7 @@ const Connection: React.FC<ConnectionProps> = ({
736730 } else {
737731 console . error ( "Readable stream not available" ) ;
738732 }
739- setIsserial ( true ) ;
733+ setIsSerial ( true ) ;
740734
741735 setSelectedChannels ( initialSelectedChannelsRef . current ) ;
742736 Connection ( true ) ;
@@ -755,7 +749,7 @@ const Connection: React.FC<ConnectionProps> = ({
755749
756750 } catch ( error ) {
757751 await disconnectDevice ( ) ;
758- setIsserial ( false ) ;
752+ setIsSerial ( false ) ;
759753 console . error ( "Error connecting to device:" , error ) ;
760754 toast . error ( "Failed to connect to device." ) ;
761755 }
@@ -771,7 +765,7 @@ const Connection: React.FC<ConnectionProps> = ({
771765 const savedPorts = JSON . parse ( localStorage . getItem ( 'savedDevices' ) || '[]' ) ;
772766 let port = null ;
773767 const ports = await navigator . serial . getPorts ( ) ;
774- setIsserial ( true ) ;
768+ setIsSerial ( true ) ;
775769 if ( savedPorts . length > 0 ) {
776770 port = ports . find ( ( p ) => {
777771 const info = p . getInfo ( ) ;
@@ -960,7 +954,7 @@ const Connection: React.FC<ConnectionProps> = ({
960954 writerRef . current = null ;
961955 }
962956 }
963- setIsserial ( false ) ;
957+ setIsSerial ( false ) ;
964958 snapShotRef . current ?. fill ( false ) ;
965959 if ( readerRef . current ) {
966960 try {
@@ -1288,7 +1282,7 @@ const Connection: React.FC<ConnectionProps> = ({
12881282 const SYNC_BYTE1 = 0xc7 ; // First synchronization byte to identify the start of a packet
12891283 const SYNC_BYTE2 = 0x7c ; // Second synchronization byte
12901284 const END_BYTE = 0x01 ; // End byte to signify the end of a packet
1291- let previousCounter : number | null = null ; // Variable to store the previous counter value for loss detection
1285+ const prevSampleCounterRef = useRef < number | null > ( null ) ; // Variable to store the previous counter value for loss detection
12921286 const notchFilters = Array . from ( { length : maxCanvasElementCountRef . current } , ( ) => new Notch ( ) ) ;
12931287 const EXGFilters = Array . from ( { length : maxCanvasElementCountRef . current } , ( ) => new EXGFilter ( ) ) ;
12941288 const pointoneFilter = Array . from ( { length : maxCanvasElementCountRef . current } , ( ) => new HighPassFilter ( ) ) ;
@@ -1383,17 +1377,17 @@ const Connection: React.FC<ConnectionProps> = ({
13831377
13841378 }
13851379
1386- if ( previousCounter !== null ) {
1380+ if ( prevSampleCounterRef . current !== null ) {
13871381 // If there was a previous counter value, check for data loss
1388- const expectedCounter : number = ( previousCounter + 1 ) % 256 ; // Calculate the expected counter value
1382+ const expectedCounter : number = ( prevSampleCounterRef . current + 1 ) % 256 ; // Calculate the expected counter value
13891383 if ( counter !== expectedCounter ) {
13901384 // Check for data loss by comparing the current counter with the expected counter
13911385 console . warn (
1392- `Data loss detected! Previous counter: ${ previousCounter } , Current counter: ${ counter } `
1386+ `Data loss detected! Previous counter: ${ prevSampleCounterRef . current } , Current counter: ${ counter } `
13931387 ) ;
13941388 }
13951389 }
1396- previousCounter = counter ; // Update the previous counter with the current counter
1390+ prevSampleCounterRef . current = counter ; // Update the previous counter with the current counter
13971391 buffer . splice ( 0 , endByteIndex + 1 ) ; // Remove the processed packet from the buffer
13981392 } else {
13991393 buffer . splice ( 0 , syncIndex + 1 ) ; // If packet is incomplete, remove bytes up to the sync byte
@@ -1549,7 +1543,7 @@ const Connection: React.FC<ConnectionProps> = ({
15491543 < PopoverTrigger asChild >
15501544 < Button
15511545 className = "flex items-center gap-1 py-2 px-4 rounded-xl font-semibold"
1552- onClick = { ( ) => ( isDeviceConnected ? isserial ? disconnectDevice ( ) : disconnect ( ) : connectToDevice ( ) ) }
1546+ onClick = { ( ) => ( isDeviceConnected ? isSerial ? disconnectDevice ( ) : disconnect ( ) : connectToDevice ( ) ) }
15531547 disabled = { isLoading }
15541548 >
15551549 { isLoading ? (
0 commit comments