Skip to content

Commit 7300aeb

Browse files
Debounce estimated action aria-live announcement (#531)
1 parent 2a26c85 commit 7300aeb

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/components/PredictedAction.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
import { HStack, Text, VisuallyHidden, VStack } from "@chakra-ui/react";
2+
import debounce from "lodash.debounce";
3+
import { useEffect, useMemo, useState } from "react";
24
import { FormattedMessage, useIntl } from "react-intl";
5+
import { useStore } from "../store";
36
import { tourElClassname } from "../tours";
47
import InfoToolTip from "./InfoToolTip";
58
import LedIcon from "./LedIcon";
69
import { predictedActionDisplayWidth } from "./LiveGraphPanel";
7-
import { useStore } from "../store";
810

911
const PredictedAction = () => {
1012
const intl = useIntl();
1113
const predictionResult = useStore((s) => s.predictionResult);
14+
const estimatedAction = predictionResult?.detected?.name;
15+
const [liveRegionEstimatedAction, setLiveRegionEstimatedAction] = useState<
16+
string | undefined
17+
>(estimatedAction);
18+
const setLiveRegionEstimatedActionDebounced = useMemo(
19+
() => debounce(setLiveRegionEstimatedAction, 500),
20+
[]
21+
);
22+
useEffect(() => {
23+
setLiveRegionEstimatedActionDebounced(estimatedAction);
24+
}, [setLiveRegionEstimatedActionDebounced, estimatedAction]);
25+
1226
return (
1327
<VStack
1428
className={tourElClassname.estimatedAction}
@@ -23,7 +37,7 @@ const PredictedAction = () => {
2337
id="estimated-action-aria"
2438
values={{
2539
action:
26-
predictionResult?.detected?.name ??
40+
liveRegionEstimatedAction ??
2741
intl.formatMessage({ id: "unknown" }),
2842
}}
2943
/>
@@ -52,7 +66,7 @@ const PredictedAction = () => {
5266
textAlign="center"
5367
w={`${predictedActionDisplayWidth}px`}
5468
>
55-
{predictionResult?.detected?.name ?? <FormattedMessage id="unknown" />}
69+
{estimatedAction ?? <FormattedMessage id="unknown" />}
5670
</Text>
5771
</VStack>
5872
);

0 commit comments

Comments
 (0)