Skip to content

Commit

Permalink
Remove state; add redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
Maja Grubic committed Jan 26, 2022
1 parent e13dd15 commit 5f46ef4
Showing 1 changed file with 15 additions and 12 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 Down Expand Up @@ -54,27 +54,33 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) {
const [indexPatternList, setIndexPatternList] = useState<
Array<SavedObject<IndexPatternAttributes>>
>([]);
const [isNewKibanaInstance, setNewKibanaInstance] = useState(false);
const { id } = useParams<DiscoverLandingParams>();

const navigateToOverview = useCallback(() => {
core.application.navigateToApp('kibanaOverview', { path: '#' });
}, [core.application]);

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

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

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

useEffect(() => {
const savedSearchId = id;

async function loadDefaultOrCurrentIndexPattern(searchSource: ISearchSource) {
try {
const defaultIndexPattern = await data.dataViews.getDefaultDataView();
if (!defaultIndexPattern) {
setNewKibanaInstance(true);
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 });
Expand Down Expand Up @@ -160,6 +166,7 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) {
toastNotifications,
core.theme,
data.dataViews,
navigateToOverview,
]);

useEffect(() => {
Expand All @@ -174,10 +181,6 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) {
return <DiscoverError error={error} />;
}

if (isNewKibanaInstance) {
core.application.navigateToApp('kibanaOverview', { path: '#' });
}

if (!indexPattern || !savedSearch) {
return <LoadingIndicator />;
}
Expand Down

0 comments on commit 5f46ef4

Please sign in to comment.