From 50e380d584c1bbdcc0de8e1485613989606c6e45 Mon Sep 17 00:00:00 2001 From: Eddasol Date: Tue, 9 Jan 2024 10:20:30 +0100 Subject: [PATCH] Improve blocked alert --- .../components/Alerts/BlockedRobotAlert.tsx | 13 ++++----- .../src/components/Contexts/AlertContext.tsx | 27 +++++++++++++++++++ .../RobotCards/RobotStatusSection.tsx | 18 +++---------- frontend/src/language/en.json | 3 ++- frontend/src/language/no.json | 3 ++- 5 files changed, 42 insertions(+), 22 deletions(-) diff --git a/frontend/src/components/Alerts/BlockedRobotAlert.tsx b/frontend/src/components/Alerts/BlockedRobotAlert.tsx index 83c60db71..115bf8129 100644 --- a/frontend/src/components/Alerts/BlockedRobotAlert.tsx +++ b/frontend/src/components/Alerts/BlockedRobotAlert.tsx @@ -3,7 +3,6 @@ import styled from 'styled-components' import { useLanguageContext } from 'components/Contexts/LanguageContext' import { Icons } from 'utils/icons' import { tokens } from '@equinor/eds-tokens' -import { Robot } from 'models/Robot' const StyledDiv = styled.div` align-items: center; @@ -20,10 +19,10 @@ const Indent = styled.div` ` interface AlertProps { - robot: Robot + robotNames: string[] } -export const BlockedRobotAlertContent = ({ robot }: AlertProps) => { +export const BlockedRobotAlertContent = ({ robotNames }: AlertProps) => { const { TranslateText } = useLanguageContext() return ( @@ -33,9 +32,11 @@ export const BlockedRobotAlertContent = ({ robot }: AlertProps) => { diff --git a/frontend/src/components/Contexts/AlertContext.tsx b/frontend/src/components/Contexts/AlertContext.tsx index df40dba41..8f747e1c2 100644 --- a/frontend/src/components/Contexts/AlertContext.tsx +++ b/frontend/src/components/Contexts/AlertContext.tsx @@ -8,6 +8,8 @@ import { useInstallationContext } from './InstallationContext' import { Alert } from 'models/Alert' import { FailedSafeZoneAlertContent } from 'components/Alerts/FailedSafeZoneAlertContent' import { useRobotContext } from './RobotContext' +import { BlockedRobotAlertContent } from 'components/Alerts/BlockedRobotAlert' +import { RobotStatus } from 'models/Robot' type AlertDictionaryType = { [key in AlertType]?: { content: ReactNode | undefined; dismissFunction: () => void } } @@ -45,6 +47,7 @@ export const AlertContext = createContext(defaultAlertInterface) export const AlertProvider: FC = ({ children }) => { const [alerts, setAlerts] = useState(defaultAlertInterface.alerts) const [recentFailedMissions, setRecentFailedMissions] = useState([]) + const [blockedRobotNames, setBlockedRobotNames] = useState([]) const { registerEvent, connectionReady } = useSignalRContext() const { installationCode } = useInstallationContext() const { enabledRobots } = useRobotContext() @@ -158,6 +161,30 @@ export const AlertProvider: FC = ({ children }) => { // eslint-disable-next-line react-hooks/exhaustive-deps }, [newFailedMissions]) + useEffect(() => { + const newBlockedRobotNames = enabledRobots + .filter( + (robot) => + robot.currentInstallation.installationCode.toLocaleLowerCase() === + installationCode.toLocaleLowerCase() && robot.status === RobotStatus.Blocked + ) + .map((robot) => robot.name!) + + const isBlockedRobotNamesModifyed = + newBlockedRobotNames.some((name) => !blockedRobotNames.includes(name)) || + newBlockedRobotNames.length !== blockedRobotNames.length + + if (isBlockedRobotNamesModifyed) { + if (newBlockedRobotNames.length > 0) { + setAlert(AlertType.BlockedRobot, ) + } else { + clearAlert(AlertType.BlockedRobot) + } + } + setBlockedRobotNames(newBlockedRobotNames) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [enabledRobots, installationCode]) + return ( { - return robot.status === 'Blocked' -} - export const RobotStatusSection = () => { const { TranslateText } = useLanguageContext() const { installationCode } = useInstallationContext() const { enabledRobots } = useRobotContext() const { switchSafeZoneStatus } = useSafeZoneContext() - const { setAlert } = useAlertContext() + const relevantRobots = enabledRobots .filter( (robot) => @@ -47,13 +41,9 @@ export const RobotStatusSection = () => { ) useEffect(() => { - const missionQueueFozenStatus = relevantRobots.some((robot: Robot) => robot.missionQueueFrozen) - switchSafeZoneStatus(missionQueueFozenStatus) - const blockedRobots = relevantRobots.filter(isRobotBlocked) - if (blockedRobots.length > 0) { - setAlert(AlertType.BlockedRobot, ) - } - }, [enabledRobots, installationCode, switchSafeZoneStatus, relevantRobots, setAlert]) + const missionQueueFrozenStatus = relevantRobots.some((robot: Robot) => robot.missionQueueFrozen) + switchSafeZoneStatus(missionQueueFrozenStatus) + }, [relevantRobots, switchSafeZoneStatus]) const robotDisplay = relevantRobots.map((robot) => ) diff --git a/frontend/src/language/en.json b/frontend/src/language/en.json index e2d50b444..d273c85e6 100644 --- a/frontend/src/language/en.json +++ b/frontend/src/language/en.json @@ -201,5 +201,6 @@ "Robot is blocked": "Robot is blocked", "The robot": "The robot", "is blocked and cannot perform tasks": "is blocked and cannot perform tasks", - "Blocked": "Blocked" + "Blocked": "Blocked", + "Several robots are blocked and cannot perform tasks.": "Several robots are blocked and cannot perform tasks." } diff --git a/frontend/src/language/no.json b/frontend/src/language/no.json index 1453bbd69..a2e05c022 100644 --- a/frontend/src/language/no.json +++ b/frontend/src/language/no.json @@ -201,5 +201,6 @@ "Robot is blocked": "Roboten er blokkert", "The robot": "Roboten", "is blocked and cannot perform tasks": "er blokkert og kan ikke utføre oppgaver", - "Blocked": "Blokkert" + "Blocked": "Blokkert", + "Several robots are blocked and cannot perform tasks.": "Flere roboter er blokkerte og kan ikke utføre oppgaver." }