Skip to content

Commit

Permalink
Simplified
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-matt-hillsdon committed Nov 22, 2024
1 parent b83ea98 commit a4b16dc
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions src/components/PredictedAction.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
HStack,
Text,
usePrevious,
VisuallyHidden,
VStack,
} from "@chakra-ui/react";
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";
Expand All @@ -17,27 +11,17 @@ import { predictedActionDisplayWidth } from "./LiveGraphPanel";
const PredictedAction = () => {
const intl = useIntl();
const predictionResult = useStore((s) => s.predictionResult);
const [estimatedAction, setEstimatedAction] = useState<string>(
intl.formatMessage({ id: "unknown" })
const estimatedAction = predictionResult?.detected?.name;
const [liveRegionEstimatedAction, setLiveRegionEstimatedAction] = useState<
string | undefined
>(estimatedAction);
const setLiveRegionEstimatedActionDebounced = useMemo(
() => debounce(setLiveRegionEstimatedAction, 500, { leading: true }),
[]
);
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]);
setLiveRegionEstimatedActionDebounced(estimatedAction);
}, [setLiveRegionEstimatedActionDebounced, estimatedAction]);

return (
<VStack
Expand All @@ -52,7 +36,9 @@ const PredictedAction = () => {
<FormattedMessage
id="estimated-action-aria"
values={{
action: estimatedAction,
action:
liveRegionEstimatedAction ??
intl.formatMessage({ id: "unknown" }),
}}
/>
</VisuallyHidden>
Expand Down Expand Up @@ -80,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 a4b16dc

Please sign in to comment.