import { basename } from 'path' ; import { createSelector } from 'reselect' ; import sortBy from 'lodash/sortBy' ; import { getConfig } from '../config' ; import { getIsTiprack } from '@opentrons/shared-data' ; import type { LabwareDefinition2 } from '@opentrons/shared-data' ; import type { State } from '../types' ; import type { CheckedLabwareFile , ValidLabwareFile , FailedLabwareFile , } from './types' ; export const INVALID_LABWARE_FILE = 'INVALID_LABWARE_FILE' ; export const DUPLICATE_LABWARE_FILE = 'DUPLICATE_LABWARE_FILE' ; export const OPENTRONS_LABWARE_FILE = 'OPENTRONS_LABWARE_FILE' ; export const VALID_LABWARE_FILE = 'VALID_LABWARE_FILE' ; export const getCustomLabwareDirectory = createSelector ( getConfig , config => config ?. labware . directory ?? '' ) ; export const getCustomLabware = createSelector ( state => state . labware . filenames , state => state . labware . filesByName , ( filenames , filesByName ) => sortBy ( filenames . map ( name => filesByName [ name ] ) , [ 'definition.metadata.displayCategory' , 'definition.metadata.displayName' ] ) ) ; export const getValidCustomLabware = createSelector ( getCustomLabware , labware => labware . filter ( ( f ) : f is ValidLabwareFile => f . type === VALID_LABWARE_FILE ) ) ; export const getValidCustomLabwareFiles = createSelector ( getValidCustomLabware , labware => { const labwareFiles = labware . map ( lw => { const jsonDefinition = JSON . stringify ( lw . definition ) ; return new File ( [ jsonDefinition ] , basename ( lw . filename ) ) ; } ) ; return labwareFiles ; } ) ; export const getAddLabwareFailure = createSelector ( state => state . labware . addFailureFile , state => state . labware . addFailureMessage , ( file , errorMessage ) => ( { file , errorMessage } ) ) ; export const getListLabwareErrorMessage = ( state ) : null | string => state . labware . listFailureMessage ; export const getCustomLabwareDefinitions = createSelector ( getValidCustomLabware , labware => labware . map ( lw => lw . definition ) ) ; export const getCustomTipRackDefinitions = createSelector ( getCustomLabwareDefinitions , labware => labware . filter ( lw => getIsTiprack ( lw ) ) ) ;