diff --git a/x-pack/plugins/stack_alerts/public/rule_types/components/data_view_select_popover.tsx b/x-pack/plugins/stack_alerts/public/rule_types/components/data_view_select_popover.tsx index 988afb8d2182b..97b614a5b5785 100644 --- a/x-pack/plugins/stack_alerts/public/rule_types/components/data_view_select_popover.tsx +++ b/x-pack/plugins/stack_alerts/public/rule_types/components/data_view_select_popover.tsx @@ -18,6 +18,7 @@ import { EuiPopover, EuiPopoverFooter, EuiPopoverTitle, + EuiLoadingSpinner, EuiText, useEuiPaddingCSS, useIsWithinBreakpoints, @@ -63,6 +64,7 @@ export const DataViewSelectPopover: React.FunctionComponent { + const [loadingDataViews, setLoadingDataViews] = useState(false); const [dataViewItems, setDataViewsItems] = useState([]); const [dataViewPopoverOpen, setDataViewPopoverOpen] = useState(false); @@ -71,7 +73,7 @@ export const DataViewSelectPopover: React.FunctionComponent void | undefined>(); const allDataViewItems = useMemo( - () => [...dataViewItems, ...metadata.adHocDataViewList.map(toDataViewListItem)], + () => [...(dataViewItems ?? []), ...metadata.adHocDataViewList.map(toDataViewListItem)], [dataViewItems, metadata.adHocDataViewList] ); @@ -87,10 +89,16 @@ export const DataViewSelectPopover: React.FunctionComponent { - const ids = await dataViews.getIds(); - const dataViewsList = await Promise.all(ids.map((id) => dataViews.get(id))); - - setDataViewsItems(dataViewsList.map(toDataViewListItem)); + setLoadingDataViews(true); + try { + // Calling getIds with refresh = true to make sure we don't get stale data + const ids = await dataViews.getIds(true); + const dataViewsList = await Promise.all(ids.map((id) => dataViews.get(id))); + setDataViewsItems(dataViewsList.map(toDataViewListItem)); + } catch (e) { + // Error fetching data views + } + setLoadingDataViews(false); }, [dataViews]); const onAddAdHocDataView = useCallback( @@ -153,8 +161,10 @@ export const DataViewSelectPopover: React.FunctionComponent; } return (