1
- import React , { useRef } from 'react' ;
1
+ import React , { useRef , useState } from 'react' ;
2
2
3
3
import './StepInput.css' ;
4
4
import { dispatchCurrentBeatEvent } from '../events/dispatchCurrentBeatEvent' ;
@@ -15,6 +15,7 @@ const StepInput: React.FC<StepInputProps> = ({ gridIndex }) => {
15
15
const { grids, selectedInput, setOrUpdateNoteInGrid, inputChannels } = useMusicStore ( ) ;
16
16
const grid = grids [ gridIndex ] ;
17
17
const currentBeatRef = useRef ( 0 ) ;
18
+ const [ stepInputMode , setStepInputMode ] = useState ( false ) ; // State for step input mode
18
19
19
20
const handleMIDIMessage = ( event : WebMidi . MIDIMessageEvent ) => {
20
21
const [ status , note , velocity ] = event . data ;
@@ -28,13 +29,15 @@ const StepInput: React.FC<StepInputProps> = ({ gridIndex }) => {
28
29
}
29
30
} ;
30
31
31
- const toggleStepInputMode = ( ) => {
32
- if ( currentBeatRef . current > 0 ) {
32
+ const toggleStepInputMode = ( forceStop : boolean = false ) => {
33
+ if ( stepInputMode || forceStop ) {
33
34
// Reset current beat when exiting step input mode
34
35
currentBeatRef . current = 0 ;
35
36
stopListening ( ) ;
37
+ setStepInputMode ( false ) ; // Update state
36
38
} else {
37
39
startListening ( ) ;
40
+ setStepInputMode ( true ) ; // Update state
38
41
}
39
42
} ;
40
43
@@ -68,22 +71,23 @@ const StepInput: React.FC<StepInputProps> = ({ gridIndex }) => {
68
71
const nextBeat = currentBeatRef . current + 1 ;
69
72
70
73
// Check if the next beat exceeds the number of columns
74
+ console . log ( nextBeat , grid . numColumns )
71
75
if ( nextBeat >= grid . numColumns ) {
72
- toggleStepInputMode ( ) ; // Automatically stop if out of bounds
76
+ toggleStepInputMode ( true ) ; // Automatically stop if out of bounds
73
77
currentBeatRef . current = 0 ; // Reset current beat for the next activation
74
- }
75
- else {
78
+ } else {
76
79
currentBeatRef . current = nextBeat ; // Update the current beat
77
80
dispatchCurrentBeatEvent ( currentBeatRef . current ) ;
78
81
}
79
82
} ;
80
83
81
84
return (
82
- < button className = 'step-input-button' onClick = { toggleStepInputMode }
83
- title = { currentBeatRef . current > 0 ? 'Exit step input mode' : 'Enter step input mode' }
85
+ < button className = { `step-input-button ${ stepInputMode ? 'active' : '' } ` }
86
+ onClick = { ( ) => toggleStepInputMode ( ) }
87
+ title = { stepInputMode ? 'Exit step input mode' : 'Enter step input mode' }
84
88
>
85
- { currentBeatRef . current > 0 ? 'Exit' : 'Step' }
86
- </ button >
89
+ { stepInputMode ? 'Exit' : 'Step' }
90
+ </ button >
87
91
) ;
88
92
} ;
89
93
0 commit comments