-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app): improve top level query chaining (#16179)
Closes EXEC-695 #16084 highlighted some of the existing issues we had after the recent React Router migration. While that PR fixed some problems, the intention was to prioritize keeping the bug radius small for the upcoming 8.0 release, which meant some changes were not implemented optimally. Now that we have plenty of time before the 8.1 release, let's dog food networking changes for as long as possible, starting with this one: currently, TopLevelRedirects chains react queries, taking the currentRunId from useCurrentRunId and feeds it directly into useNotifyQuery. Whenever currentRunId is null, we GET /runs/null, which is a network request that we should avoid. After discussion, the most Reactive solution is to isolate the hooks into their own components, and conditionally render a component with the chained hook only if the hook(s) farther up the chain are not null. In other words, keep the concept of query chaining, but just do it in components.
- Loading branch information
Showing
8 changed files
with
233 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const CURRENT_RUN_POLL = 5000 |
146 changes: 146 additions & 0 deletions
146
app/src/App/ODDTopLevelRedirects/hooks/__tests__/useCurrentRunRoute.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { renderHook } from '@testing-library/react' | ||
import { describe, it, expect, vi } from 'vitest' | ||
import { useCurrentRunRoute } from '../useCurrentRunRoute' | ||
import { useNotifyRunQuery } from '../../../../resources/runs' | ||
import { | ||
RUN_STATUS_BLOCKED_BY_OPEN_DOOR, | ||
RUN_STATUS_FAILED, | ||
RUN_STATUS_IDLE, | ||
RUN_STATUS_STOPPED, | ||
RUN_STATUS_SUCCEEDED, | ||
} from '@opentrons/api-client' | ||
|
||
vi.mock('../../../../resources/runs') | ||
|
||
const MOCK_RUN_ID = 'MOCK_RUN_ID' | ||
|
||
describe('useCurrentRunRoute', () => { | ||
it('returns null when the run record is null', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: null, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBeNull() | ||
}) | ||
|
||
it('returns null when isFetching is true', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { data: { startedAt: '123' } }, | ||
isFetching: true, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBeNull() | ||
}) | ||
|
||
it('returns the summary route for a run with succeeded status', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { | ||
id: MOCK_RUN_ID, | ||
status: RUN_STATUS_SUCCEEDED, | ||
startedAt: '123', | ||
}, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/summary`) | ||
}) | ||
|
||
it('returns the summary route for a started run that has a stopped status', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { | ||
id: MOCK_RUN_ID, | ||
status: RUN_STATUS_STOPPED, | ||
startedAt: '123', | ||
}, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/summary`) | ||
}) | ||
|
||
it('returns summary route for a run with failed status', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { id: MOCK_RUN_ID, status: RUN_STATUS_FAILED, startedAt: '123' }, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/summary`) | ||
}) | ||
|
||
it('returns the setup route for a run with an idle status', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { id: MOCK_RUN_ID, status: RUN_STATUS_IDLE, startedAt: null }, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/setup`) | ||
}) | ||
|
||
it('returns the setup route for a "blocked by open door" run that has not been started yet', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { | ||
id: MOCK_RUN_ID, | ||
status: RUN_STATUS_BLOCKED_BY_OPEN_DOOR, | ||
startedAt: null, | ||
}, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/setup`) | ||
}) | ||
|
||
it('returns run route for a started run', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { | ||
id: MOCK_RUN_ID, | ||
status: RUN_STATUS_BLOCKED_BY_OPEN_DOOR, | ||
startedAt: '123', | ||
}, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBe(`/runs/${MOCK_RUN_ID}/run`) | ||
}) | ||
|
||
it('returns null for cancelled run before starting', () => { | ||
vi.mocked(useNotifyRunQuery).mockReturnValue({ | ||
data: { | ||
data: { id: MOCK_RUN_ID, status: RUN_STATUS_STOPPED, startedAt: null }, | ||
}, | ||
isFetching: false, | ||
} as any) | ||
|
||
const { result } = renderHook(() => useCurrentRunRoute(MOCK_RUN_ID)) | ||
|
||
expect(result.current).toBeNull() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useCurrentRunRoute } from './useCurrentRunRoute' |
42 changes: 42 additions & 0 deletions
42
app/src/App/ODDTopLevelRedirects/hooks/useCurrentRunRoute.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { | ||
RUN_STATUS_BLOCKED_BY_OPEN_DOOR, | ||
RUN_STATUS_FAILED, | ||
RUN_STATUS_IDLE, | ||
RUN_STATUS_STOPPED, | ||
RUN_STATUS_SUCCEEDED, | ||
} from '@opentrons/api-client' | ||
|
||
import { useNotifyRunQuery } from '../../../resources/runs' | ||
import { CURRENT_RUN_POLL } from '../constants' | ||
|
||
// Returns the route to which React Router should navigate, if any. | ||
export function useCurrentRunRoute(currentRunId: string): string | null { | ||
const { data: runRecord, isFetching } = useNotifyRunQuery(currentRunId, { | ||
refetchInterval: CURRENT_RUN_POLL, | ||
}) | ||
|
||
// grabbing run id off of the run query to have all routing info come from one source of truth | ||
const runId = runRecord?.data.id | ||
const hasRunStarted = runRecord?.data.startedAt != null | ||
const runStatus = runRecord?.data.status | ||
|
||
if (isFetching) { | ||
return null | ||
} else if ( | ||
runStatus === RUN_STATUS_SUCCEEDED || | ||
(runStatus === RUN_STATUS_STOPPED && hasRunStarted) || | ||
runStatus === RUN_STATUS_FAILED | ||
) { | ||
return `/runs/${runId}/summary` | ||
} else if ( | ||
runStatus === RUN_STATUS_IDLE || | ||
(!hasRunStarted && runStatus === RUN_STATUS_BLOCKED_BY_OPEN_DOOR) | ||
) { | ||
return `/runs/${runId}/setup` | ||
} else if (hasRunStarted) { | ||
return `/runs/${runId}/run` | ||
} else { | ||
// includes runs cancelled before starting and runs not yet started | ||
return null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as React from 'react' | ||
import { Navigate, Route, Routes } from 'react-router-dom' | ||
|
||
import { useCurrentRunId } from '../../resources/runs' | ||
import { CURRENT_RUN_POLL } from './constants' | ||
import { useCurrentRunRoute } from './hooks' | ||
|
||
export function ODDTopLevelRedirects(): JSX.Element | null { | ||
const currentRunId = useCurrentRunId({ refetchInterval: CURRENT_RUN_POLL }) | ||
|
||
return currentRunId != null ? ( | ||
<CurrentRunRoute currentRunId={currentRunId} /> | ||
) : null | ||
} | ||
|
||
function CurrentRunRoute({ | ||
currentRunId, | ||
}: { | ||
currentRunId: string | ||
}): JSX.Element | null { | ||
const currentRunRoute = useCurrentRunRoute(currentRunId) | ||
|
||
return currentRunRoute != null ? ( | ||
<Routes> | ||
<Route path="*" element={<Navigate to={currentRunRoute} />} /> | ||
</Routes> | ||
) : null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters