From 5f46ef4ad01b9ea1b5671d7b3ed1f98c140620b5 Mon Sep 17 00:00:00 2001 From: Maja Grubic Date: Wed, 26 Jan 2022 16:11:41 +0100 Subject: [PATCH] Remove state; add redirect --- .../application/main/discover_main_route.tsx | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) 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 8d8229328d981..f853e8d554f4f 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'; @@ -54,27 +54,33 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) { const [indexPatternList, setIndexPatternList] = useState< Array> >([]); - const [isNewKibanaInstance, setNewKibanaInstance] = useState(false); const { id } = useParams(); + 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 }); @@ -160,6 +166,7 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) { toastNotifications, core.theme, data.dataViews, + navigateToOverview, ]); useEffect(() => { @@ -174,10 +181,6 @@ export function DiscoverMainRoute({ services, history }: DiscoverMainProps) { return ; } - if (isNewKibanaInstance) { - core.application.navigateToApp('kibanaOverview', { path: '#' }); - } - if (!indexPattern || !savedSearch) { return ; }