import { kea , MakeLogicType } from 'kea' ; import { recursivelyFetchEngines } from '../../../../utils/recursively_fetch_engines' ; import { EngineDetails } from '../../../engine/types' ; interface MetaEnginesTableValues { expandedRows : { [ id ] : boolean ; } ; sourceEngines : { [ id ] : EngineDetails [ ] ; } ; expandedSourceEngines : { [ id ] : EngineDetails [ ] ; } ; } interface MetaEnginesTableActions { addSourceEngines ( sourceEngines ) : { sourceEngines : MetaEnginesTableValues [ 'sourceEngines' ] ; } ; displayRow ( itemId ) : { itemId : string ; } ; fetchOrDisplayRow ( itemId ) : { itemId : string ; } ; fetchSourceEngines ( engineName ) : { engineName : string ; } ; hideRow ( itemId ) : { itemId : string ; } ; } export const MetaEnginesTableLogic = kea < MakeLogicType < MetaEnginesTableValues , MetaEnginesTableActions > > ( { path : [ 'enterprise_search' , 'app_search' , 'meta_engines_table_logic' ] , actions : ( ) => ( { addSourceEngines : ( sourceEngines ) => ( { sourceEngines } ) , displayRow : ( itemId ) => ( { itemId } ) , hideRow : ( itemId ) => ( { itemId } ) , fetchOrDisplayRow : ( itemId ) => ( { itemId } ) , fetchSourceEngines : ( engineName ) => ( { engineName } ) , } ) , reducers : ( ) => ( { expandedRows : [ { } , { displayRow : ( expandedRows , { itemId } ) => ( { ... expandedRows , [ itemId ] : true , } ) , hideRow : ( expandedRows , { itemId } ) => { const newRows = { ... expandedRows } ; delete newRows [ itemId ] ; return newRows ; } , } , ] , sourceEngines : [ { } , { addSourceEngines : ( currentSourceEngines , { sourceEngines : newSourceEngines } ) => ( { ... currentSourceEngines , ... newSourceEngines , } ) , } , ] , } ) , selectors : { expandedSourceEngines : [ ( selectors ) => [ selectors . sourceEngines , selectors . expandedRows ] , ( sourceEngines , expandedRows ) => { return Object . keys ( expandedRows ) . reduce ( ( expandedRowMap , engineName ) => { expandedRowMap [ engineName ] = sourceEngines [ engineName ] ; return expandedRowMap ; } , { } as MetaEnginesTableValues [ 'sourceEngines' ] ) ; } , ] , } , listeners : ( { actions , values } ) => ( { fetchOrDisplayRow : ( { itemId } ) => { const sourceEngines = values . sourceEngines ; if ( sourceEngines [ itemId ] ) { actions . displayRow ( itemId ) ; } else { actions . fetchSourceEngines ( itemId ) ; } } , fetchSourceEngines : ( { engineName } ) => { recursivelyFetchEngines ( { endpoint : `/internal/app_search/engines/${ engineName }/source_engines` , onComplete : ( sourceEngines ) => { actions . addSourceEngines ( { [ engineName ] : sourceEngines } ) ; actions . displayRow ( engineName ) ; } , } ) ; } , } ) , } ) ;