Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Redirect if no data views #123366

Merged
merged 8 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,35 @@ 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]);

useEffect(() => {
const fetchIsNewKibanaInstance = async () => {
const hasUserIndexPattern = await data.dataViews.hasUserDataView().catch(() => true);

if (!hasUserIndexPattern) {
navigateToOverview();
}
};

fetchIsNewKibanaInstance();
}, [data.dataViews, navigateToOverview]);

majagrubic marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
const savedSearchId = id;

async function loadDefaultOrCurrentIndexPattern(searchSource: ISearchSource) {
try {
await data.indexPatterns.ensureDefaultDataView();
const defaultDataView = await data.dataViews.getDefaultDataView();
const hasUserDataView = await data.dataViews.hasUserDataView().catch(() => true);
if (!defaultDataView || !hasUserDataView) {
navigateToOverview();
return;
}
const { appStateContainer } = getState({ history, uiSettings: config });
const { index } = appStateContainer.getState();
const ip = await loadIndexPattern(index || '', data.indexPatterns, config);
Expand Down Expand Up @@ -90,6 +110,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 +165,8 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) {
services,
toastNotifications,
core.theme,
data.dataViews,
navigateToOverview,
]);

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'));
});
}