Skip to content

Commit

Permalink
feat(app): add useUpdateRecoveryPolicyWithStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
mjhuff committed Nov 22, 2024
1 parent 4c0f813 commit 795eefc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/resources/runs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export * from './useModuleCalibrationStatus'
export * from './useProtocolAnalysisErrors'
export * from './useLastRunCommand'
export * from './useRunStatuses'
export * from './useUpdateRecoveryPolicyWithStrategy'
60 changes: 60 additions & 0 deletions app/src/resources/runs/useUpdateRecoveryPolicyWithStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {
useHost,
useUpdateErrorRecoveryPolicy,
} from '@opentrons/react-api-client'
import { getErrorRecoveryPolicy } from '@opentrons/api-client'

import type {
HostConfig,
ErrorRecoveryPolicyResponse,
RecoveryPolicyRulesParams,
} from '@opentrons/api-client'

/**
* append - Add a new policy rule to the end of the existing recovery policy.
*/
export type UpdatePolicyStrategy = 'append'

export interface UpdateErrorRecoveryPolicyWithStrategy {
runId: string
newPolicy: RecoveryPolicyRulesParams[number]
strategy: UpdatePolicyStrategy
}

export function useUpdateRecoveryPolicyWithStrategy(
runId: string
): (
newPolicy: UpdateErrorRecoveryPolicyWithStrategy['newPolicy'],
strategy: UpdateErrorRecoveryPolicyWithStrategy['strategy']
) => Promise<ErrorRecoveryPolicyResponse> {
const host = useHost()

const {
mutateAsync: updateErrorRecoveryPolicy,
} = useUpdateErrorRecoveryPolicy(runId)

return (
newPolicy: UpdateErrorRecoveryPolicyWithStrategy['newPolicy'],
strategy: UpdateErrorRecoveryPolicyWithStrategy['strategy']
) =>
getErrorRecoveryPolicy(host as HostConfig, runId).then(res => {
const existingPolicyRules = res.data.data.policyRules.map(rule => ({
commandType: rule.matchCriteria.command.commandType,
errorType: rule.matchCriteria.command.error.errorType,
ifMatch: rule.ifMatch,
}))

const buildUpdatedPolicy = (): RecoveryPolicyRulesParams => {
switch (strategy) {
case 'append':
return [...existingPolicyRules, newPolicy]
default: {
console.error('Handled policy strategy, using append.')
return [...existingPolicyRules, newPolicy]
}
}
}

return updateErrorRecoveryPolicy(buildUpdatedPolicy())
})
}

0 comments on commit 795eefc

Please sign in to comment.