import _ from 'lodash' ; import { getDashboard60Warning , dashboardLoadingErrorStrings } from '../../dashboard_strings' ; import { savedObjectToDashboardState } from './convert_dashboard_state' ; import { DashboardState , DashboardBuildContext } from '../../types' ; import { DashboardConstants , DashboardSavedObject } from '../..' ; import { migrateLegacyQuery } from './migrate_legacy_query' ; import { cleanFiltersForSerialize } from './filter_utils' ; import { ViewMode } from '../../services/embeddable' ; interface LoadSavedDashboardStateReturn { savedDashboardState : DashboardState ; savedDashboard : DashboardSavedObject ; } /**
 * Loads, migrates, and returns state from a dashboard saved object.
 */ export const loadSavedDashboardState = async ( { query , history , notifications , indexPatterns , savedDashboards , usageCollection , savedDashboardId , initializerContext , savedObjectsTagging , dashboardCapabilities , } ) : Promise < LoadSavedDashboardStateReturn | undefined > => { const { showWriteControls } = dashboardCapabilities ; const { queryString } = query ; if ( savedDashboardId === 'create' ) { history . replace ( { ... history . location , pathname : DashboardConstants . CREATE_NEW_DASHBOARD_URL , } ) ; notifications . toasts . addWarning ( getDashboard60Warning ( ) ) ; return ; } await indexPatterns . ensureDefaultDataView ( ) ; try { const savedDashboard = ( await savedDashboards . get ( { id : savedDashboardId , useResolve : true , } ) ) as DashboardSavedObject ; const savedDashboardState = savedObjectToDashboardState ( { savedDashboard , usageCollection , showWriteControls , savedObjectsTagging , version : initializerContext . env . packageInfo . version , } ) ; const isViewMode = ! showWriteControls || Boolean ( savedDashboard . id ) ; savedDashboardState . viewMode = isViewMode ? ViewMode . VIEW : ViewMode . EDIT ; savedDashboardState . filters = cleanFiltersForSerialize ( savedDashboardState . filters ) ; savedDashboardState . query = migrateLegacyQuery ( savedDashboardState . query || queryString . getDefaultQuery ( ) ) ; return { savedDashboardState , savedDashboard } ; } catch ( error ) { notifications . toasts . addDanger ( dashboardLoadingErrorStrings . getDashboardLoadError ( error . message ) ) ; history . push ( DashboardConstants . LANDING_PAGE_PATH ) ; return ; } } ;