Skip to content

Commit 59be09e

Browse files
authored
fix(app): fix stale error data displaying (#16305)
Closes RQA-3213 Before the network request completes when entering ER the 2nd time + in a run, useCurrentlyRecoveringFrom uses stale data. If the command that failed previously is the same command that failed currently, this gives it a flickering effect of "correct error, wrong error, correct error", but the stale data is more noticeable if you previously failed a command that wasn't the same kind as the current failure. To fix, let's just clear the query cache when we first enter recovery.
1 parent 65b0d7f commit 59be09e

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

app/src/organisms/ErrorRecoveryFlows/hooks/__tests__/useCurrentlyRecoveringFrom.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { vi, describe, it, expect } from 'vitest'
1+
import { vi, describe, it, expect, beforeEach } from 'vitest'
22
import { renderHook } from '@testing-library/react'
3+
import { useQueryClient } from 'react-query'
34

45
import { useCommandQuery } from '@opentrons/react-api-client'
56
import {
@@ -10,13 +11,25 @@ import {
1011
import { useNotifyAllCommandsQuery } from '../../../../resources/runs'
1112
import { useCurrentlyRecoveringFrom } from '../useCurrentlyRecoveringFrom'
1213

14+
import type { Mock } from 'vitest'
15+
1316
vi.mock('@opentrons/react-api-client')
1417
vi.mock('../../../../resources/runs')
18+
vi.mock('react-query')
1519

1620
const MOCK_RUN_ID = 'runId'
1721
const MOCK_COMMAND_ID = 'commandId'
1822

1923
describe('useCurrentlyRecoveringFrom', () => {
24+
let mockInvalidateQueries: Mock
25+
26+
beforeEach(() => {
27+
mockInvalidateQueries = vi.fn()
28+
vi.mocked(useQueryClient).mockReturnValue({
29+
invalidateQueries: mockInvalidateQueries,
30+
} as any)
31+
})
32+
2033
it('disables all queries if the run is not awaiting-recovery', () => {
2134
vi.mocked(useNotifyAllCommandsQuery).mockReturnValue({
2235
data: {
@@ -97,4 +110,12 @@ describe('useCurrentlyRecoveringFrom', () => {
97110
)
98111
expect(result.current).toStrictEqual('mockCommandDetails')
99112
})
113+
114+
it('calls invalidateQueries when the run enters recovery mode', () => {
115+
renderHook(() =>
116+
useCurrentlyRecoveringFrom(MOCK_RUN_ID, RUN_STATUS_AWAITING_RECOVERY)
117+
)
118+
119+
expect(mockInvalidateQueries).toHaveBeenCalled()
120+
})
100121
})

app/src/organisms/ErrorRecoveryFlows/hooks/useCurrentlyRecoveringFrom.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import * as React from 'react'
2+
import { useQueryClient } from 'react-query'
3+
14
import {
25
RUN_STATUS_AWAITING_RECOVERY,
36
RUN_STATUS_AWAITING_RECOVERY_BLOCKED_BY_OPEN_DOOR,
47
RUN_STATUS_AWAITING_RECOVERY_PAUSED,
58
} from '@opentrons/api-client'
6-
import { useCommandQuery } from '@opentrons/react-api-client'
9+
import { useCommandQuery, useHost } from '@opentrons/react-api-client'
710

811
import { useNotifyAllCommandsQuery } from '../../../resources/runs'
912

@@ -25,10 +28,19 @@ export function useCurrentlyRecoveringFrom(
2528
runId: string,
2629
runStatus: RunStatus | null
2730
): FailedCommand | null {
31+
const queryClient = useQueryClient()
32+
const host = useHost()
2833
// There can only be a currentlyRecoveringFrom command when the run is in recovery mode.
2934
// In case we're falling back to polling, only enable queries when that is the case.
3035
const isRunInRecoveryMode = VALID_RECOVERY_FETCH_STATUSES.includes(runStatus)
3136

37+
// Prevent stale data on subsequent recoveries by clearing the query cache at the start of each recovery.
38+
React.useEffect(() => {
39+
if (isRunInRecoveryMode) {
40+
void queryClient.invalidateQueries([host, 'runs', runId])
41+
}
42+
}, [isRunInRecoveryMode, host, runId])
43+
3244
const { data: allCommandsQueryData } = useNotifyAllCommandsQuery(
3345
runId,
3446
{ cursor: null, pageLength: 0 }, // pageLength 0 because we only care about the links.

app/src/organisms/ErrorRecoveryFlows/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ export function ErrorRecoveryFlows(
147147
failedCommand: failedCommandBySource,
148148
})
149149

150-
console.log('=>(index.tsx:180) showTakeover', showTakeover)
151-
152150
return (
153151
<>
154152
{showTakeover ? (

0 commit comments

Comments
 (0)