From 879f72b9d76feac3bd8f7436977a273f42b5a716 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Fri, 21 Feb 2025 05:40:25 +1100 Subject: [PATCH] [8.x] [Synthetics] modify use overview status hook to mimic use_monitor_list (#210936) (#211931) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Backport This will backport the following commits from `main` to `8.x`: - [[Synthetics] modify use overview status hook to mimic use_monitor_list (#210936)](https://github.com/elastic/kibana/pull/210936) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sorenlouv/backport) Co-authored-by: Bailey Cash --- .../hooks/use_overview_status.ts | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_overview_status.ts b/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_overview_status.ts index da86b130af692..b6999fcc3a0b3 100644 --- a/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_overview_status.ts +++ b/x-pack/solutions/observability/plugins/synthetics/public/apps/synthetics/components/monitors_page/hooks/use_overview_status.ts @@ -5,8 +5,9 @@ * 2.0. */ -import { useEffect } from 'react'; +import { useEffect, useRef } from 'react'; import { useDispatch, useSelector } from 'react-redux'; +import useDebounce from 'react-use/lib/useDebounce'; import { useSyntheticsRefreshContext } from '../../../contexts/synthetics_refresh_context'; import { selectOverviewPageState } from '../../../state'; import { @@ -17,22 +18,51 @@ import { export function useOverviewStatus({ scopeStatusByLocation }: { scopeStatusByLocation: boolean }) { const pageState = useSelector(selectOverviewPageState); - const { status, error, loaded, loading, allConfigs } = useSelector(selectOverviewStatus); + const isInitialMount = useRef(true); const { lastRefresh } = useSyntheticsRefreshContext(); const dispatch = useDispatch(); + // Periodically refresh useEffect(() => { - if (loaded) { + if (!isInitialMount.current) { dispatch(quietFetchOverviewStatusAction.get({ pageState, scopeStatusByLocation })); - } else { - dispatch(fetchOverviewStatusAction.get({ pageState, scopeStatusByLocation })); } - // loaded is omitted from the dependency array because it is not used in the callback + // specifically only want to run this on refreshInterval change // eslint-disable-next-line react-hooks/exhaustive-deps - }, [dispatch, lastRefresh, pageState, scopeStatusByLocation]); + }, [lastRefresh]); + + // On initial mount, load the page + useDebounce( + () => { + if (isInitialMount.current) { + if (loaded) { + dispatch(quietFetchOverviewStatusAction.get({ pageState, scopeStatusByLocation })); + } else { + dispatch(fetchOverviewStatusAction.get({ pageState, scopeStatusByLocation })); + } + } + }, + 100, + // we don't use pageState or scopeStatus here, for pageState, useDebounce will handle it + [dispatch] + ); + + useDebounce( + () => { + // Don't load on initial mount, only meant to handle pageState changes + if (isInitialMount.current || !loaded) { + // setting false here to account for debounce timing + isInitialMount.current = false; + return; + } + dispatch(fetchOverviewStatusAction.get({ pageState, scopeStatusByLocation })); + }, + 100, + [pageState, scopeStatusByLocation] + ); return { status,