Skip to content

Commit

Permalink
Move installation filtering to contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Aug 1, 2024
1 parent 86ef1f4 commit c64a0fc
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 128 deletions.
15 changes: 2 additions & 13 deletions frontend/src/components/Contexts/AlertContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,7 @@ export const AlertProvider: FC<Props> = ({ children }) => {
const backendAlert: Alert = JSON.parse(message)
const alertType = alertTypeEnumMap[backendAlert.alertCode]

if (backendAlert.robotId !== null) {
const relevantRobots = enabledRobots.filter((r) => r.id === backendAlert.robotId)
if (!relevantRobots) return
const relevantRobot = relevantRobots[0]
if (relevantRobot.currentInstallation.installationCode !== installationCode) return

// Here we could update the robot state manually, but this is best done on the backend
}
if (backendAlert.robotId !== null && !enabledRobots.filter((r) => r.id === backendAlert.robotId)) return

if (alertType === AlertType.SafeZoneSuccess) {
setAlert(
Expand Down Expand Up @@ -211,11 +204,7 @@ export const AlertProvider: FC<Props> = ({ children }) => {

useEffect(() => {
const newBlockedRobotNames = enabledRobots
.filter(
(robot) =>
robot.currentInstallation.installationCode.toLocaleLowerCase() ===
installationCode.toLocaleLowerCase() && robot.status === RobotStatus.Blocked
)
.filter((robot) => robot.status === RobotStatus.Blocked)
.map((robot) => robot.name!)

const isBlockedRobotNamesModifyed =
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/components/Contexts/InstallationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,21 @@ export const InstallationProvider: FC<Props> = ({ children }) => {
window.localStorage.setItem('installationCode', derivedCode)
}

const [filteredInstallationDecks, setFilteredInstallationDecks] = useState<Deck[]>([])
const [filteredInstallationAreas, setFilteredInstallationAreas] = useState<Area[]>([])
useEffect(() => {
setFilteredInstallationDecks(installationDecks.filter((d) => d.installationCode === installationCode))
setFilteredInstallationAreas(installationAreas.filter((a) => a.installationCode === installationCode))
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [installationCode, installationDecks, installationAreas])

return (
<InstallationContext.Provider
value={{
installationCode,
installationName,
installationDecks,
installationAreas,
installationDecks: filteredInstallationDecks,
installationAreas: filteredInstallationAreas,
switchInstallation,
}}
>
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/components/Contexts/MissionDefinitionsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,16 @@ export const useMissionDefinitions = (): IMissionDefinitionsContext => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [BackendAPICaller.accessToken, installationCode])

return { missionDefinitions }
const [filteredMissionDefinitions, setFilteredMissionDefinitions] = useState<CondensedMissionDefinition[]>([])

useEffect(() => {
setFilteredMissionDefinitions(
missionDefinitions.filter((m) => m.installationCode.toLowerCase() === installationCode.toLowerCase())
)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [installationCode, missionDefinitions])

return { missionDefinitions: filteredMissionDefinitions }
}

export const MissionDefinitionsProvider: FC<Props> = ({ children }) => {
Expand Down
17 changes: 16 additions & 1 deletion frontend/src/components/Contexts/MissionRunsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useLanguageContext } from './LanguageContext'
import { AlertType, useAlertContext } from './AlertContext'
import { FailedRequestAlertContent } from 'components/Alerts/FailedRequestAlert'
import { AlertCategory } from 'components/Alerts/AlertsBanner'
import { useInstallationContext } from './InstallationContext'

const upsertMissionList = (list: Mission[], mission: Mission) => {
let newMissionList = [...list]
Expand Down Expand Up @@ -82,6 +83,7 @@ export const useMissionRuns = (): IMissionRunsContext => {
const { registerEvent, connectionReady } = useSignalRContext()
const { TranslateText } = useLanguageContext()
const { setAlert } = useAlertContext()
const { installationCode } = useInstallationContext()

useEffect(() => {
if (connectionReady) {
Expand Down Expand Up @@ -157,7 +159,20 @@ export const useMissionRuns = (): IMissionRunsContext => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [BackendAPICaller.accessToken])

return { ongoingMissions, missionQueue, loadingMissionSet, setLoadingMissionSet }
const [filteredMissionQueue, setFilteredMissionQueue] = useState<Mission[]>([])
const [filteredOngoingMissions, setFilteredOngoingMissions] = useState<Mission[]>([])
useEffect(() => {
setFilteredOngoingMissions(ongoingMissions.filter((m) => m.area?.installationCode === installationCode))
setFilteredMissionQueue(missionQueue.filter((m) => m.area?.installationCode === installationCode))
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [installationCode, ongoingMissions, missionQueue])

return {
ongoingMissions: filteredOngoingMissions,
missionQueue: filteredMissionQueue,
loadingMissionSet,
setLoadingMissionSet,
}
}

export const MissionRunsProvider: FC<Props> = ({ children }) => {
Expand Down
15 changes: 14 additions & 1 deletion frontend/src/components/Contexts/RobotContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useLanguageContext } from './LanguageContext'
import { AlertType, useAlertContext } from './AlertContext'
import { FailedRequestAlertContent } from 'components/Alerts/FailedRequestAlert'
import { AlertCategory } from 'components/Alerts/AlertsBanner'
import { useInstallationContext } from './InstallationContext'

const upsertRobotList = (list: Robot[], mission: Robot) => {
let newList = [...list]
Expand Down Expand Up @@ -34,6 +35,7 @@ export const RobotProvider: FC<Props> = ({ children }) => {
const { registerEvent, connectionReady } = useSignalRContext()
const { TranslateText } = useLanguageContext()
const { setAlert } = useAlertContext()
const { installationCode } = useInstallationContext()

useEffect(() => {
if (connectionReady) {
Expand Down Expand Up @@ -86,10 +88,21 @@ export const RobotProvider: FC<Props> = ({ children }) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

const [filteredRobots, setFilteredRobots] = useState<Robot[]>([])

useEffect(() => {
setFilteredRobots(
enabledRobots.filter(
(r) => r.currentInstallation.installationCode.toLowerCase() === installationCode.toLowerCase()
)
)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [installationCode, enabledRobots])

return (
<RobotContext.Provider
value={{
enabledRobots,
enabledRobots: filteredRobots,
}}
>
{children}
Expand Down
12 changes: 3 additions & 9 deletions frontend/src/components/Contexts/SafeZoneContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createContext, FC, useContext, useEffect, useState } from 'react'
import { useRobotContext } from './RobotContext'
import { useInstallationContext } from './InstallationContext'
import { AlertType, useAlertContext } from './AlertContext'
import { SafeZoneAlertContent } from 'components/Alerts/SafeZoneAlert'
import { AlertCategory } from 'components/Alerts/AlertsBanner'
Expand All @@ -23,17 +22,12 @@ export const SafeZoneContext = createContext<ISafeZoneContext>(defaultSafeZoneIn
export const SafeZoneProvider: FC<Props> = ({ children }) => {
const [safeZoneStatus, setSafeZoneStatus] = useState<boolean>(defaultSafeZoneInterface.safeZoneStatus)
const { enabledRobots } = useRobotContext()
const { installationCode } = useInstallationContext()
const { setAlert, clearAlert } = useAlertContext()

useEffect(() => {
const missionQueueFozenStatus = enabledRobots
.filter(
(robot) =>
robot.currentInstallation.installationCode.toLocaleLowerCase() ===
installationCode.toLocaleLowerCase()
)
.filter((robot) => robot.flotillaStatus === RobotFlotillaStatus.SafeZone)
const missionQueueFozenStatus = enabledRobots.filter(
(robot) => robot.flotillaStatus === RobotFlotillaStatus.SafeZone
)

if (missionQueueFozenStatus.length > 0 && safeZoneStatus === false) {
setSafeZoneStatus((oldStatus) => !oldStatus)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Mission, placeholderMission } from 'models/Mission'
import { EmptyMissionQueuePlaceholder } from './NoMissionPlaceholder'
import { useLanguageContext } from 'components/Contexts/LanguageContext'
import { useMissionsContext } from 'components/Contexts/MissionRunsContext'
import { useInstallationContext } from 'components/Contexts/InstallationContext'
import { AlertType, useAlertContext } from 'components/Contexts/AlertContext'
import { FailedRequestAlertContent } from 'components/Alerts/FailedRequestAlert'
import { FrontPageSectionId } from 'models/FrontPageSectionId'
Expand All @@ -30,11 +29,7 @@ const MissionTable = styled.div`
export const MissionQueueView = (): JSX.Element => {
const { TranslateText } = useLanguageContext()
const { missionQueue, ongoingMissions, loadingMissionSet, setLoadingMissionSet } = useMissionsContext()
const { installationCode } = useInstallationContext()
const { setAlert } = useAlertContext()
const localMissionQueue = missionQueue.filter(
(m) => m.installationCode?.toLocaleLowerCase() === installationCode.toLocaleLowerCase()
)

const onDeleteMission = (mission: Mission) =>
BackendAPICaller.deleteMission(mission.id).catch((_) =>
Expand All @@ -55,7 +50,7 @@ export const MissionQueueView = (): JSX.Element => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [missionQueue, ongoingMissions])

const missionQueueDisplay = localMissionQueue.map((mission, index) => (
const missionQueueDisplay = missionQueue.map((mission, index) => (
<MissionQueueCard key={index} order={index + 1} mission={mission} onDeleteMission={onDeleteMission} />
))

Expand All @@ -74,9 +69,9 @@ export const MissionQueueView = (): JSX.Element => {
{TranslateText('Mission Queue')}
</Typography>
<MissionTable>
{localMissionQueue.length > 0 && missionQueueDisplay}
{missionQueue.length > 0 && missionQueueDisplay}
{loadingMissionSet.size > 0 && loadingQueueDisplay}
{loadingMissionSet.size === 0 && localMissionQueue.length === 0 && <EmptyMissionQueuePlaceholder />}
{loadingMissionSet.size === 0 && missionQueue.length === 0 && <EmptyMissionQueuePlaceholder />}
</MissionTable>
</StyledMissionView>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { useNavigate } from 'react-router-dom'
import { config } from 'config'
import { Icons } from 'utils/icons'
import { useMissionsContext } from 'components/Contexts/MissionRunsContext'
import { useInstallationContext } from 'components/Contexts/InstallationContext'
import { useEffect, useState } from 'react'
import { Mission } from 'models/Mission'
import { tokens } from '@equinor/eds-tokens'

const StyledOngoingMissionView = styled.div`
Expand Down Expand Up @@ -42,24 +39,14 @@ const StyledButton = styled(Button)`
export const OngoingMissionView = (): JSX.Element => {
const { TranslateText } = useLanguageContext()
const { ongoingMissions } = useMissionsContext()
const { installationCode } = useInstallationContext()
const [ongingMissionsToDisplay, setOngoingMissionsToDisplay] = useState<Mission[]>([])

let navigate = useNavigate()
const routeChange = () => {
const path = `${config.FRONTEND_BASE_ROUTE}/history`
navigate(path)
}

useEffect(() => {
setOngoingMissionsToDisplay(
ongoingMissions.filter(
(m) => m.installationCode?.toLocaleLowerCase() === installationCode.toLocaleLowerCase()
)
)
}, [ongoingMissions, installationCode])

const ongoingMissionCards = ongingMissionsToDisplay.map((mission, index) => (
const ongoingMissionCards = ongoingMissions.map((mission, index) => (
<OngoingMissionCard key={index} mission={mission} />
))

Expand All @@ -71,8 +58,8 @@ export const OngoingMissionView = (): JSX.Element => {
</Typography>
</OngoingMissionHeader>
<OngoingMissionSection>
{ongingMissionsToDisplay.length > 0 && ongoingMissionCards}
{ongingMissionsToDisplay.length === 0 && <NoOngoingMissionsPlaceholder />}
{ongoingMissions.length > 0 && ongoingMissionCards}
{ongoingMissions.length === 0 && <NoOngoingMissionsPlaceholder />}
</OngoingMissionSection>
<ButtonStyle>
<StyledButton variant="outlined" onClick={routeChange}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,12 @@ const SelectRobotComponent = memo(
}) => {
const { enabledRobots } = useRobotContext()
const { TranslateText } = useLanguageContext()
const { installationCode } = useInstallationContext()

return (
<Autocomplete
optionLabel={(r) => (r ? r.name + ' (' + r.model.type + ')' : '')}
options={enabledRobots.filter(
(r) =>
r.currentInstallation.installationCode.toLocaleLowerCase() ===
installationCode.toLocaleLowerCase() &&
(r.status === RobotStatus.Available || r.status === RobotStatus.Busy) &&
r.isarConnected
(r) => (r.status === RobotStatus.Available || r.status === RobotStatus.Busy) && r.isarConnected
)}
disabled={!enabledRobots}
selectedOptions={[selectedRobot]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ export const StopRobotDialog = (): JSX.Element => {
const { installationCode } = useInstallationContext()
const { setAlert } = useAlertContext()

const safeZoneActivated =
enabledRobots.find(
(r) =>
r.currentInstallation.installationCode === installationCode &&
r.flotillaStatus === RobotFlotillaStatus.SafeZone
) !== undefined
const safeZoneActivated = enabledRobots.find((r) => r.flotillaStatus === RobotFlotillaStatus.SafeZone) !== undefined

const openDialog = async () => {
setIsStopRobotDialogOpen(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Typography } from '@equinor/eds-core-react'
import styled from 'styled-components'
import { RobotStatusCard, RobotStatusCardPlaceholder } from './RobotStatusCard'
import { useInstallationContext } from 'components/Contexts/InstallationContext'
import { useLanguageContext } from 'components/Contexts/LanguageContext'
import { useRobotContext } from 'components/Contexts/RobotContext'

Expand All @@ -18,23 +17,17 @@ const RobotView = styled.div`

export const RobotStatusSection = () => {
const { TranslateText } = useLanguageContext()
const { installationCode } = useInstallationContext()
const { enabledRobots } = useRobotContext()

const relevantRobots = enabledRobots
.filter(
(robot) =>
robot.currentInstallation.installationCode.toLocaleLowerCase() === installationCode.toLocaleLowerCase()
)
.sort((robot, robotToCompareWith) =>
robot.status! !== robotToCompareWith.status!
? robot.status! > robotToCompareWith.status!
? 1
: -1
: robot.name! > robotToCompareWith.name!
? 1
: -1
)
const relevantRobots = enabledRobots.sort((robot, robotToCompareWith) =>
robot.status! !== robotToCompareWith.status!
? robot.status! > robotToCompareWith.status!
? 1
: -1
: robot.name! > robotToCompareWith.name!
? 1
: -1
)

const robotDisplay = relevantRobots.map((robot) => <RobotStatusCard key={robot.id} robot={robot} />)

Expand Down
7 changes: 1 addition & 6 deletions frontend/src/components/Pages/InspectionPage/DeckCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { Icons } from 'utils/icons'
import { tokens } from '@equinor/eds-tokens'
import { useMissionsContext } from 'components/Contexts/MissionRunsContext'
import { useRobotContext } from 'components/Contexts/RobotContext'
import { useInstallationContext } from 'components/Contexts/InstallationContext'

interface IDeckCardProps {
deckMissions: DeckInspectionTuple[]
Expand All @@ -33,12 +32,8 @@ const DeckCard = ({ deckData, onClickDeck, selectedDeck, handleScheduleAll }: De
const { TranslateText } = useLanguageContext()
const { ongoingMissions } = useMissionsContext()
const { enabledRobots } = useRobotContext()
const { installationCode } = useInstallationContext()

const isScheduleMissionsDisabled =
enabledRobots.filter((r) => r.currentInstallation.installationCode === installationCode).length === 0 ||
installationCode === '' ||
deckData.inspections.length === 0
const isScheduleMissionsDisabled = enabledRobots.length === 0 || deckData.inspections.length === 0

const getCardColorFromInspections = (inspections: Inspection[]): DeckCardColors => {
if (inspections.length === 0) return DeckCardColors.Gray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,18 @@ export const InspectionOverviewSection = () => {
const [echoMissions, setEchoMissions] = useState<EchoMissionDefinition[]>([])
const [activeTab, setActiveTab] = useState(0)

const isScheduleButtonDisabled =
enabledRobots.filter((r) => r.currentInstallation.installationCode === installationCode).length === 0 ||
installationCode === ''
const isScheduleButtonDisabled = enabledRobots.length === 0 || installationCode === ''

const anchorRef = useRef<HTMLButtonElement>(null)

const allInspections = missionDefinitions
.filter((m) => m.installationCode === installationCode)
.map((m) => {
return {
missionDefinition: m,
deadline: m.lastSuccessfulRun
? getInspectionDeadline(m.inspectionFrequency, m.lastSuccessfulRun.endTime!)
: undefined,
}
})
const allInspections = missionDefinitions.map((m) => {
return {
missionDefinition: m,
deadline: m.lastSuccessfulRun
? getInspectionDeadline(m.inspectionFrequency, m.lastSuccessfulRun.endTime!)
: undefined,
}
})

const fetchEchoMissions = () => {
setIsFetchingEchoMissions(true)
Expand Down
Loading

0 comments on commit c64a0fc

Please sign in to comment.