Skip to content

Commit

Permalink
Debounce estimated action aria-live announcement (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-robert authored Nov 22, 2024
1 parent 2a26c85 commit 7300aeb
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/components/PredictedAction.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { HStack, Text, VisuallyHidden, VStack } from "@chakra-ui/react";
import debounce from "lodash.debounce";
import { useEffect, useMemo, useState } from "react";
import { FormattedMessage, useIntl } from "react-intl";
import { useStore } from "../store";
import { tourElClassname } from "../tours";
import InfoToolTip from "./InfoToolTip";
import LedIcon from "./LedIcon";
import { predictedActionDisplayWidth } from "./LiveGraphPanel";
import { useStore } from "../store";

const PredictedAction = () => {
const intl = useIntl();
const predictionResult = useStore((s) => s.predictionResult);
const estimatedAction = predictionResult?.detected?.name;
const [liveRegionEstimatedAction, setLiveRegionEstimatedAction] = useState<
string | undefined
>(estimatedAction);
const setLiveRegionEstimatedActionDebounced = useMemo(
() => debounce(setLiveRegionEstimatedAction, 500),
[]
);
useEffect(() => {
setLiveRegionEstimatedActionDebounced(estimatedAction);
}, [setLiveRegionEstimatedActionDebounced, estimatedAction]);

return (
<VStack
className={tourElClassname.estimatedAction}
Expand All @@ -23,7 +37,7 @@ const PredictedAction = () => {
id="estimated-action-aria"
values={{
action:
predictionResult?.detected?.name ??
liveRegionEstimatedAction ??
intl.formatMessage({ id: "unknown" }),
}}
/>
Expand Down Expand Up @@ -52,7 +66,7 @@ const PredictedAction = () => {
textAlign="center"
w={`${predictedActionDisplayWidth}px`}
>
{predictionResult?.detected?.name ?? <FormattedMessage id="unknown" />}
{estimatedAction ?? <FormattedMessage id="unknown" />}
</Text>
</VStack>
);
Expand Down

0 comments on commit 7300aeb

Please sign in to comment.