import { Reducer } from 'react' ; import { State } from './store' ; import { OnHighlightChangeArgs } from '../components/profile_tree' ; import { ShardSerialized , Targets } from '../types' ; import { hasSearch , hasAggregations } from '../lib' ; export type Action = { type : 'setProfiling' ; value : boolean ; } | { type : 'setHighlightDetails' ; value : OnHighlightChangeArgs | null ; } | { type : 'setActiveTab' ; value : Targets | null ; } | { type : 'setCurrentResponse' ; value : ShardSerialized [ ] | null ; } ; export const reducer = ( state , action ) => { const nextState = { ... state } ; if ( action . type === 'setProfiling' ) { nextState . pristine = false ; nextState . profiling = action . value ; if ( nextState . profiling ) { nextState . currentResponse = null ; nextState . highlightDetails = null ; } return nextState ; } if ( action . type === 'setHighlightDetails' ) { if ( action . value ) { const value = action . value ; const { children , parent , ... restOfOperation } = value . operation ; nextState . highlightDetails = { indexName : value . indexName , operation : Object . freeze ( restOfOperation ) , shardName : `[${ value . shard . id [ 0 ] }][${ value . shard . id [ 2 ] }]` , } ; } else { nextState . highlightDetails = null ; } return nextState ; } if ( action . type === 'setActiveTab' ) { nextState . activeTab = action . value ; return nextState ; } if ( action . type === 'setCurrentResponse' ) { nextState . currentResponse = action . value ; if ( nextState . currentResponse ) { const currentResponseHasAggregations = hasAggregations ( nextState . currentResponse ) ; const currentResponseHasSearch = hasSearch ( nextState . currentResponse ) ; if ( nextState . activeTab === 'searches' && ! currentResponseHasSearch && currentResponseHasAggregations ) { nextState . activeTab = 'aggregations' ; } else if ( nextState . activeTab === 'aggregations' && ! currentResponseHasAggregations && currentResponseHasSearch ) { nextState . activeTab = 'searches' ; } else if ( ! nextState . activeTab ) { nextState . activeTab = 'searches' ; } } else { nextState . activeTab = null ; } return nextState ; } throw new Error ( `Unknown action: ${ action }` ) ; } ;