4
4
* SPDX-License-Identifier: MIT
5
5
*/
6
6
import { Box , Button , Switch , VStack } from "@chakra-ui/react" ;
7
- import { ReactNode , useCallback , useState } from "react" ;
7
+ import { ReactNode , useCallback , useRef , useState } from "react" ;
8
8
import { useIntl } from "react-intl" ;
9
9
import {
10
10
RangeSensor as RangeSensorType ,
@@ -35,15 +35,18 @@ const SensorInput = ({
35
35
minimised,
36
36
} : SensorInputProps ) => {
37
37
const sensor = state [ sensorId ] as RangeSensorType ;
38
+ const sensorValue = useRef < number > ( sensor . value ) ;
38
39
const intl = useIntl ( ) ;
39
- const [ mouseDown , setMouseDown ] = useState < boolean > ( false ) ;
40
+ const mouseDown = useRef < boolean > ( false ) ;
40
41
const handleSensorChange = useCallback (
41
42
( value : number ) => {
42
43
// In this case isHeld is true, so the value should be reversed.
43
- if ( sensor . value === value ) {
44
+ if ( sensorValue . current === value ) {
44
45
onValueChange ( sensorId , value === sensor . min ? sensor . max : sensor . min ) ;
46
+ sensorValue . current = value === sensor . min ? sensor . max : sensor . min ;
45
47
} else {
46
48
onValueChange ( sensorId , value ) ;
49
+ sensorValue . current = value ;
47
50
}
48
51
} ,
49
52
[ onValueChange , sensor , sensorId ]
@@ -61,12 +64,12 @@ const SensorInput = ({
61
64
}
62
65
} ;
63
66
const mouseDownTouchStartAction = ( ) => {
64
- setMouseDown ( true ) ;
67
+ mouseDown . current = true ;
65
68
handleSensorChange ( sensor . max ) ;
66
69
} ;
67
70
const mouseUpTouchEndAction = ( ) => {
68
- if ( mouseDown ) {
69
- setMouseDown ( false ) ;
71
+ if ( mouseDown . current ) {
72
+ mouseDown . current = false ;
70
73
handleSensorChange ( sensor . min ) ;
71
74
}
72
75
} ;
@@ -87,8 +90,8 @@ const SensorInput = ({
87
90
mouseUpTouchEndAction ( ) ;
88
91
} ;
89
92
const mouseLeaveListener = ( ) => {
90
- if ( mouseDown ) {
91
- setMouseDown ( false ) ;
93
+ if ( mouseDown . current ) {
94
+ mouseDown . current = false ;
92
95
handleSensorChange ( sensor . min ) ;
93
96
}
94
97
} ;
@@ -108,11 +111,11 @@ const SensorInput = ({
108
111
} else if ( sensor . value === sensor . max ) {
109
112
setIsHeld ( true ) ;
110
113
}
114
+ sensorValue . current = sensor . value ;
111
115
setPrevSensorValue ( sensor . value ) ;
112
116
}
113
117
114
118
const disabled = running === RunningStatus . STOPPED ;
115
-
116
119
return (
117
120
< VStack spacing = { 3 } >
118
121
< Button
@@ -122,13 +125,13 @@ const SensorInput = ({
122
125
) }
123
126
transition = "none"
124
127
_active = {
125
- sensor . value === sensor . min
128
+ sensorValue . current === sensor . min
126
129
? { }
127
130
: {
128
131
background : "brand.100" ,
129
132
}
130
133
}
131
- isActive = { ! ! sensor . value }
134
+ isActive = { ! ! sensorValue . current }
132
135
disabled = { disabled }
133
136
size = "sm"
134
137
onKeyDown = { keyListener }
0 commit comments