import { TimeRange , Filter , Query , opensearchFilters , QueryState , RefreshInterval , } from '../../data/public' ; import { setStateToOsdUrl } from '../../opensearch_dashboards_utils/public' ; import { UrlGeneratorsDefinition } from '../../share/public' ; import { SavedObjectLoader } from '../../saved_objects/public' ; import { ViewMode } from '../../embeddable/public' ; export const STATE_STORAGE_KEY = '_a' ; export const GLOBAL_STATE_STORAGE_KEY = '_g' ; export const DASHBOARD_APP_URL_GENERATOR = 'DASHBOARD_APP_URL_GENERATOR' ; export interface DashboardUrlGeneratorState { /**
     * If given, the dashboard saved object with this id will be loaded. If not given,
     * a new, unsaved dashboard will be loaded up.
     */ dashboardId ? : string ; /**
     * Optionally set the time range in the time picker.
     */ timeRange ? : TimeRange ; /**
     * Optionally set the refresh interval.
     */ refreshInterval ? : RefreshInterval ; /**
     * Optionally apply filers. NOTE: if given and used in conjunction with `dashboardId`, and the
     * saved dashboard has filters saved with it, this will _replace_ those filters.
     */ filters ? : Filter [ ] ; /**
     * Optionally set a query. NOTE: if given and used in conjunction with `dashboardId`, and the
     * saved dashboard has a query saved with it, this will _replace_ that query.
     */ query ? : Query ; /**
     * If not given, will use the uiSettings configuration for `storeInSessionStorage`. useHash determines
     * whether to hash the data in the url to avoid url length issues.
     */ useHash ? : boolean ; /**
     * When `true` filters from saved filters from destination dashboard as merged with applied filters
     * When `false` applied filters take precedence and override saved filters
     *
     * true is default
     */ preserveSavedFilters ? : boolean ; /**
     * View mode of the dashboard.
     */ viewMode ? : ViewMode ; } export const createDashboardUrlGenerator = ( getStartServices ) : UrlGeneratorsDefinition < typeof DASHBOARD_APP_URL_GENERATOR > => ( { id : DASHBOARD_APP_URL_GENERATOR , createUrl : async ( state ) => { const startServices = await getStartServices ( ) ; const useHash = state . useHash ?? startServices . useHashedUrl ; const appBasePath = startServices . appBasePath ; const hash = state . dashboardId ? `view/${ state . dashboardId }` : `create` ; const getSavedFiltersFromDestinationDashboardIfNeeded = async ( ) : Promise < Filter [ ] > => { if ( state . preserveSavedFilters === false ) return [ ] ; if ( ! state . dashboardId ) return [ ] ; try { const dashboard = await startServices . savedDashboardLoader . get ( state . dashboardId ) ; return dashboard ?. searchSource ?. getField ( 'filter' ) ?? [ ] ; } catch ( e ) { return [ ] ; } } ; const cleanEmptyKeys = ( stateObj ) => { Object . keys ( stateObj ) . forEach ( ( key ) => { if ( stateObj [ key ] === undefined ) { delete stateObj [ key ] ; } } ) ; return stateObj ; } ; const filters = state . filters && [ ... ( await getSavedFiltersFromDestinationDashboardIfNeeded ( ) ) , ... state . filters , ] ; const appStateUrl = setStateToOsdUrl ( STATE_STORAGE_KEY , cleanEmptyKeys ( { query : state . query , filters : filters ?. filter ( ( f ) => ! opensearchFilters . isFilterPinned ( f ) ) , viewMode : state . viewMode , } ) , { useHash } , `${ appBasePath }#/${ hash }` ) ; return setStateToOsdUrl < QueryState > ( GLOBAL_STATE_STORAGE_KEY , cleanEmptyKeys ( { time : state . timeRange , filters : filters ?. filter ( ( f ) => opensearchFilters . isFilterPinned ( f ) ) , refreshInterval : state . refreshInterval , } ) , { useHash } , appStateUrl ) ; } , } ) ;