mirror of
https://git.mirrors.martin98.com/https://github.com/SigNoz/signoz
synced 2025-08-12 03:39:02 +08:00
fix: fix the race condition resulting in switching between views not working properly
This commit is contained in:
parent
94e0423479
commit
e88e24e434
@ -1,7 +1,8 @@
|
|||||||
import useUrlQuery from 'hooks/useUrlQuery';
|
|
||||||
import { useCallback, useMemo } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
import { useHistory, useLocation } from 'react-router-dom';
|
import { useHistory, useLocation } from 'react-router-dom';
|
||||||
|
|
||||||
|
import useUrlQuery from './useUrlQuery';
|
||||||
|
|
||||||
const useUrlQueryData = <T>(
|
const useUrlQueryData = <T>(
|
||||||
queryKey: string,
|
queryKey: string,
|
||||||
defaultData?: T,
|
defaultData?: T,
|
||||||
@ -10,7 +11,7 @@ const useUrlQueryData = <T>(
|
|||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const urlQuery = useUrlQuery();
|
const urlQuery = useUrlQuery();
|
||||||
|
|
||||||
const query = useMemo(() => urlQuery.get(queryKey), [queryKey, urlQuery]);
|
const query = useMemo(() => urlQuery.get(queryKey), [urlQuery, queryKey]);
|
||||||
|
|
||||||
const queryData: T = useMemo(() => (query ? JSON.parse(query) : defaultData), [
|
const queryData: T = useMemo(() => (query ? JSON.parse(query) : defaultData), [
|
||||||
query,
|
query,
|
||||||
@ -21,11 +22,17 @@ const useUrlQueryData = <T>(
|
|||||||
(newQueryData: T): void => {
|
(newQueryData: T): void => {
|
||||||
const newQuery = JSON.stringify(newQueryData);
|
const newQuery = JSON.stringify(newQueryData);
|
||||||
|
|
||||||
urlQuery.set(queryKey, newQuery);
|
// Create a new URLSearchParams to get the latest URL state
|
||||||
const generatedUrl = `${location.pathname}?${urlQuery.toString()}`;
|
const currentUrlQuery = new URLSearchParams(window.location.search);
|
||||||
|
|
||||||
|
// Update the query parameter
|
||||||
|
currentUrlQuery.set(queryKey, newQuery);
|
||||||
|
|
||||||
|
// Generate the new URL with updated parameters
|
||||||
|
const generatedUrl = `${location.pathname}?${currentUrlQuery.toString()}`;
|
||||||
history.replace(generatedUrl);
|
history.replace(generatedUrl);
|
||||||
},
|
},
|
||||||
[history, location, urlQuery, queryKey],
|
[history, location.pathname, queryKey],
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user