From 56f260dabf80f5a4d7e5a0668a6e9033435f4a30 Mon Sep 17 00:00:00 2001 From: ahmadshaheer Date: Mon, 23 Dec 2024 19:28:07 +0430 Subject: [PATCH] refactor: integrate safeNavigate in LogsExplorerChart and deprecate the existing back navigation --- .../src/container/LogsExplorerChart/index.tsx | 41 +++---------------- 1 file changed, 6 insertions(+), 35 deletions(-) diff --git a/frontend/src/container/LogsExplorerChart/index.tsx b/frontend/src/container/LogsExplorerChart/index.tsx index 7ac1934bb7..d0acc3f2fd 100644 --- a/frontend/src/container/LogsExplorerChart/index.tsx +++ b/frontend/src/container/LogsExplorerChart/index.tsx @@ -2,14 +2,12 @@ import Graph from 'components/Graph'; import Spinner from 'components/Spinner'; import { QueryParams } from 'constants/query'; import { themeColors } from 'constants/theme'; -import { CustomTimeType } from 'container/TopNav/DateTimeSelectionV2/config'; +import { useSafeNavigate } from 'hooks/useSafeNavigate'; import useUrlQuery from 'hooks/useUrlQuery'; import getChartData, { GetChartDataProps } from 'lib/getChartData'; import GetMinMax from 'lib/getMinMax'; import { colors } from 'lib/getRandomColor'; -import getTimeString from 'lib/getTimeString'; -import history from 'lib/history'; -import { memo, useCallback, useEffect, useMemo } from 'react'; +import { memo, useCallback, useMemo } from 'react'; import { useDispatch } from 'react-redux'; import { useLocation } from 'react-router-dom'; import { UpdateTimeInterval } from 'store/actions'; @@ -28,6 +26,7 @@ function LogsExplorerChart({ const dispatch = useDispatch(); const urlQuery = useUrlQuery(); const location = useLocation(); + const { safeNavigate } = useSafeNavigate(); const handleCreateDatasets: Required['createDataset'] = useCallback( (element, index, allLabels) => ({ data: element, @@ -62,41 +61,13 @@ function LogsExplorerChart({ urlQuery.set(QueryParams.startTime, minTime.toString()); urlQuery.set(QueryParams.endTime, maxTime.toString()); + urlQuery.delete(QueryParams.relativeTime); const generatedUrl = `${location.pathname}?${urlQuery.toString()}`; - history.push(generatedUrl); + safeNavigate(generatedUrl); }, - [dispatch, location.pathname, urlQuery], + [dispatch, location.pathname, safeNavigate, urlQuery], ); - const handleBackNavigation = (): void => { - const searchParams = new URLSearchParams(window.location.search); - const startTime = searchParams.get(QueryParams.startTime); - const endTime = searchParams.get(QueryParams.endTime); - const relativeTime = searchParams.get( - QueryParams.relativeTime, - ) as CustomTimeType; - - if (relativeTime) { - dispatch(UpdateTimeInterval(relativeTime)); - } else if (startTime && endTime && startTime !== endTime) { - dispatch( - UpdateTimeInterval('custom', [ - parseInt(getTimeString(startTime), 10), - parseInt(getTimeString(endTime), 10), - ]), - ); - } - }; - - useEffect(() => { - window.addEventListener('popstate', handleBackNavigation); - - return (): void => { - window.removeEventListener('popstate', handleBackNavigation); - }; - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); - const graphData = useMemo( () => getChartData({