Skip to content

Commit

Permalink
refactor(app): split useRunningStepCounts
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff committed Nov 18, 2024
1 parent a0e0330 commit 8f953a4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
13 changes: 11 additions & 2 deletions app/src/organisms/ErrorRecoveryFlows/hooks/useERUtils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useMemo } from 'react'

import { useInstrumentsQuery } from '@opentrons/react-api-client'

import { useRouteUpdateActions } from './useRouteUpdateActions'
Expand All @@ -13,12 +15,12 @@ import {
} from '/app/resources/runs'
import { useRecoveryOptionCopy } from './useRecoveryOptionCopy'
import { useRecoveryActionMutation } from './useRecoveryActionMutation'
import { useRunningStepCounts } from '/app/resources/protocols/hooks'
import { useRecoveryToasts } from './useRecoveryToasts'
import { useRecoveryAnalytics } from '/app/redux-resources/analytics'
import { useShowDoorInfo } from './useShowDoorInfo'
import { useCleanupRecoveryState } from './useCleanupRecoveryState'
import { useFailedPipetteUtils } from './useFailedPipetteUtils'
import { getRunningStepCountsFrom } from '/app/resources/protocols'

import type {
LabwareDefinition2,
Expand Down Expand Up @@ -102,7 +104,14 @@ export function useERUtils({
pageLength: 999,
})

const stepCounts = useRunningStepCounts(runId, runCommands)
const stepCounts = useMemo(
() =>
getRunningStepCountsFrom(
protocolAnalysis?.commands ?? [],
failedCommand?.byRunRecord ?? null
),
[protocolAnalysis != null, failedCommand]
)

const analytics = useRecoveryAnalytics()

Expand Down
19 changes: 2 additions & 17 deletions app/src/resources/protocols/hooks/useRunningStepCounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useMostRecentCompletedAnalysis } from '/app/resources/runs'
import { useLastRunProtocolCommand } from './useLastRunProtocolCommand'

import type { CommandsData } from '@opentrons/api-client'
import { getRunningStepCountsFrom } from '/app/resources/protocols'

export interface StepCounts {
/* Excludes "fixit" commands. Returns null if the step is not found. */
Expand Down Expand Up @@ -35,21 +36,5 @@ export function useRunningStepCounts(
commandsData ?? null
)

const lastRunAnalysisCommandIndex = analysisCommands.findIndex(
c => c.key === lastRunCommandNoFixit?.key
)

const currentStepNumberByAnalysis =
lastRunAnalysisCommandIndex === -1 ? null : lastRunAnalysisCommandIndex + 1

const hasRunDiverged =
lastRunCommandNoFixit?.key == null || currentStepNumberByAnalysis == null

const totalStepCount = !hasRunDiverged ? analysisCommands.length : null

return {
currentStepNumber: currentStepNumberByAnalysis,
totalStepCount,
hasRunDiverged,
}
return getRunningStepCountsFrom(analysisCommands, lastRunCommandNoFixit)
}
26 changes: 26 additions & 0 deletions app/src/resources/protocols/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import type { RunTimeCommand } from '@opentrons/shared-data'
import type { RunCommandSummary } from '@opentrons/api-client'
import type { StepCounts } from '/app/resources/protocols/hooks'

export function isGripperInCommands(commands: RunTimeCommand[]): boolean {
return (
Expand All @@ -8,3 +10,27 @@ export function isGripperInCommands(commands: RunTimeCommand[]): boolean {
) ?? false
)
}

// See useRunningStepCounts.
export function getRunningStepCountsFrom(
analysisCommands: RunTimeCommand[],
lastRunProtocolCommand: RunCommandSummary | null
): StepCounts {
const lastRunAnalysisCommandIndex = analysisCommands.findIndex(
c => c.key === lastRunProtocolCommand?.key
)

const currentStepNumberByAnalysis =
lastRunAnalysisCommandIndex === -1 ? null : lastRunAnalysisCommandIndex + 1

const hasRunDiverged =
lastRunProtocolCommand?.key == null || currentStepNumberByAnalysis == null

const totalStepCount = !hasRunDiverged ? analysisCommands.length : null

return {
currentStepNumber: currentStepNumberByAnalysis,
totalStepCount,
hasRunDiverged,
}
}

0 comments on commit 8f953a4

Please sign in to comment.