From baa6510547a79aab39ad819a94f2330cd2fca2c5 Mon Sep 17 00:00:00 2001 From: Maja Grubic Date: Thu, 27 Jan 2022 15:05:58 +0100 Subject: [PATCH] [Discover] Redirect if no data views (#123366) * [Discover] Redirect if new Kibana instance * Add a functional test * Remove state; add redirect * Code polishing Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../application/main/discover_main_route.tsx | 27 ++++++++++++++--- test/functional/apps/discover/_empty_state.ts | 30 +++++++++++++++++++ test/functional/apps/discover/index.ts | 1 + 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 test/functional/apps/discover/_empty_state.ts diff --git a/src/plugins/discover/public/application/main/discover_main_route.tsx b/src/plugins/discover/public/application/main/discover_main_route.tsx index dd1d036b811a..f1d7cc2385cd 100644 --- a/src/plugins/discover/public/application/main/discover_main_route.tsx +++ b/src/plugins/discover/public/application/main/discover_main_route.tsx @@ -5,7 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ -import React, { useEffect, useState, memo } from 'react'; +import React, { useEffect, useState, memo, useCallback } from 'react'; import { History } from 'history'; import { useParams } from 'react-router-dom'; @@ -21,10 +21,10 @@ import { DiscoverMainApp } from './discover_main_app'; import { getRootBreadcrumbs, getSavedSearchBreadcrumbs } from '../../utils/breadcrumbs'; import { redirectWhenMissing } from '../../../../kibana_utils/public'; import { DataViewSavedObjectConflictError } from '../../../../data_views/common'; -import { getUrlTracker } from '../../kibana_services'; import { LoadingIndicator } from '../../components/common/loading_indicator'; import { DiscoverError } from '../../components/common/error_alert'; import { DiscoverRouteProps } from '../types'; +import { getUrlTracker } from '../../kibana_services'; const DiscoverMainAppMemoized = memo(DiscoverMainApp); @@ -54,15 +54,29 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) { const [indexPatternList, setIndexPatternList] = useState< Array> >([]); - const { id } = useParams(); + const navigateToOverview = useCallback(() => { + core.application.navigateToApp('kibanaOverview', { path: '#' }); + }, [core.application]); + + const checkForDataViews = useCallback(async () => { + const hasUserDataView = await data.dataViews.hasUserDataView().catch(() => true); + if (!hasUserDataView) { + navigateToOverview(); + } + const defaultDataView = await data.dataViews.getDefaultDataView(); + if (!defaultDataView) { + navigateToOverview(); + } + }, [navigateToOverview, data.dataViews]); + useEffect(() => { const savedSearchId = id; async function loadDefaultOrCurrentIndexPattern(searchSource: ISearchSource) { try { - await data.indexPatterns.ensureDefaultDataView(); + await checkForDataViews(); const { appStateContainer } = getState({ history, uiSettings: config }); const { index } = appStateContainer.getState(); const ip = await loadIndexPattern(index || '', data.indexPatterns, config); @@ -90,6 +104,10 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) { currentSavedSearch.searchSource ); + if (!loadedIndexPattern) { + return; + } + if (!currentSavedSearch.searchSource.getField('index')) { currentSavedSearch.searchSource.setField('index', loadedIndexPattern); } @@ -141,6 +159,7 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) { services, toastNotifications, core.theme, + checkForDataViews, ]); useEffect(() => { diff --git a/test/functional/apps/discover/_empty_state.ts b/test/functional/apps/discover/_empty_state.ts new file mode 100644 index 000000000000..e78f5de8bd78 --- /dev/null +++ b/test/functional/apps/discover/_empty_state.ts @@ -0,0 +1,30 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import expect from '@kbn/expect'; +import { FtrProviderContext } from '../../ftr_provider_context'; + +export default function ({ getService, getPageObjects }: FtrProviderContext) { + const testSubjects = getService('testSubjects'); + const kibanaServer = getService('kibanaServer'); + const PageObjects = getPageObjects(['common', 'timePicker', 'discover']); + + describe('empty state', () => { + before(async () => { + await kibanaServer.uiSettings.unset('defaultIndex'); + await kibanaServer.savedObjects.clean({ types: ['search', 'index-pattern'] }); + }); + + it('redirects to Overview app', async () => { + await PageObjects.common.navigateToApp('discover'); + const selector = await testSubjects.find('kibanaChrome'); + const content = await selector.findByCssSelector('.kbnNoDataPageContents'); + expect(content).not.to.be(null); + }); + }); +} diff --git a/test/functional/apps/discover/index.ts b/test/functional/apps/discover/index.ts index 1241b0e892e9..b5eb16052687 100644 --- a/test/functional/apps/discover/index.ts +++ b/test/functional/apps/discover/index.ts @@ -54,5 +54,6 @@ export default function ({ getService, loadTestFile }: FtrProviderContext) { loadTestFile(require.resolve('./_search_on_page_load')); loadTestFile(require.resolve('./_chart_hidden')); loadTestFile(require.resolve('./_context_encoded_url_param')); + loadTestFile(require.resolve('./_empty_state')); }); }