-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vmui: fix URL params handling for navigation (#6284)
This PR fixes the handling of URL parameters to ensure correct browser navigation using the back and forward buttons. #6126 #5516 (comment) (cherry picked from commit f14497f)
- Loading branch information
Showing
7 changed files
with
158 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 111 additions & 22 deletions
133
app/vmui/packages/vmui/src/pages/CustomPanel/hooks/useSetQueryParams.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,127 @@ | ||
import { useEffect } from "react"; | ||
import { useTimeState } from "../../../state/time/TimeStateContext"; | ||
import { useCustomPanelState } from "../../../state/customPanel/CustomPanelStateContext"; | ||
import { useAppState } from "../../../state/common/StateContext"; | ||
import { useQueryState } from "../../../state/query/QueryStateContext"; | ||
import { useEffect, useState } from "react"; | ||
import { useTimeDispatch, useTimeState } from "../../../state/time/TimeStateContext"; | ||
import { useCustomPanelDispatch, useCustomPanelState } from "../../../state/customPanel/CustomPanelStateContext"; | ||
import { useAppDispatch, useAppState } from "../../../state/common/StateContext"; | ||
import { useQueryDispatch, useQueryState } from "../../../state/query/QueryStateContext"; | ||
import { displayTypeTabs } from "../DisplayTypeSwitch"; | ||
import { compactObject } from "../../../utils/object"; | ||
import { useGraphState } from "../../../state/graph/GraphStateContext"; | ||
import { useGraphDispatch, useGraphState } from "../../../state/graph/GraphStateContext"; | ||
import { useSearchParams } from "react-router-dom"; | ||
import { useCallback } from "preact/compat"; | ||
import { getInitialDisplayType } from "../../../state/customPanel/reducer"; | ||
import { getInitialTimeState } from "../../../state/time/reducer"; | ||
import useEventListener from "../../../hooks/useEventListener"; | ||
import { getQueryArray } from "../../../utils/query-string"; | ||
import { arrayEquals } from "../../../utils/array"; | ||
import { isEqualURLSearchParams } from "../../../utils/url"; | ||
|
||
export const useSetQueryParams = () => { | ||
const { tenantId } = useAppState(); | ||
const { displayType } = useCustomPanelState(); | ||
const { query } = useQueryState(); | ||
const { duration, relativeTime, period: { date, step } } = useTimeState(); | ||
const { customStep } = useGraphState(); | ||
const [, setSearchParams] = useSearchParams(); | ||
const [searchParams, setSearchParams] = useSearchParams(); | ||
|
||
const dispatch = useAppDispatch(); | ||
const timeDispatch = useTimeDispatch(); | ||
const graphDispatch = useGraphDispatch(); | ||
const queryDispatch = useQueryDispatch(); | ||
const customPanelDispatch = useCustomPanelDispatch(); | ||
|
||
const [isPopstate, setIsPopstate] = useState(false); | ||
|
||
const setterSearchParams = useCallback(() => { | ||
if (isPopstate) { | ||
// After the popstate event, the states synchronizes with the searchParams, | ||
// so there's no need to refresh the searchParams again. | ||
setIsPopstate(false); | ||
return; | ||
} | ||
|
||
const newSearchParams = new URLSearchParams(searchParams); | ||
|
||
const setSearchParamsFromState = () => { | ||
const params: Record<string, unknown> = {}; | ||
query.forEach((q, i) => { | ||
const group = `g${i}`; | ||
params[`${group}.expr`] = q; | ||
params[`${group}.range_input`] = duration; | ||
params[`${group}.end_input`] = date; | ||
params[`${group}.tab`] = displayTypeTabs.find(t => t.value === displayType)?.prometheusCode || 0; | ||
params[`${group}.relative_time`] = relativeTime; | ||
params[`${group}.tenantID`] = tenantId; | ||
|
||
if ((step !== customStep) && customStep) params[`${group}.step_input`] = customStep; | ||
if ((searchParams.get(`${group}.expr`) !== q) && q) { | ||
newSearchParams.set(`${group}.expr`, q); | ||
} | ||
|
||
if (searchParams.get(`${group}.range_input`) !== duration) { | ||
newSearchParams.set(`${group}.range_input`, duration); | ||
} | ||
|
||
if (searchParams.get(`${group}.end_input`) !== date) { | ||
newSearchParams.set(`${group}.end_input`, date); | ||
} | ||
|
||
if (searchParams.get(`${group}.relative_time`) !== relativeTime) { | ||
newSearchParams.set(`${group}.relative_time`, relativeTime || "none"); | ||
} | ||
|
||
const stepFromUrl = searchParams.get(`${group}.step_input`) || step; | ||
if (stepFromUrl && (stepFromUrl !== customStep)) { | ||
newSearchParams.set(`${group}.step_input`, customStep); | ||
} | ||
|
||
const displayTypeCode = `${displayTypeTabs.find(t => t.value === displayType)?.prometheusCode || 0}`; | ||
if (searchParams.get(`${group}.tab`) !== displayTypeCode) { | ||
newSearchParams.set(`${group}.tab`, `${displayTypeCode}`); | ||
} | ||
|
||
if (searchParams.get(`${group}.tenantID`) !== tenantId && tenantId) { | ||
newSearchParams.set(`${group}.tenantID`, tenantId); | ||
} | ||
}); | ||
if (isEqualURLSearchParams(newSearchParams, searchParams) || !newSearchParams.size) return; | ||
setSearchParams(newSearchParams); | ||
}, [tenantId, displayType, query, duration, relativeTime, date, step, customStep]); | ||
|
||
useEffect(() => { | ||
const timer = setTimeout(setterSearchParams, 200); | ||
return () => clearTimeout(timer); | ||
}, [setterSearchParams]); | ||
|
||
useEffect(() => { | ||
// Synchronize the states with searchParams only after the popstate event. | ||
if (!isPopstate) return; | ||
|
||
const timeFromUrl = getInitialTimeState(); | ||
const isDurationDifferent = (timeFromUrl.duration !== duration); | ||
const isRelativeTimeDifferent = timeFromUrl.relativeTime !== relativeTime; | ||
const isDateDifferent = timeFromUrl.relativeTime === "none" && timeFromUrl.period.date !== date; | ||
const someNotEqual = isDurationDifferent || isRelativeTimeDifferent || isDateDifferent; | ||
if (someNotEqual) { | ||
timeDispatch({ type: "SET_TIME_STATE", payload: timeFromUrl }); | ||
} | ||
|
||
const displayTypeFromUrl = getInitialDisplayType(); | ||
if (displayTypeFromUrl !== displayType) { | ||
customPanelDispatch({ type: "SET_DISPLAY_TYPE", payload: displayTypeFromUrl }); | ||
} | ||
|
||
const tenantIdFromUrl = searchParams.get("g0.tenantID") || ""; | ||
if (tenantIdFromUrl !== tenantId) { | ||
dispatch({ type: "SET_TENANT_ID", payload: tenantIdFromUrl }); | ||
} | ||
|
||
const queryFromUrl = getQueryArray(); | ||
if (!arrayEquals(queryFromUrl, query)) { | ||
queryDispatch({ type: "SET_QUERY", payload: queryFromUrl }); | ||
timeDispatch({ type: "RUN_QUERY" }); | ||
} | ||
|
||
// Timer prevents customStep reset on time range change. | ||
const timer = setTimeout(() => { | ||
const customStepFromUrl = searchParams.get("g0.step_input") || step; | ||
if (customStepFromUrl && customStepFromUrl !== customStep) { | ||
graphDispatch({ type: "SET_CUSTOM_STEP", payload: customStepFromUrl }); | ||
} | ||
}, 50); | ||
|
||
setSearchParams(compactObject(params) as Record<string, string>); | ||
}; | ||
return () => clearTimeout(timer); | ||
}, [searchParams, isPopstate]); | ||
|
||
useEffect(setSearchParamsFromState, [tenantId, displayType, query, duration, relativeTime, date, step, customStep]); | ||
useEffect(setSearchParamsFromState, []); | ||
useEventListener("popstate", () => { | ||
setIsPopstate(true); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters