import { difference , flatten , keyBy } from 'lodash' ; import React from 'react' ; import type { SortEnd } from 'react-sortable-hoc' ; import { SortableContainer , SortableElement } from 'react-sortable-hoc' ; import type { IInstanceTypeFamily } from '@spinnaker/core' ; import { InstanceTypeRow } from './InstanceTypeRow' ; import type { IAmazonPreferredInstanceType } from '../../../../../instance/awsInstanceType.service' ; import type { IAmazonInstanceTypeOverride } from '../../../serverGroupConfiguration.service' ; export function InstanceTypeTableBody ( props ) { return ( < TableRows isCustom = { props . isCustom } selectedInstanceTypesMap = { props . selectedInstanceTypesMap } removeInstanceType = { props . removeInstanceType } addOrUpdateInstanceType = { props . addOrUpdateInstanceType } instanceTypeDetails = { props . isCustom ? null : new Map ( Object . entries ( keyBy ( flatten ( props . profileFamiliesDetails . map ( ( f ) => f . instanceTypes ) ) , 'name' ) ) ) } onSortEnd = { ( sortEnd ) => props . handleSortEnd ( sortEnd ) } distance = { 1 } / > ) ; } const TableRows = SortableContainer ( ( props ) => { const { isCustom , selectedInstanceTypesMap } = props ; let selectedRows , unselectedRows ; if ( isCustom ) { selectedRows = selectedInstanceTypesMap . size > 0 && Array . from ( selectedInstanceTypesMap . values ( ) ) . sort ( ( i1 , i2 ) => i1 . priority - i2 . priority ) . map ( ( selectedType , index ) => ( < SortableRow key = { `${ selectedType . instanceType }-${ index }` } index = { index } isCustom = { true } selectedType = { selectedType } removeInstanceType = { props . removeInstanceType } addOrUpdateInstanceType = { props . addOrUpdateInstanceType } / > ) ) ; } else { const { instanceTypeDetails } = props ; const instanceTypesInProfile = Array . from ( instanceTypeDetails ?. keys ( ) ) ; const unselectedInstanceTypes = difference ( instanceTypesInProfile , Array . from ( selectedInstanceTypesMap . keys ( ) ) ) ; const selectedRowsOrdered = Array . from ( selectedInstanceTypesMap . values ( ) ) . filter ( ( selectedType ) => instanceTypesInProfile . includes ( selectedType . instanceType ) ) . sort ( ( i1 , i2 ) => i1 . priority - i2 . priority ) ; selectedRows = selectedRowsOrdered && selectedRowsOrdered . length > 0 && selectedRowsOrdered . map ( ( selectedType , index ) => ( < SortableRow key = { `${ selectedType . instanceType }-${ index }` } index = { index } isCustom = { false } selectedType = { selectedType } instanceTypeDetails = { instanceTypeDetails } removeInstanceType = { props . removeInstanceType } addOrUpdateInstanceType = { props . addOrUpdateInstanceType } / > ) ) ; unselectedRows = unselectedInstanceTypes && unselectedInstanceTypes . length > 0 && unselectedInstanceTypes . map ( ( instanceType ) => ( < InstanceTypeRow key = { instanceType } isCustom = { false } instanceTypeDetails = { instanceTypeDetails . get ( instanceType ) } addOrUpdateInstanceType = { props . addOrUpdateInstanceType } / > ) ) ; } return ( < tbody > { selectedRows } { ! isCustom ? unselectedRows : null } < /tbody>); } ) ; const SortableRow = SortableElement ( ( props ) => ( < InstanceTypeRow key = { props . selectedType . instanceType } isCustom = { props . isCustom } selectedType = { props . selectedType } instanceTypeDetails = { ! props . isCustom ? props . instanceTypeDetails . get ( props . selectedType . instanceType ) : null } removeInstanceType = { props . removeInstanceType } addOrUpdateInstanceType = { props . addOrUpdateInstanceType } / > ) ) ;