Skip to content

Commit 8f953a4

Browse files
committed
refactor(app): split useRunningStepCounts
1 parent a0e0330 commit 8f953a4

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useMemo } from 'react'
2+
13
import { useInstrumentsQuery } from '@opentrons/react-api-client'
24

35
import { useRouteUpdateActions } from './useRouteUpdateActions'
@@ -13,12 +15,12 @@ import {
1315
} from '/app/resources/runs'
1416
import { useRecoveryOptionCopy } from './useRecoveryOptionCopy'
1517
import { useRecoveryActionMutation } from './useRecoveryActionMutation'
16-
import { useRunningStepCounts } from '/app/resources/protocols/hooks'
1718
import { useRecoveryToasts } from './useRecoveryToasts'
1819
import { useRecoveryAnalytics } from '/app/redux-resources/analytics'
1920
import { useShowDoorInfo } from './useShowDoorInfo'
2021
import { useCleanupRecoveryState } from './useCleanupRecoveryState'
2122
import { useFailedPipetteUtils } from './useFailedPipetteUtils'
23+
import { getRunningStepCountsFrom } from '/app/resources/protocols'
2224

2325
import type {
2426
LabwareDefinition2,
@@ -102,7 +104,14 @@ export function useERUtils({
102104
pageLength: 999,
103105
})
104106

105-
const stepCounts = useRunningStepCounts(runId, runCommands)
107+
const stepCounts = useMemo(
108+
() =>
109+
getRunningStepCountsFrom(
110+
protocolAnalysis?.commands ?? [],
111+
failedCommand?.byRunRecord ?? null
112+
),
113+
[protocolAnalysis != null, failedCommand]
114+
)
106115

107116
const analytics = useRecoveryAnalytics()
108117

app/src/resources/protocols/hooks/useRunningStepCounts.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useMostRecentCompletedAnalysis } from '/app/resources/runs'
33
import { useLastRunProtocolCommand } from './useLastRunProtocolCommand'
44

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

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

38-
const lastRunAnalysisCommandIndex = analysisCommands.findIndex(
39-
c => c.key === lastRunCommandNoFixit?.key
40-
)
41-
42-
const currentStepNumberByAnalysis =
43-
lastRunAnalysisCommandIndex === -1 ? null : lastRunAnalysisCommandIndex + 1
44-
45-
const hasRunDiverged =
46-
lastRunCommandNoFixit?.key == null || currentStepNumberByAnalysis == null
47-
48-
const totalStepCount = !hasRunDiverged ? analysisCommands.length : null
49-
50-
return {
51-
currentStepNumber: currentStepNumberByAnalysis,
52-
totalStepCount,
53-
hasRunDiverged,
54-
}
39+
return getRunningStepCountsFrom(analysisCommands, lastRunCommandNoFixit)
5540
}

app/src/resources/protocols/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import type { RunTimeCommand } from '@opentrons/shared-data'
2+
import type { RunCommandSummary } from '@opentrons/api-client'
3+
import type { StepCounts } from '/app/resources/protocols/hooks'
24

35
export function isGripperInCommands(commands: RunTimeCommand[]): boolean {
46
return (
@@ -8,3 +10,27 @@ export function isGripperInCommands(commands: RunTimeCommand[]): boolean {
810
) ?? false
911
)
1012
}
13+
14+
// See useRunningStepCounts.
15+
export function getRunningStepCountsFrom(
16+
analysisCommands: RunTimeCommand[],
17+
lastRunProtocolCommand: RunCommandSummary | null
18+
): StepCounts {
19+
const lastRunAnalysisCommandIndex = analysisCommands.findIndex(
20+
c => c.key === lastRunProtocolCommand?.key
21+
)
22+
23+
const currentStepNumberByAnalysis =
24+
lastRunAnalysisCommandIndex === -1 ? null : lastRunAnalysisCommandIndex + 1
25+
26+
const hasRunDiverged =
27+
lastRunProtocolCommand?.key == null || currentStepNumberByAnalysis == null
28+
29+
const totalStepCount = !hasRunDiverged ? analysisCommands.length : null
30+
31+
return {
32+
currentStepNumber: currentStepNumberByAnalysis,
33+
totalStepCount,
34+
hasRunDiverged,
35+
}
36+
}

0 commit comments

Comments
 (0)