Skip to content

Commit

Permalink
fix(app): generic "error" splash screen no longer displays instead of…
Browse files Browse the repository at this point in the history
… cancelling a run in some spots
  • Loading branch information
mjhuff committed Nov 20, 2024
1 parent d5b8482 commit 2f501a6
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions app/src/organisms/ErrorRecoveryFlows/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo, useEffect, useState } from 'react'
import { useMemo, useLayoutEffect, useState } from 'react'
import { useSelector } from 'react-redux'

import {
Expand Down Expand Up @@ -65,13 +65,15 @@ export function useErrorRecoveryFlows(
runStatus: RunStatus | null
): UseErrorRecoveryResult {
const [isERActive, setIsERActive] = useState(false)
const [hasSeenAwaitingRecovery, setHasSeenAwaitingRecovery] = useState(false)
const failedCommand = useCurrentlyRecoveringFrom(runId, runStatus)

// The complexity of this logic exists to persist Error Recovery screens past the server's definition of Error Recovery.
// Ex, show a "cancelling run" modal in Error Recovery flows despite the robot no longer being in a recoverable state.

const isValidERStatus = (status: RunStatus | null): boolean => {
const isValidERStatus = (
status: RunStatus | null,
hasSeenAwaitingRecovery: boolean
): boolean => {
return (
status !== null &&
(status === RUN_STATUS_AWAITING_RECOVERY ||
Expand All @@ -81,31 +83,20 @@ export function useErrorRecoveryFlows(

// If client accesses a valid ER runs status besides AWAITING_RECOVERY but accesses it outside of Error Recovery flows,
// don't show ER.
useEffect(() => {
useLayoutEffect(() => {
if (runStatus != null) {
const isAwaitingRecovery =
VALID_ER_RUN_STATUSES.includes(runStatus) &&
runStatus !== RUN_STATUS_STOP_REQUESTED
runStatus !== RUN_STATUS_STOP_REQUESTED &&
failedCommand != null // Prevents one render cycle of an unknown failed command.

if (isAwaitingRecovery && !hasSeenAwaitingRecovery) {
setHasSeenAwaitingRecovery(true)
if (isAwaitingRecovery) {
setIsERActive(isValidERStatus(runStatus, true))
} else if (INVALID_ER_RUN_STATUSES.includes(runStatus)) {
setHasSeenAwaitingRecovery(false)
setIsERActive(isValidERStatus(runStatus, false))
}
}
}, [runStatus, hasSeenAwaitingRecovery])

// Manage isERActive state, the condition that actually renders Error Recovery.
useEffect(() => {
const shouldBeActive =
isValidERStatus(runStatus) &&
// The failedCommand is null when a stop is requested, but we still want to persist Error Recovery in specific circumstances.
(failedCommand !== null || runStatus === RUN_STATUS_STOP_REQUESTED)

if (shouldBeActive !== isERActive) {
setIsERActive(shouldBeActive)
}
}, [runStatus, failedCommand, hasSeenAwaitingRecovery, isERActive])
}, [runStatus, failedCommand])

return {
isERActive,
Expand Down

0 comments on commit 2f501a6

Please sign in to comment.