|
| 1 | +import type { LabwareOffset } from '@opentrons/api-client' |
| 2 | +import type { LPCLabwareInfo } from '/app/redux/protocol-runs' |
| 3 | +import isEqual from 'lodash/isEqual' |
| 4 | + |
| 5 | +// Given run record offsets, update the LPC store with those supplied vectors details. |
| 6 | +// All run record supplied offsets will either match location-specific offsets or default offsets |
| 7 | +// supplied by the database. |
| 8 | +// |
| 9 | +// The database provided offsets are injected at LPC store initialization. |
| 10 | +// |
| 11 | +// Note that hardcoded offsets are injected into the store on initialization too, |
| 12 | +// and these will never need updating, as they are never supplied by the run record. |
| 13 | +export function updateLPCLabwareInfoFrom( |
| 14 | + clonedRunOffsets: LabwareOffset[], |
| 15 | + currentLwInfoLw: LPCLabwareInfo['labware'] |
| 16 | +): LPCLabwareInfo['labware'] { |
| 17 | + return clonedRunOffsets.reduce( |
| 18 | + (acc, clonedRunOffset) => { |
| 19 | + const definitionUri = clonedRunOffset.definitionUri |
| 20 | + const lwInfo = acc[definitionUri] |
| 21 | + |
| 22 | + if (clonedRunOffset.locationSequence == null || lwInfo == null) { |
| 23 | + console.error( |
| 24 | + 'Expected cloned run offset to have location sequence, but did not.' |
| 25 | + ) |
| 26 | + return acc |
| 27 | + } |
| 28 | + |
| 29 | + const { locationSpecificOffsetDetails, defaultOffsetDetails } = lwInfo |
| 30 | + const { vector: defaultVector } = defaultOffsetDetails.existingOffset ?? { |
| 31 | + vector: null, |
| 32 | + } |
| 33 | + |
| 34 | + // Whether the cloned run offset location matches a location-specific offset location |
| 35 | + // in the LPC store keyed at the same labware URI. |
| 36 | + const relevantLSIdx = locationSpecificOffsetDetails.findIndex( |
| 37 | + lsStoreOffset => |
| 38 | + isEqual( |
| 39 | + lsStoreOffset.locationDetails.lwOffsetLocSeq, |
| 40 | + clonedRunOffset.locationSequence |
| 41 | + ) |
| 42 | + ) |
| 43 | + |
| 44 | + // Matching location-specific offset case. |
| 45 | + if (relevantLSIdx > -1) { |
| 46 | + const newLocationSpecificOffsetDetails = locationSpecificOffsetDetails.map( |
| 47 | + (detail, idx) => { |
| 48 | + if (idx === relevantLSIdx) { |
| 49 | + return { |
| 50 | + ...detail, |
| 51 | + existingOffset: { |
| 52 | + ...clonedRunOffset, |
| 53 | + }, |
| 54 | + } |
| 55 | + } |
| 56 | + return detail |
| 57 | + } |
| 58 | + ) |
| 59 | + |
| 60 | + return { |
| 61 | + ...acc, |
| 62 | + [definitionUri]: { |
| 63 | + ...lwInfo, |
| 64 | + locationSpecificOffsetDetails: newLocationSpecificOffsetDetails, |
| 65 | + }, |
| 66 | + } |
| 67 | + } |
| 68 | + // Matching default offset case. |
| 69 | + // Whether the cloned run offset matches the default offset keyed |
| 70 | + // at the same labware URI. |
| 71 | + else if ( |
| 72 | + defaultVector != null && |
| 73 | + isEqual(defaultVector, clonedRunOffset.vector) |
| 74 | + ) { |
| 75 | + // Return updated state with new default offset |
| 76 | + return { |
| 77 | + ...acc, |
| 78 | + [definitionUri]: { |
| 79 | + ...lwInfo, |
| 80 | + defaultOffsetDetails: { |
| 81 | + ...defaultOffsetDetails, |
| 82 | + existingOffset: { ...clonedRunOffset }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return acc |
| 89 | + }, |
| 90 | + { ...currentLwInfoLw } |
| 91 | + ) |
| 92 | +} |
0 commit comments