import { createStateContainer , IOsdUrlStateStorage , syncState , } from '../../../../opensearch_dashboards_utils/public' ; import { QuerySetup , QueryStart } from '../query_service' ; import { connectToQueryState } from './connect_to_query_state' ; import { QueryState } from './types' ; import { FilterStateStore } from '../../../common/opensearch_query/filters' ; const GLOBAL_STATE_STORAGE_KEY = '_g' ; param QueryService param osdUrlStateStorage export const syncQueryStateWithUrl = ( query , osdUrlStateStorage ) => { const { timefilter : { timefilter } , filterManager , } = query ; const defaultState = { time : timefilter . getTime ( ) , refreshInterval : timefilter . getRefreshInterval ( ) , filters : filterManager . getGlobalFilters ( ) , } ; const initialStateFromUrl = osdUrlStateStorage . get < QueryState > ( GLOBAL_STATE_STORAGE_KEY ) ; const hasInheritedQueryFromUrl = Boolean ( initialStateFromUrl && Object . keys ( initialStateFromUrl ) . length ) ; const initialState = { ... defaultState , ... initialStateFromUrl , } ; const globalQueryStateContainer = createStateContainer ( initialState ) ; const stopSyncingWithStateContainer = connectToQueryState ( query , globalQueryStateContainer , { refreshInterval : true , time : true , filters : FilterStateStore . GLOBAL_STATE , } ) ; if ( ! initialStateFromUrl ) { osdUrlStateStorage . set < QueryState > ( GLOBAL_STATE_STORAGE_KEY , initialState , { replace : true , } ) ; } globalQueryStateContainer . set ( initialState ) ; const { start , stop : stopSyncingWithUrl } = syncState ( { stateStorage : osdUrlStateStorage , stateContainer : { ... globalQueryStateContainer , set : ( state ) => { if ( state ) { globalQueryStateContainer . set ( state ) ; } } , } , storageKey : GLOBAL_STATE_STORAGE_KEY , } ) ; start ( ) ; return { stop : ( ) => { stopSyncingWithStateContainer ( ) ; stopSyncingWithUrl ( ) ; } , hasInheritedQueryFromUrl , } ; } ;