-
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): Split RunProgressMeter (#15893)
Closes RQA-2842 Migrates the logic for building RunProgressMeter's step copy to its own utility, cleaning up the order of operations slightly. Refactors the final step text from nothing to "N/A" when the run is canceled before starting.
- Loading branch information
Showing
5 changed files
with
177 additions
and
62 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
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,15 @@ | ||
import { | ||
RUN_STATUS_FAILED, | ||
RUN_STATUS_FINISHING, | ||
RUN_STATUS_STOPPED, | ||
RUN_STATUS_SUCCEEDED, | ||
} from '@opentrons/api-client' | ||
|
||
import type { RunStatus } from '@opentrons/api-client' | ||
|
||
export const TERMINAL_RUN_STATUSES: RunStatus[] = [ | ||
RUN_STATUS_STOPPED, | ||
RUN_STATUS_FAILED, | ||
RUN_STATUS_FINISHING, | ||
RUN_STATUS_SUCCEEDED, | ||
] |
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 * from './useRunProgressCopy' |
118 changes: 118 additions & 0 deletions
118
app/src/organisms/RunProgressMeter/hooks/useRunProgressCopy.tsx
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,118 @@ | ||
import { | ||
RUN_STATUS_BLOCKED_BY_OPEN_DOOR, | ||
RUN_STATUS_IDLE, | ||
} from '@opentrons/api-client' | ||
import * as React from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { getCommandTextData } from '../../../molecules/Command/utils/getCommandTextData' | ||
import { LegacyStyledText } from '@opentrons/components' | ||
import { CommandText } from '../../../molecules/Command' | ||
import { TERMINAL_RUN_STATUSES } from '../constants' | ||
|
||
import type { CommandDetail, RunStatus } from '@opentrons/api-client' | ||
import type { | ||
CompletedProtocolAnalysis, | ||
RobotType, | ||
RunTimeCommand, | ||
} from '@opentrons/shared-data' | ||
|
||
interface UseRunProgressResult { | ||
currentStepContents: React.ReactNode | ||
stepCountStr: string | null | ||
progressPercentage: number | ||
} | ||
|
||
interface UseRunProgressProps { | ||
runStatus: RunStatus | null | ||
currentStepNumber: number | null | ||
totalStepCount: number | null | ||
analysis: CompletedProtocolAnalysis | null | ||
hasRunDiverged: boolean | ||
runCommandDetails: CommandDetail | null | ||
robotType: RobotType | ||
analysisCommands: RunTimeCommand[] | ||
} | ||
|
||
// TODO(jh, 08-05-24): Testing is sufficiently covered by RunProgressMeter, but we should migrate relevant tests to this | ||
// hook after devising a better way to test i18n outside of a component. | ||
export function useRunProgressCopy({ | ||
runStatus, | ||
currentStepNumber, | ||
totalStepCount, | ||
hasRunDiverged, | ||
runCommandDetails, | ||
analysisCommands, | ||
robotType, | ||
analysis, | ||
}: UseRunProgressProps): UseRunProgressResult { | ||
const { t } = useTranslation('run_details') | ||
|
||
const runHasNotBeenStarted = | ||
(currentStepNumber === 0 && | ||
runStatus === RUN_STATUS_BLOCKED_BY_OPEN_DOOR) || | ||
runStatus === RUN_STATUS_IDLE | ||
|
||
const currentStepContents = ((): JSX.Element | null => { | ||
if (runHasNotBeenStarted) { | ||
return <LegacyStyledText as="h2">{t('not_started_yet')}</LegacyStyledText> | ||
} else if (analysis != null && !hasRunDiverged) { | ||
return ( | ||
<CommandText | ||
commandTextData={getCommandTextData(analysis)} | ||
command={analysisCommands[(currentStepNumber as number) - 1]} | ||
robotType={robotType} | ||
/> | ||
) | ||
} else if ( | ||
analysis != null && | ||
hasRunDiverged && | ||
runCommandDetails != null | ||
) { | ||
return ( | ||
<CommandText | ||
commandTextData={getCommandTextData(analysis)} | ||
command={runCommandDetails.data} | ||
robotType={robotType} | ||
/> | ||
) | ||
} else { | ||
return null | ||
} | ||
})() | ||
|
||
const progressPercentage = runHasNotBeenStarted | ||
? 0 | ||
: ((currentStepNumber as number) / analysisCommands.length) * 100 | ||
|
||
const stepCountStr = ((): string | null => { | ||
if (runStatus == null) { | ||
return null | ||
} else { | ||
const isTerminalStatus = TERMINAL_RUN_STATUSES.includes(runStatus) | ||
const stepType = isTerminalStatus ? t('final_step') : t('current_step') | ||
|
||
if (runStatus === RUN_STATUS_IDLE) { | ||
return `${stepType}:` | ||
} else if (isTerminalStatus && currentStepNumber == null) { | ||
return `${stepType}: N/A` | ||
} else { | ||
const getCountString = (): string => { | ||
const current = currentStepNumber ?? '?' | ||
const total = totalStepCount ?? '?' | ||
|
||
return `${current}/${total}` | ||
} | ||
|
||
const countString = getCountString() | ||
|
||
return `${stepType} ${countString}:` | ||
} | ||
} | ||
})() | ||
|
||
return { | ||
currentStepContents, | ||
stepCountStr, | ||
progressPercentage, | ||
} | ||
} |
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