@@ -286,36 +286,6 @@ export const AlertProvider: FC<Props> = ({ children }) => {
286286 }
287287 } , [ registerEvent , connectionReady , installation , enabledRobots ] )
288288
289- useEffect ( ( ) => {
290- if ( recentFailedMissions . length > 0 ) {
291- setAlert (
292- AlertType . MissionFail ,
293- < FailedMissionAlertContent missions = { recentFailedMissions } /> ,
294- AlertCategory . ERROR
295- )
296- setListAlert (
297- AlertType . MissionFail ,
298- < FailedMissionAlertListContent missions = { recentFailedMissions } /> ,
299- AlertCategory . ERROR
300- )
301- }
302- } , [ recentFailedMissions ] )
303-
304- useEffect ( ( ) => {
305- if ( Object . keys ( autoScheduleFailedMissionDict ) . length > 0 ) {
306- setListAlert (
307- AlertType . AutoScheduleFail ,
308- < FailedAutoMissionAlertContent autoScheduleFailedMissionDict = { autoScheduleFailedMissionDict } /> ,
309- AlertCategory . ERROR
310- )
311- setAlert (
312- AlertType . AutoScheduleFail ,
313- < FailedAutoMissionAlertContent autoScheduleFailedMissionDict = { autoScheduleFailedMissionDict } /> ,
314- AlertCategory . ERROR
315- )
316- }
317- } , [ connectionReady , autoScheduleFailedMissionDict ] )
318-
319289 const robotsWithFrozenQueue = enabledRobots . filter ( ( robot ) => robot . status === RobotStatus . Lockdown )
320290
321291 const getActiveSendToDockAlertType = ( ) => {
@@ -324,48 +294,80 @@ export const AlertProvider: FC<Props> = ({ children }) => {
324294 else return AlertType . DockSuccess
325295 }
326296
297+ const computedDockAlertType = getActiveSendToDockAlertType ( )
327298 const [ activeSendToDockAlertType , setActiveSendToDockAlertType ] = useState < AlertType | undefined > (
328- getActiveSendToDockAlertType ( )
299+ computedDockAlertType
329300 )
301+ const [ prevComputedDockAlertType , setPrevComputedDockAlertType ] = useState < AlertType | undefined > (
302+ computedDockAlertType
303+ )
304+ const [ dismissedDockAlertType , setDismissedDockAlertType ] = useState < AlertType | undefined > ( undefined )
305+
306+ if ( computedDockAlertType !== prevComputedDockAlertType ) {
307+ setPrevComputedDockAlertType ( computedDockAlertType )
308+ setDismissedDockAlertType ( undefined )
309+ setActiveSendToDockAlertType ( ( current ) => {
310+ if ( current === computedDockAlertType ) return current
311+ if ( current !== undefined && computedDockAlertType === undefined ) return AlertType . DismissDock
312+ return computedDockAlertType
313+ } )
314+ }
330315
331- useEffect ( ( ) => {
332- let newActiveSendToDockAlertType = getActiveSendToDockAlertType ( )
333- if ( activeSendToDockAlertType === newActiveSendToDockAlertType ) return
316+ const showDockAlert =
317+ activeSendToDockAlertType !== undefined && activeSendToDockAlertType !== dismissedDockAlertType
318+
319+ const combinedAlerts : AlertDictionaryType = { ...alerts }
320+ const combinedListAlerts : AlertDictionaryType = { ...listAlerts }
334321
335- if ( activeSendToDockAlertType !== undefined && newActiveSendToDockAlertType === undefined ) {
336- newActiveSendToDockAlertType = AlertType . DismissDock
322+ if ( recentFailedMissions . length > 0 ) {
323+ combinedAlerts [ AlertType . MissionFail ] = {
324+ content : < FailedMissionAlertContent missions = { recentFailedMissions } /> ,
325+ dismissFunction : ( ) => clearAlert ( AlertType . MissionFail ) ,
326+ alertCategory : AlertCategory . ERROR ,
337327 }
338- setActiveSendToDockAlertType ( newActiveSendToDockAlertType )
339- } , [ robotsWithFrozenQueue ] )
328+ combinedListAlerts [ AlertType . MissionFail ] = {
329+ content : < FailedMissionAlertListContent missions = { recentFailedMissions } /> ,
330+ dismissFunction : ( ) => clearListAlert ( AlertType . MissionFail ) ,
331+ alertCategory : AlertCategory . ERROR ,
332+ }
333+ }
340334
341- useEffect ( ( ) => {
342- clearListAlert ( AlertType . DismissDock )
343- clearAlert ( AlertType . DismissDock )
344- clearListAlert ( AlertType . RequestDock )
345- clearAlert ( AlertType . RequestDock )
346- clearListAlert ( AlertType . DockSuccess )
347- clearAlert ( AlertType . DockSuccess )
348-
349- if ( activeSendToDockAlertType === undefined ) return
350- const alertCategory =
351- activeSendToDockAlertType === AlertType . RequestDock ? AlertCategory . WARNING : AlertCategory . INFO
335+ if ( Object . keys ( autoScheduleFailedMissionDict ) . length > 0 ) {
336+ combinedAlerts [ AlertType . AutoScheduleFail ] = {
337+ content : < FailedAutoMissionAlertContent autoScheduleFailedMissionDict = { autoScheduleFailedMissionDict } /> ,
338+ dismissFunction : ( ) => clearAlert ( AlertType . AutoScheduleFail ) ,
339+ alertCategory : AlertCategory . ERROR ,
340+ }
341+ combinedListAlerts [ AlertType . AutoScheduleFail ] = {
342+ content : < FailedAutoMissionAlertContent autoScheduleFailedMissionDict = { autoScheduleFailedMissionDict } /> ,
343+ dismissFunction : ( ) => clearListAlert ( AlertType . AutoScheduleFail ) ,
344+ alertCategory : AlertCategory . ERROR ,
345+ }
346+ }
352347
353- setListAlert (
354- AlertType . RequestDock ,
355- < DockAlertListContent alertType = { activeSendToDockAlertType } /> ,
356- alertCategory
357- )
358- setAlert ( AlertType . RequestDock , < DockAlertContent alertType = { activeSendToDockAlertType } /> , alertCategory )
359- } , [ activeSendToDockAlertType ] )
348+ if ( showDockAlert ) {
349+ const dockAlertCategory =
350+ activeSendToDockAlertType === AlertType . RequestDock ? AlertCategory . WARNING : AlertCategory . INFO
351+ combinedAlerts [ AlertType . RequestDock ] = {
352+ content : < DockAlertContent alertType = { activeSendToDockAlertType ! } /> ,
353+ dismissFunction : ( ) => setDismissedDockAlertType ( activeSendToDockAlertType ) ,
354+ alertCategory : dockAlertCategory ,
355+ }
356+ combinedListAlerts [ AlertType . RequestDock ] = {
357+ content : < DockAlertListContent alertType = { activeSendToDockAlertType ! } /> ,
358+ dismissFunction : ( ) => setDismissedDockAlertType ( activeSendToDockAlertType ) ,
359+ alertCategory : dockAlertCategory ,
360+ }
361+ }
360362
361363 return (
362364 < AlertContext . Provider
363365 value = { {
364- alerts,
366+ alerts : combinedAlerts ,
365367 setAlert,
366368 clearAlerts,
367369 clearAlert,
368- listAlerts,
370+ listAlerts : combinedListAlerts ,
369371 setListAlert,
370372 clearListAlerts,
371373 clearListAlert,
0 commit comments