diff --git a/src/plugins/dashboard/public/dashboard_container/embeddable/create/create_dashboard.ts b/src/plugins/dashboard/public/dashboard_container/embeddable/create/create_dashboard.ts index 124969c481798..cdd9cfd7d4dcd 100644 --- a/src/plugins/dashboard/public/dashboard_container/embeddable/create/create_dashboard.ts +++ b/src/plugins/dashboard/public/dashboard_container/embeddable/create/create_dashboard.ts @@ -13,18 +13,19 @@ import { CONTROL_GROUP_TYPE, getDefaultControlGroupInput, } from '@kbn/controls-plugin/common'; -import { GlobalQueryStateFromUrl, syncGlobalQueryStateWithUrl } from '@kbn/data-plugin/public'; +import { TimeRange } from '@kbn/es-query'; import { isErrorEmbeddable, ViewMode } from '@kbn/embeddable-plugin/public'; import { lazyLoadReduxToolsPackage } from '@kbn/presentation-util-plugin/public'; import { type ControlGroupContainer, ControlGroupOutput } from '@kbn/controls-plugin/public'; +import { GlobalQueryStateFromUrl, syncGlobalQueryStateWithUrl } from '@kbn/data-plugin/public'; import { DashboardContainerInput } from '../../../../common'; import { DashboardContainer } from '../dashboard_container'; import { pluginServices } from '../../../services/plugin_services'; -import { DEFAULT_DASHBOARD_INPUT, GLOBAL_STATE_STORAGE_KEY } from '../../../dashboard_constants'; import { DashboardCreationOptions } from '../dashboard_container_factory'; import { startSyncingDashboardDataViews } from './data_views/sync_dashboard_data_views'; import { syncUnifiedSearchState } from './unified_search/sync_dashboard_unified_search_state'; +import { DEFAULT_DASHBOARD_INPUT, GLOBAL_STATE_STORAGE_KEY } from '../../../dashboard_constants'; import { startSyncingDashboardControlGroup } from './controls/dashboard_control_group_integration'; import { startDashboardSearchSessionIntegration } from './search_sessions/start_dashboard_search_session_integration'; import { LoadDashboardFromSavedObjectReturn } from '../../../services/dashboard_saved_object/lib/load_dashboard_state_from_saved_object'; @@ -174,7 +175,13 @@ export const initializeDashboard = async ({ // Set up unified search integration. // -------------------------------------------------------------------------------------- if (useUnifiedSearchIntegration && unifiedSearchSettings?.kbnUrlStateStorage) { - const { filters, query, timeRestore, timeRange, refreshInterval } = initialInput; + const { + query, + filters, + timeRestore, + timeRange: savedTimeRange, + refreshInterval: savedRefreshInterval, + } = initialInput; const { kbnUrlStateStorage } = unifiedSearchSettings; // apply filters and query to the query service @@ -182,17 +189,24 @@ export const initializeDashboard = async ({ queryString.setQuery(query ?? queryString.getDefaultQuery()); /** - * If a global time range is not set explicitly and the time range was saved with the dashboard, apply - * time range and refresh interval to the query service. Otherwise, set the current dashboard time range - * from the query service. The order of the following lines is very important. + * Get initial time range, and set up dashboard time restore if applicable */ - const inheritedTimeRange = - kbnUrlStateStorage.get(GLOBAL_STATE_STORAGE_KEY)?.time; - if (inheritedTimeRange || !timeRestore) { - initialInput.timeRange = inheritedTimeRange; - } else if (timeRestore) { - if (timeRange) timefilterService.setTime(timeRange); - if (refreshInterval) timefilterService.setRefreshInterval(refreshInterval); + const initialTimeRange: TimeRange = (() => { + // if there is an explicit time range in the URL it always takes precedence. + const urlOverrideTimeRange = + kbnUrlStateStorage.get(GLOBAL_STATE_STORAGE_KEY)?.time; + if (urlOverrideTimeRange) return urlOverrideTimeRange; + + // if this Dashboard has timeRestore return the time range that was saved with the dashboard. + if (timeRestore && savedTimeRange) return savedTimeRange; + + // otherwise fall back to the time range from the timefilterService. + return timefilterService.getTime(); + })(); + initialInput.timeRange = initialTimeRange; + if (timeRestore) { + if (savedTimeRange) timefilterService.setTime(savedTimeRange); + if (savedRefreshInterval) timefilterService.setRefreshInterval(savedRefreshInterval); } // start syncing global query state with the URL.