Skip to content

Commit

Permalink
[Discover] Redirect if no data views (#123366)
Browse files Browse the repository at this point in the history
* [Discover] Redirect if new Kibana instance

* Add a functional test

* Remove state; add redirect

* Code polishing

Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
Maja Grubic and kibanamachine authored Jan 27, 2022
1 parent 4f1d97a commit baa6510
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);

Expand Down Expand Up @@ -54,15 +54,29 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) {
const [indexPatternList, setIndexPatternList] = useState<
Array<SavedObject<IndexPatternAttributes>>
>([]);

const { id } = useParams<DiscoverLandingParams>();

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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -141,6 +159,7 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) {
services,
toastNotifications,
core.theme,
checkForDataViews,
]);

useEffect(() => {
Expand Down
30 changes: 30 additions & 0 deletions test/functional/apps/discover/_empty_state.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
1 change: 1 addition & 0 deletions test/functional/apps/discover/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
}

0 comments on commit baa6510

Please sign in to comment.