From b83ea98670de23056c9d24c4806c8ba3434d10b9 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Tue, 19 Nov 2024 14:16:27 +0000 Subject: [PATCH] Debounce estimated action aria-live announcement --- src/components/PredictedAction.tsx | 38 ++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/components/PredictedAction.tsx b/src/components/PredictedAction.tsx index 5b9846f83..a76da0e3f 100644 --- a/src/components/PredictedAction.tsx +++ b/src/components/PredictedAction.tsx @@ -1,14 +1,44 @@ -import { HStack, Text, VisuallyHidden, VStack } from "@chakra-ui/react"; +import { + HStack, + Text, + usePrevious, + 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, setEstimatedAction] = useState( + intl.formatMessage({ id: "unknown" }) + ); + const debouncedEstimatedAction = useMemo( + () => + debounce((name: string | undefined) => { + if (name) { + setEstimatedAction(name); + } else { + setEstimatedAction(intl.formatMessage({ id: "unknown" })); + } + }, 500), + [intl] + ); + + const prevEstimatedAction = usePrevious(predictionResult?.detected?.name); + useEffect(() => { + if (prevEstimatedAction !== predictionResult?.detected?.name) { + debouncedEstimatedAction(predictionResult?.detected?.name); + } + }, [debouncedEstimatedAction, predictionResult, prevEstimatedAction]); + return ( {