Skip to content

Commit

Permalink
[DataViews] Fix checking remote clusters in empty state (elastic#110054)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant authored and kibanamachine committed Aug 26, 2021
1 parent 3797202 commit e0686a2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -47,31 +47,30 @@ export const EmptyPrompts: FC<Props> = ({ allSources, onCancel, children, loadSo
} = useKibana<IndexPatternEditorContext>();

const [remoteClustersExist, setRemoteClustersExist] = useState<boolean>(false);
const [hasCheckedRemoteClusters, setHasCheckedRemoteClusters] = useState<boolean>(false);

const [goToForm, setGoToForm] = useState<boolean>(false);

const hasDataIndices = allSources.some(isUserDataIndex);
const hasUserIndexPattern = useAsync(() =>
indexPatternService.hasUserIndexPattern().catch(() => true)
);

useCallback(() => {
let isMounted = true;
if (!hasDataIndices)
useEffect(() => {
if (!hasDataIndices && !hasCheckedRemoteClusters) {
setHasCheckedRemoteClusters(true);

getIndices({
http,
isRollupIndex: () => false,
pattern: '*:*',
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ const IndexPatternEditorFlyoutContentComponent = ({
defaultTypeIsRollup,
requireTimestampField = false,
}: Props) => {
const isMounted = useRef<boolean>(false);
const {
services: { http, indexPatternService, uiSettings, searchClient },
} = useKibana<IndexPatternEditorContext>();
Expand Down Expand Up @@ -156,30 +155,23 @@ 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
useEffect(() => {
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
Expand Down Expand Up @@ -214,10 +206,7 @@ const IndexPatternEditorFlyoutContentComponent = ({
);
timestampOptions = extractTimeFields(fields, requireTimestampField);
}
if (
isMounted.current &&
currentLoadingTimestampFieldsIdx === currentLoadingTimestampFieldsRef.current
) {
if (currentLoadingTimestampFieldsIdx === currentLoadingTimestampFieldsRef.current) {
setIsLoadingTimestampFields(false);
setTimestampFieldOptions(timestampOptions);
}
Expand Down Expand Up @@ -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));
Expand All @@ -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', '');
Expand Down

0 comments on commit e0686a2

Please sign in to comment.