import { useSelector } from 'react-redux' ; import reduce from 'lodash/reduce' ; import { getAttachedModules } from '../../../../redux/modules' ; import { getConnectedRobot } from '../../../../redux/discovery/selectors' ; import type { State } from '../../../../redux/types' ; import type { AttachedModule } from '../../../../redux/modules/types' ; import { useModuleRenderInfoById } from '../../hooks' ; interface ModuleMatchResults { missingModuleIds : string [ ] ; remainingAttachedModules : AttachedModule [ ] ; } export function useMissingModuleIds ( ) { const robot = useSelector ( ( state ) => getConnectedRobot ( state ) ) ; const moduleRenderInfoById = useModuleRenderInfoById ( ) ; const attachedModules = useSelector ( ( state ) => getAttachedModules ( state , robot === null ? null : robot . name ) ) ; if ( robot === null ) { return [ ] ; } const { missingModuleIds } = reduce < typeof moduleRenderInfoById , ModuleMatchResults > ( moduleRenderInfoById , ( acc , { moduleDef } , id ) => { const { model } = moduleDef ; const moduleTypeMatchIndex = acc . remainingAttachedModules . findIndex ( attachedModule => model === attachedModule . model ) ; return moduleTypeMatchIndex !== - 1 ? { ... acc , remainingAttachedModules : acc . remainingAttachedModules . filter ( ( _remainingAttachedModule , index ) => index !== moduleTypeMatchIndex ) , } : { ... acc , missingModuleIds : [ ... acc . missingModuleIds , id ] , } ; } , { missingModuleIds : [ ] , remainingAttachedModules : attachedModules } ) ; return missingModuleIds ; }