Skip to content

Commit

Permalink
fix(app): allow completed runs when starting protocol
Browse files Browse the repository at this point in the history
This means that people don't have to make sure to explicitly close the
run context by clicking the x button in the post-run flow before picking
a new robot to run.

Use the current run's completed at timestamp to decide whether or not
the run is complete for the purposes of idle. This requires fetching the
current run, which we do by ID.

Closes EXEC-519
  • Loading branch information
sfoster1 committed Jul 30, 2024
1 parent 905258b commit f6b6f4c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 14 deletions.
38 changes: 30 additions & 8 deletions app/src/organisms/ChooseRobotSlideout/AvailableRobotOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { getNetworkInterfaces, fetchStatus } from '../../redux/networking'
import { appShellRequestor } from '../../redux/shell/remote'
import OT2_PNG from '../../assets/images/OT2-R_HERO.png'
import FLEX_PNG from '../../assets/images/FLEX.png'
import { useNotifyAllRunsQuery } from '../../resources/runs'
import { useCurrentRunId, useNotifyRunQuery } from '../../resources/runs'

import type { IconName } from '@opentrons/components'
import type { Runs } from '@opentrons/api-client'
Expand Down Expand Up @@ -59,14 +59,15 @@ export function AvailableRobotOption(
getRobotModelByName(state, robotName)
)

const { data: runsData } = useNotifyAllRunsQuery(
{ pageLength: 0 },
const [isBusy, setIsBusy] = React.useState(true)

const currentRunId = useCurrentRunId(
{
onSuccess: data => {
if ((data as Runs)?.links?.current != null)
registerRobotBusyStatus({ type: 'robotIsBusy', robotName })
else {
const definitelyIdle = (data as Runs)?.links?.current == null
if (definitelyIdle) {
registerRobotBusyStatus({ type: 'robotIsIdle', robotName })
setIsBusy(false)
}
},
},
Expand All @@ -75,7 +76,28 @@ export function AvailableRobotOption(
requestor: ip === OPENTRONS_USB ? appShellRequestor : undefined,
}
)
const robotHasCurrentRun = runsData?.links?.current != null

useNotifyRunQuery(
currentRunId,
{
onSuccess: data => {
const busy = data?.data != null && data.data.completedAt == null
registerRobotBusyStatus({
type: busy ? 'robotIsBusy' : 'robotIsIdle',
robotName,
})
setIsBusy(busy)
},
onError: () => {
registerRobotBusyStatus({ type: 'robotIsIdle', robotName })
setIsBusy(false)
},
},
{
hostname: ip,
requestor: ip === OPENTRONS_USB ? appShellRequestor : undefined,
}
)

const { ethernet, wifi } = useSelector((state: State) =>
getNetworkInterfaces(state, robotName)
Expand All @@ -95,7 +117,7 @@ export function AvailableRobotOption(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

return showIdleOnly && robotHasCurrentRun ? null : (
return showIdleOnly && isBusy ? null : (
<>
<MiniCard
onClick={onClick}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { when } from 'vitest-when'
import { renderWithProviders } from '../../../__testing-utils__'
import { i18n } from '../../../i18n'
import { useTrackCreateProtocolRunEvent } from '../../../organisms/Devices/hooks'
import {
useCloseCurrentRun,
useCurrentRunId,
} from '../../../organisms/ProtocolUpload/hooks'
import { useCloseCurrentRun } from '../../../organisms/ProtocolUpload/hooks'
import { useCurrentRunStatus } from '../../../organisms/RunTimeControl/hooks'
import {
getConnectableRobots,
Expand All @@ -34,8 +31,11 @@ import { useCreateRunFromProtocol } from '../useCreateRunFromProtocol'
import { useOffsetCandidatesForAnalysis } from '../../ApplyHistoricOffsets/hooks/useOffsetCandidatesForAnalysis'
import { ChooseRobotToRunProtocolSlideout } from '../'
import { useNotifyDataReady } from '../../../resources/useNotifyDataReady'
import { useCurrentRunId } from '../../../resources/runs'

import type { State } from '../../../redux/types'
import type { Runs } from '@opentrons/api-client'

Check failure on line 37 in app/src/organisms/ChooseRobotToRunProtocolSlideout/__tests__/ChooseRobotToRunProtocolSlideout.test.tsx

View workflow job for this annotation

GitHub Actions / js checks

'Runs' is defined but never used

Check failure on line 37 in app/src/organisms/ChooseRobotToRunProtocolSlideout/__tests__/ChooseRobotToRunProtocolSlideout.test.tsx

View workflow job for this annotation

GitHub Actions / js checks

'Runs' is defined but never used
import { UseAllRunsQueryOptions } from '@opentrons/react-api-client/src/runs/useAllRunsQuery'

Check failure on line 38 in app/src/organisms/ChooseRobotToRunProtocolSlideout/__tests__/ChooseRobotToRunProtocolSlideout.test.tsx

View workflow job for this annotation

GitHub Actions / js checks

All imports in the declaration are only used as types. Use `import type`

Check failure on line 38 in app/src/organisms/ChooseRobotToRunProtocolSlideout/__tests__/ChooseRobotToRunProtocolSlideout.test.tsx

View workflow job for this annotation

GitHub Actions / js checks

All imports in the declaration are only used as types. Use `import type`

vi.mock('../../../organisms/Devices/hooks')
vi.mock('../../../organisms/ProtocolUpload/hooks')
Expand All @@ -46,6 +46,7 @@ vi.mock('../../../redux/networking')
vi.mock('../useCreateRunFromProtocol')
vi.mock('../../ApplyHistoricOffsets/hooks/useOffsetCandidatesForAnalysis')
vi.mock('../../../resources/useNotifyDataReady')
vi.mock('../../../resources/runs')

const render = (
props: React.ComponentProps<typeof ChooseRobotToRunProtocolSlideout>
Expand Down Expand Up @@ -88,7 +89,7 @@ describe('ChooseRobotToRunProtocolSlideout', () => {
isClosingCurrentRun: false,
closeCurrentRun: mockCloseCurrentRun,
})
vi.mocked(useCurrentRunId).mockReturnValue(null)
provideNullCurrentRunIdFor(mockConnectableRobot.ip)
vi.mocked(useCurrentRunStatus).mockReturnValue(null)
when(vi.mocked(useCreateRunFromProtocol))
.calledWith(
Expand Down Expand Up @@ -191,6 +192,7 @@ describe('ChooseRobotToRunProtocolSlideout', () => {
{ ...mockConnectableRobot, name: 'otherRobot', ip: 'otherIp' },
mockConnectableRobot,
])
provideNullCurrentRunIdFor('otherIp')
render({
storedProtocolData: storedProtocolDataFixture,
onCloseClick: vi.fn(),
Expand Down Expand Up @@ -372,6 +374,7 @@ describe('ChooseRobotToRunProtocolSlideout', () => {
mockConnectableRobot,
{ ...mockConnectableRobot, name: 'otherRobot', ip: 'otherIp' },
])
provideNullCurrentRunIdFor('otherIp')
render({
storedProtocolData: storedProtocolDataFixture,
onCloseClick: vi.fn(),
Expand All @@ -387,7 +390,7 @@ describe('ChooseRobotToRunProtocolSlideout', () => {
fireEvent.click(proceedButton)
fireEvent.click(screen.getByRole('button', { name: 'Confirm values' }))
expect(vi.mocked(useCreateRunFromProtocol)).nthCalledWith(
2,
3,
expect.any(Object),
{ hostname: '127.0.0.1' },
[
Expand Down Expand Up @@ -450,3 +453,21 @@ describe('ChooseRobotToRunProtocolSlideout', () => {
)
})
})

const provideNullCurrentRunIdFor = (hostname: string): void => {
let once = true
when(vi.mocked(useCurrentRunId))
.calledWith(expect.any(Object), {
hostname,
requestor: undefined,
})
.thenDo(options => {
void (options?.onSuccess != null && once
? options.onSuccess(({
links: { current: null },
} as unknown) as UseAllRunsQueryOptions)
: {})
once = false
return null
})
}

0 comments on commit f6b6f4c

Please sign in to comment.