-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMotionInput.jsx
More file actions
61 lines (56 loc) · 1.77 KB
/
Copy pathMotionInput.jsx
File metadata and controls
61 lines (56 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from "react";
import { useEffect, useState, startTransition } from "react";
import { useSelector } from "react-redux";
import { Switch } from "@raspberrypifoundation/design-system-react";
import Sk from "skulpt";
import "../../../assets/stylesheets/AstroPiModel.scss";
import { useTranslation } from "react-i18next";
const MotionInput = (props) => {
const { defaultValue } = props;
const [value, setValue] = useState(defaultValue);
const codeRunTriggered = useSelector(
(state) => state.editor.codeRunTriggered,
);
const { t } = useTranslation();
useEffect(() => {
if (!codeRunTriggered) {
Sk.sense_hat.start_motion_callback = () => {};
Sk.sense_hat.stop_motion_callback = () => {};
}
}, [codeRunTriggered]);
useEffect(() => {
if (Sk.sense_hat) {
startTransition(() => {
Sk.sense_hat.motion = value;
});
}
value
? Sk.sense_hat.start_motion_callback()
: Sk.sense_hat.stop_motion_callback();
}, [value]);
return (
<div className="sense-hat-controls-panel__container">
<label
className="sense-hat-controls-panel__control-name"
htmlFor={`sense_hat_motion`}
>
{t("output.senseHat.controls.motion")}
</label>
<div className="sense-hat-controls-panel__control-toggle">
<label htmlFor={`sense_hat_motion`}>
{t("output.senseHat.controls.motionSensorOptions.no")}
</label>
<Switch
id="sense_hat_motion"
name="senseHatMotionToggle"
checked={value}
onChange={(e) => setValue(e.target.checked)}
/>
<label htmlFor={`sense_hat_motion`}>
{t("output.senseHat.controls.motionSensorOptions.yes")}
</label>
</div>
</div>
);
};
export default MotionInput;