Skip to content

Commit

Permalink
Reinstate DockContext.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddasol committed Nov 12, 2024
1 parent 7e27d7c commit d9e0d0a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 16 deletions.
35 changes: 19 additions & 16 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { RobotProvider } from 'components/Contexts/RobotContext'
import { config } from 'config'
import { MissionDefinitionsProvider } from 'components/Contexts/MissionDefinitionsContext'
import { MediaStreamProvider } from 'components/Contexts/MediaStreamContext'
import { DockProvider } from 'components/Contexts/DockContext'

const appInsights = new ApplicationInsights({
config: {
Expand All @@ -35,22 +36,24 @@ const App = () => (
<RobotProvider>
<MissionRunsProvider>
<AlertProvider>
<MissionRunsProvider>
<MissionControlProvider>
<UnauthenticatedTemplate>
<div className="sign-in-page">
<AssetSelectionPage></AssetSelectionPage>
</div>
</UnauthenticatedTemplate>
<AuthenticatedTemplate>
<MissionFilterProvider>
<MediaStreamProvider>
<FlotillaSite />
</MediaStreamProvider>
</MissionFilterProvider>
</AuthenticatedTemplate>
</MissionControlProvider>
</MissionRunsProvider>
<DockProvider>
<MissionRunsProvider>
<MissionControlProvider>
<UnauthenticatedTemplate>
<div className="sign-in-page">
<AssetSelectionPage></AssetSelectionPage>
</div>
</UnauthenticatedTemplate>
<AuthenticatedTemplate>
<MissionFilterProvider>
<MediaStreamProvider>
<FlotillaSite />
</MediaStreamProvider>
</MissionFilterProvider>
</AuthenticatedTemplate>
</MissionControlProvider>
</MissionRunsProvider>
</DockProvider>
</AlertProvider>
</MissionRunsProvider>
</RobotProvider>
Expand Down
76 changes: 76 additions & 0 deletions frontend/src/components/Contexts/DockContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { createContext, FC, useEffect, useState } from 'react'
import { useRobotContext } from './RobotContext'
import { AlertType } from './AlertContext'
import { DockAlertContent, DockAlertListContent } from 'components/Alerts/DockAlert'
import { AlertCategory } from 'components/Alerts/AlertsBanner'
import { RobotFlotillaStatus } from 'models/Robot'
import { useAlertContext } from './AlertContext'

interface IDockContext {
DockStatus: boolean
}

interface Props {
children: React.ReactNode
}

const defaultDockInterface = {
DockStatus: false,
}

const DockContext = createContext<IDockContext>(defaultDockInterface)

export const DockProvider: FC<Props> = ({ children }) => {
const [DockStatus, setDockStatus] = useState<boolean>(defaultDockInterface.DockStatus)
const { enabledRobots } = useRobotContext()
const { setAlert, clearAlert, setListAlert, clearListAlert } = useAlertContext()

useEffect(() => {
const missionQueueFozenStatus = enabledRobots.filter(
(robot) => robot.flotillaStatus === RobotFlotillaStatus.Docked
)

if (missionQueueFozenStatus.length > 0 && DockStatus === false) {
setDockStatus((oldStatus) => !oldStatus)
clearListAlert(AlertType.DismissDock)
clearAlert(AlertType.DismissDock)
setListAlert(
AlertType.RequestDock,
<DockAlertListContent alertType={AlertType.RequestDock} alertCategory={AlertCategory.WARNING} />,
AlertCategory.WARNING
)
setAlert(
AlertType.RequestDock,
<DockAlertContent alertType={AlertType.RequestDock} alertCategory={AlertCategory.WARNING} />,
AlertCategory.WARNING
)
} else if (missionQueueFozenStatus.length === 0 && DockStatus === true) {
setDockStatus((oldStatus) => !oldStatus)
clearListAlert(AlertType.RequestDock)
clearListAlert(AlertType.DockSuccess)
clearAlert(AlertType.RequestDock)
clearAlert(AlertType.DockSuccess)
setListAlert(
AlertType.DismissDock,
<DockAlertListContent alertType={AlertType.DismissDock} alertCategory={AlertCategory.INFO} />,
AlertCategory.INFO
)
setAlert(
AlertType.DismissDock,
<DockAlertContent alertType={AlertType.DismissDock} alertCategory={AlertCategory.INFO} />,
AlertCategory.INFO
)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [enabledRobots])

return (
<DockContext.Provider
value={{
DockStatus,
}}
>
{children}
</DockContext.Provider>
)
}

0 comments on commit d9e0d0a

Please sign in to comment.