Skip to content

Commit 4a2516e

Browse files
refactor(app): Make getStacksOnModules() more type-safe (#18905)
1 parent c8f9e65 commit 4a2516e

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

app/src/organisms/Desktop/Devices/ProtocolRun/SetupLabware/SetupLabwareMap.tsx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
getLabwareDefinitionsByURIForProtocol,
2424
getLabwareInfoByLiquidId,
2525
getLabwareOnDeck,
26-
getModuleFromStack,
2726
getStackedItemsOnStartingDeck,
2827
getStacksOnModules,
2928
getTopLabwareFromStack,
@@ -36,7 +35,6 @@ import { SlotDetailModal } from './SlotDetailModal'
3635
import type { LabwareOnDeck } from '@opentrons/components'
3736
import type {
3837
CompletedProtocolAnalysis,
39-
ModuleModel,
4038
ProtocolAnalysisOutput,
4139
} from '@opentrons/shared-data'
4240
import type { StackItem } from '/app/transformations/commands'
@@ -80,8 +78,7 @@ export function SetupLabwareMap({
8078
const labwareByLiquidId = getLabwareInfoByLiquidId(protocolAnalysis.commands)
8179

8280
const modulesOnDeck = Object.entries(getStacksOnModules(startingDeck)).map(
83-
([slotName, stackedItems]) => {
84-
const module = getModuleFromStack(stackedItems)
81+
([slotName, { allItemsInStack: stackedItems, moduleInStack: module }]) => {
8582
const topLabwareInfo = getTopLabwareFromStack(stackedItems)
8683
const topLabwareDefinition =
8784
topLabwareInfo != null
@@ -97,14 +94,13 @@ export function SetupLabwareMap({
9794
labwareByLiquidId
9895
)
9996
: undefined
100-
const moduleType =
101-
module?.moduleModel == null ? null : getModuleType(module.moduleModel)
97+
const moduleType = getModuleType(module.moduleModel)
10298

10399
return {
104-
moduleModel: module?.moduleModel ?? ('' as ModuleModel),
105-
moduleLocation: { slotName: module?.moduleSlotName ?? slotName },
100+
moduleModel: module.moduleModel,
101+
moduleLocation: { slotName: module.moduleSlotName },
106102
innerProps:
107-
module?.moduleModel === THERMOCYCLER_MODULE_V1
103+
module.moduleModel === THERMOCYCLER_MODULE_V1
108104
? { lidMotorState: 'open' }
109105
: {},
110106

@@ -118,7 +114,7 @@ export function SetupLabwareMap({
118114
onClick={() => {
119115
if (topLabwareInfo != null) {
120116
setSelectedStack({
121-
slotName: slotName,
117+
slotName,
122118
stack: stackedItems,
123119
})
124120
}
@@ -131,7 +127,7 @@ export function SetupLabwareMap({
131127
onMouseLeave={() => {
132128
setHoverLabwareId(null)
133129
}}
134-
cursor={'pointer'}
130+
cursor="pointer"
135131
>
136132
{topLabwareDefinition != null && topLabwareInfo != null ? (
137133
<LabwareInfoOverlay
@@ -183,12 +179,12 @@ export function SetupLabwareMap({
183179
definition: topLabwareDefinition,
184180
highlight: hoverLabwareId === topLabwareInfo.labwareId,
185181
stacked: isLabwareInStack,
186-
wellFill: wellFill,
182+
wellFill,
187183
labwareChildren: (
188184
<g
189-
cursor={'pointer'}
185+
cursor="pointer"
190186
onClick={() => {
191-
setSelectedStack({ slotName: slotName, stack: stackedItems })
187+
setSelectedStack({ slotName, stack: stackedItems })
192188
}}
193189
onMouseEnter={() => {
194190
setHoverLabwareId(() => topLabwareInfo.labwareId)

app/src/organisms/ODD/ProtocolSetup/ProtocolSetupLabware/LabwareMapView.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ import { getWellFillFromLabwareId } from '/app/organisms/ProtocolDeck'
1212
import {
1313
getLabwareDefinitionsByURIForProtocol,
1414
getLabwareOnDeck,
15-
getModuleFromStack,
1615
getStacksOnModules,
1716
getTopLabwareFromStack,
1817
} from '/app/transformations/commands'
1918

2019
import type { Dispatch, SetStateAction } from 'react'
2120
import type { LabwareOnDeck } from '@opentrons/components'
22-
import type {
23-
CompletedProtocolAnalysis,
24-
ModuleModel,
25-
} from '@opentrons/shared-data'
21+
import type { CompletedProtocolAnalysis } from '@opentrons/shared-data'
2622
import type {
2723
LabwareByLiquidId,
2824
StackedItemsOnDeck,
@@ -50,8 +46,7 @@ export function LabwareMapView(props: LabwareMapViewProps): JSX.Element {
5046
[mostRecentAnalysis]
5147
)
5248
const modulesOnDeck = Object.entries(getStacksOnModules(startingDeck)).map(
53-
([slotName, stackedItems]) => {
54-
const module = getModuleFromStack(stackedItems)
49+
([slotName, { allItemsInStack: stackedItems, moduleInStack: module }]) => {
5550
const topLabwareInfo = getTopLabwareFromStack(stackedItems)
5651
const topLabwareDefinition =
5752
topLabwareInfo != null
@@ -67,10 +62,10 @@ export function LabwareMapView(props: LabwareMapViewProps): JSX.Element {
6762
)
6863
: undefined
6964
return {
70-
moduleModel: module?.moduleModel ?? ('' as ModuleModel),
71-
moduleLocation: { slotName: module?.moduleSlotName ?? slotName },
65+
moduleModel: module.moduleModel,
66+
moduleLocation: { slotName: module.moduleSlotName },
7267
innerProps:
73-
module?.moduleModel === THERMOCYCLER_MODULE_V1
68+
module.moduleModel === THERMOCYCLER_MODULE_V1
7469
? { lidMotorState: 'open' }
7570
: {},
7671
nestedLabwareDef: topLabwareDefinition,

app/src/organisms/ProtocolDeck/index.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
getLabwareDefinitionsByURIForProtocol,
1111
getLabwareInfoByLiquidId,
1212
getLabwareOnDeck,
13-
getModuleFromStack,
1413
getStackedItemsOnStartingDeck,
1514
getStacksOnModules,
1615
getTopLabwareFromStack,
@@ -25,7 +24,6 @@ import type { ComponentProps } from 'react'
2524
import type { LabwareOnDeck } from '@opentrons/components'
2625
import type {
2726
CompletedProtocolAnalysis,
28-
ModuleModel,
2927
ProtocolAnalysisOutput,
3028
} from '@opentrons/shared-data'
3129

@@ -60,17 +58,16 @@ export function ProtocolDeck(props: ProtocolDeckProps): JSX.Element | null {
6058
const labwareByLiquidId = getLabwareInfoByLiquidId(protocolAnalysis.commands)
6159

6260
const modulesOnDeck = Object.entries(getStacksOnModules(startingDeck)).map(
63-
([slotName, stackedItems]) => {
64-
const module = getModuleFromStack(stackedItems)
61+
([slotName, { allItemsInStack: stackedItems, moduleInStack: module }]) => {
6562
const topLabwareInfo = getTopLabwareFromStack(stackedItems)
6663
const topLabwareDefinition =
6764
topLabwareInfo != null
6865
? labwareDefinitionsByURI[topLabwareInfo.definitionUri]
6966
: null
7067

7168
return {
72-
moduleModel: module?.moduleModel ?? ('' as ModuleModel),
73-
moduleLocation: { slotName: module?.moduleSlotName ?? slotName },
69+
moduleModel: module.moduleModel,
70+
moduleLocation: { slotName: module.moduleSlotName },
7471
nestedLabwareDef: topLabwareDefinition,
7572
nestedLabwareWellFill:
7673
topLabwareInfo != null

app/src/transformations/commands/transformations/getStackedItemsOnStartingDeck.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ export function getLabwareOnDeck(
474474
return Object.fromEntries(labwareOnDeckEntries)
475475
}
476476

477-
// filter function to get stacks that include modules
477+
// filter function to get stacks that include labware
478478
export function getStacksWithLabware(
479479
itemsOnDeck: StackedItemsOnDeck
480480
): { [slotName: string]: StackItem[] } {
@@ -491,15 +491,31 @@ export function getStacksWithLabware(
491491
// filter function to get stacks that include modules
492492
export function getStacksOnModules(
493493
itemsOnDeck: StackedItemsOnDeck
494-
): { [slotName: string]: StackItem[] } {
495-
const stacksOnModuleEntries = Object.entries(
496-
itemsOnDeck
497-
).filter(([key, value]) =>
498-
value.some(
494+
): {
495+
[slotName: string]: {
496+
// This could be typed more cleverly as:
497+
// [ModuleInStack, ...StackItem[]]
498+
// if we're sure that the module is always the first element in the array,
499+
// but I'm not sure if that's actually the case.
500+
allItemsInStack: StackItem[]
501+
moduleInStack: ModuleInStack
502+
}
503+
} {
504+
return Object.entries(itemsOnDeck).reduce((acc, entry) => {
505+
const [slotName, stack] = entry
506+
const moduleInStack = stack.find(
499507
(stackItem): stackItem is ModuleInStack => 'moduleId' in stackItem
500508
)
501-
)
502-
return Object.fromEntries(stacksOnModuleEntries)
509+
return moduleInStack != null
510+
? {
511+
...acc,
512+
[slotName]: {
513+
allItemsInStack: stack,
514+
moduleInStack,
515+
},
516+
}
517+
: acc
518+
}, {})
503519
}
504520

505521
export function getTopLabwareFromStack(

0 commit comments

Comments
 (0)