import { useContext , createContext } from 'react' ; import standardDeckDef from '@opentrons/shared-data/deck/definitions/2/ot2_standard.json' ; import { checkModuleCompatibility } from '@opentrons/shared-data' ; import { useProtocolDetails } from '../RunDetails/hooks' ; import { useSelector } from 'react-redux' ; import { State } from '../../redux/types' ; import { getProtocolModulesInfo } from './utils/getProtocolModulesInfo' ; import { getLabwareRenderInfo } from './utils/getLabwareRenderInfo' ; import { getAttachedModules } from '../../redux/modules' ; import { getConnectedRobotName } from '../../redux/robot/selectors' ; import { AttachedModule } from '../../redux/modules/types' ; import { useCurrentProtocolRun } from '../ProtocolUpload/hooks' ; import type { ProtocolModuleInfo } from './utils/getProtocolModulesInfo' ; import type { LabwareRenderInfoById } from './utils/getLabwareRenderInfo' ; import type { Command } from '@opentrons/shared-data/protocol/types/schemaV6' ; import type { LoadPipetteCommand } from '@opentrons/shared-data/protocol/types/schemaV6/command/setup' ; interface ProtocolMetadata { author ? : string ; lastUpdated ? : number | null ; description ? : string | null ; creationMethod ? : 'json' | 'python' ; } export function useProtocolMetadata ( ) { const currentProtocolRun = useCurrentProtocolRun ( ) ; const protocolMetadata = currentProtocolRun . protocolRecord ?. data ?. metadata ; const creationMethod = currentProtocolRun . protocolRecord ?. data ?. protocolType ; const author = protocolMetadata ?. author ; const description = protocolMetadata ?. description ; const lastUpdated = protocolMetadata ?. lastModified ; return { author , lastUpdated , description , creationMethod } ; } interface ModuleRenderInfo extends ProtocolModuleInfo { attachedModuleMatch : AttachedModule | null ; } export function useModuleRenderInfoById ( ) { const { protocolData } = useProtocolDetails ( ) ; const robotName = useSelector ( ( state ) => getConnectedRobotName ( state ) ) ; const attachedModules = useSelector ( ( state ) => getAttachedModules ( state , robotName ) ) ; if ( protocolData == null ) return { } ; const protocolModulesInfo = getProtocolModulesInfo ( protocolData , standardDeckDef as any ) ; const protocolModulesInfoInLoadOrder = protocolModulesInfo . sort ( ( modA , modB ) => modA . protocolLoadOrder - modB . protocolLoadOrder ) ; let matchedAmod = [ ] ; const allModuleRenderInfo = protocolModulesInfoInLoadOrder . map ( protocolMod => { const compatibleAttachedModule = attachedModules . find ( attachedMod => checkModuleCompatibility ( attachedMod . model , protocolMod . moduleDef . model ) && ! matchedAmod . find ( m => m === attachedMod ) ) ?? null ; if ( compatibleAttachedModule !== null ) { matchedAmod = [ ... matchedAmod , compatibleAttachedModule ] ; return { ... protocolMod , attachedModuleMatch : compatibleAttachedModule , } ; } return { ... protocolMod , attachedModuleMatch : null , } ; } ) ; return allModuleRenderInfo . reduce ( ( acc , moduleInfo ) => ( { ... acc , [ moduleInfo . moduleId ] : moduleInfo , } ) , { } ) ; } export function useLabwareRenderInfoById ( ) { const { protocolData } = useProtocolDetails ( ) ; return protocolData != null ? getLabwareRenderInfo ( protocolData , standardDeckDef as any ) : { } ; } export function usePipetteMount ( pipetteId ) { const { protocolData } = useProtocolDetails ( ) ; return ( protocolData ?. commands . find ( ( command ) : command is LoadPipetteCommand => command . commandType === 'loadPipette' && command . params . pipetteId === pipetteId ) ?. params . mount ?? null ) ; } export const LPCSuccessToastContext = createContext < { setShowLPCSuccessToast : ( ) => void ; } > ( { setShowLPCSuccessToast : ( ) => null } ) ; export function useLPCSuccessToast ( ) { const { setShowLPCSuccessToast } = useContext ( LPCSuccessToastContext ) ; return { setShowLPCSuccessToast } ; }