From f601fdc1b305a800329713a6022170c4cfb12ce3 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 26 Aug 2021 08:45:21 -0400 Subject: [PATCH] [DataViews] Fix checking remote clusters in empty state (#110054) (#110194) Co-authored-by: Anton Dosov --- .../empty_prompts/empty_prompts.tsx | 21 ++++++------ .../index_pattern_editor_flyout_content.tsx | 32 ++++--------------- 2 files changed, 17 insertions(+), 36 deletions(-) diff --git a/src/plugins/index_pattern_editor/public/components/empty_prompts/empty_prompts.tsx b/src/plugins/index_pattern_editor/public/components/empty_prompts/empty_prompts.tsx index 2f1631694e952..696194d8113c7 100644 --- a/src/plugins/index_pattern_editor/public/components/empty_prompts/empty_prompts.tsx +++ b/src/plugins/index_pattern_editor/public/components/empty_prompts/empty_prompts.tsx @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import React, { useState, useCallback, FC } from 'react'; +import React, { useState, FC, useEffect } from 'react'; import useAsync from 'react-use/lib/useAsync'; import { useKibana } from '../../shared_imports'; @@ -47,6 +47,8 @@ export const EmptyPrompts: FC = ({ allSources, onCancel, children, loadSo } = useKibana(); const [remoteClustersExist, setRemoteClustersExist] = useState(false); + const [hasCheckedRemoteClusters, setHasCheckedRemoteClusters] = useState(false); + const [goToForm, setGoToForm] = useState(false); const hasDataIndices = allSources.some(isUserDataIndex); @@ -54,9 +56,10 @@ export const EmptyPrompts: FC = ({ allSources, onCancel, children, loadSo indexPatternService.hasUserIndexPattern().catch(() => true) ); - useCallback(() => { - let isMounted = true; - if (!hasDataIndices) + useEffect(() => { + if (!hasDataIndices && !hasCheckedRemoteClusters) { + setHasCheckedRemoteClusters(true); + getIndices({ http, isRollupIndex: () => false, @@ -64,14 +67,10 @@ export const EmptyPrompts: FC = ({ allSources, onCancel, children, loadSo showAllIndices: false, searchClient, }).then((dataSources) => { - if (isMounted) { - setRemoteClustersExist(!!dataSources.filter(removeAliases).length); - } + setRemoteClustersExist(!!dataSources.filter(removeAliases).length); }); - return () => { - isMounted = false; - }; - }, [http, hasDataIndices, searchClient]); + } + }, [http, hasDataIndices, searchClient, hasCheckedRemoteClusters]); if (hasUserIndexPattern.loading) return null; // return null to prevent UI flickering while loading diff --git a/src/plugins/index_pattern_editor/public/components/index_pattern_editor_flyout_content.tsx b/src/plugins/index_pattern_editor/public/components/index_pattern_editor_flyout_content.tsx index 0eed74053f667..c4d8ed11fe7c2 100644 --- a/src/plugins/index_pattern_editor/public/components/index_pattern_editor_flyout_content.tsx +++ b/src/plugins/index_pattern_editor/public/components/index_pattern_editor_flyout_content.tsx @@ -69,7 +69,6 @@ const IndexPatternEditorFlyoutContentComponent = ({ defaultTypeIsRollup, requireTimestampField = false, }: Props) => { - const isMounted = useRef(false); const { services: { http, indexPatternService, uiSettings, searchClient }, } = useKibana(); @@ -156,19 +155,14 @@ const IndexPatternEditorFlyoutContentComponent = ({ // loading list of index patterns useEffect(() => { - isMounted.current = true; loadSources(); const getTitles = async () => { const indexPatternTitles = await indexPatternService.getTitles(); - if (isMounted.current) { - setExistingIndexPatterns(indexPatternTitles); - setIsLoadingIndexPatterns(false); - } + + setExistingIndexPatterns(indexPatternTitles); + setIsLoadingIndexPatterns(false); }; getTitles(); - return () => { - isMounted.current = false; - }; }, [http, indexPatternService, loadSources]); // loading rollup info @@ -176,10 +170,8 @@ const IndexPatternEditorFlyoutContentComponent = ({ const getRollups = async () => { try { const response = await http.get('/api/rollup/indices'); - if (isMounted.current) { - if (response) { - setRollupIndicesCapabilities(response); - } + if (response) { + setRollupIndicesCapabilities(response); } } catch (e) { // Silently swallow failure responses such as expired trials @@ -214,10 +206,7 @@ const IndexPatternEditorFlyoutContentComponent = ({ ); timestampOptions = extractTimeFields(fields, requireTimestampField); } - if ( - isMounted.current && - currentLoadingTimestampFieldsIdx === currentLoadingTimestampFieldsRef.current - ) { + if (currentLoadingTimestampFieldsIdx === currentLoadingTimestampFieldsRef.current) { setIsLoadingTimestampFields(false); setTimestampFieldOptions(timestampOptions); } @@ -266,10 +255,7 @@ const IndexPatternEditorFlyoutContentComponent = ({ exactMatched: [], }; - if ( - currentLoadingMatchedIndicesIdx === currentLoadingMatchedIndicesRef.current && - isMounted.current - ) { + if (currentLoadingMatchedIndicesIdx === currentLoadingMatchedIndicesRef.current) { // we are still interested in this result if (type === INDEX_PATTERN_TYPE.ROLLUP) { const rollupIndices = exactMatched.filter((index) => isRollupIndex(index.name)); @@ -291,10 +277,6 @@ const IndexPatternEditorFlyoutContentComponent = ({ [http, allowHidden, allSources, type, rollupIndicesCapabilities, searchClient, isLoadingSources] ); - useEffect(() => { - reloadMatchedIndices(title); - }, [allowHidden, reloadMatchedIndices, title]); - const onTypeChange = useCallback( (newType) => { form.setFieldValue('title', '');