Skip to content

Commit 9d71c31

Browse files
test commit
1 parent a9caa8b commit 9d71c31

1 file changed

Lines changed: 42 additions & 55 deletions

File tree

  • src/components/pipelines-overview

src/components/pipelines-overview/utils.ts

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import {
44
K8sResourceKindReference,
55
PrometheusResponse,
66
} from '@openshift-console/dynamic-plugin-sdk';
7-
import { useState, useCallback, useEffect } from 'react';
7+
import { useCallback, useEffect, useRef, useState } from 'react';
88
import { useTranslation } from 'react-i18next';
9-
import { useLocation, useNavigate } from 'react-router';
9+
import { useSearchParams } from 'react-router';
1010
import { ALL_NAMESPACES_KEY } from '../../consts';
1111
import { adjustToStartOfWeek } from '../pipelines-metrics/utils';
1212

@@ -286,64 +286,51 @@ export const useQueryParams = (param) => {
286286
loadFormat,
287287
value,
288288
} = param;
289-
const [isLoaded, setIsLoaded] = useState(0);
290-
const navigate = useNavigate();
291-
const location = useLocation();
292-
const queryParams = {};
293-
location.search
294-
.substring(1)
295-
?.split('&')
296-
.forEach((_) => {
297-
const [key, value] = _.split('=');
298-
if (key) queryParams[key] = value;
299-
});
300-
301-
function setQueryParams(key?: string, value?: string) {
302-
const path = location.pathname;
303-
304-
if (key && value) queryParams[key] = value;
305-
navigate(
306-
`${path}?${Object.keys(queryParams)
307-
.map((k) => {
308-
const v = queryParams[k];
309-
if (k) return k + '=' + v;
310-
})
311-
.join('&')}`,
312-
);
313-
}
289+
const [searchParams, setSearchParams] = useSearchParams();
290+
const initializedRef = useRef(false);
314291

315-
//Loads Url Params Data
316292
useEffect(() => {
317-
if (queryParams[key]) {
318-
const paramValue = queryParams[key];
319-
if (!options || options[paramValue])
320-
setValue(loadFormat ? loadFormat(paramValue) : paramValue);
321-
}
322-
}, []);
323-
//If Url Params doesn't contain a key, initializes with defaultValue
324-
useEffect(() => {
325-
if (isLoaded >= 0) {
326-
if (!queryParams[key]) {
327-
const newValue = displayFormat
328-
? displayFormat(defaultValue)
329-
: defaultValue;
330-
if (newValue) {
331-
setQueryParams(key, newValue);
332-
setIsLoaded(isLoaded + 1);
333-
}
334-
} else {
335-
setIsLoaded(-1);
293+
const urlValue = searchParams.get(key);
294+
if (urlValue) {
295+
if (!options || options[urlValue]) {
296+
setValue(loadFormat ? loadFormat(urlValue) : urlValue);
297+
}
298+
} else {
299+
const formatted = displayFormat
300+
? displayFormat(defaultValue)
301+
: defaultValue;
302+
if (formatted) {
303+
setSearchParams(
304+
(prev) => {
305+
prev.set(key, String(formatted));
306+
return prev;
307+
},
308+
{ replace: true },
309+
);
336310
}
337311
}
338-
}, [isLoaded]);
339-
//Updating Url Params when values of filter changes
312+
initializedRef.current = true;
313+
}, []);
314+
340315
useEffect(() => {
341-
const newValue = displayFormat ? displayFormat(value) : value;
342-
if (newValue) {
343-
setQueryParams(key, newValue);
344-
} else if (queryParams[key]) {
345-
delete queryParams[key];
346-
setQueryParams();
316+
if (!initializedRef.current) return;
317+
const formatted = displayFormat ? displayFormat(value) : value;
318+
if (formatted) {
319+
setSearchParams(
320+
(prev) => {
321+
prev.set(key, String(formatted));
322+
return prev;
323+
},
324+
{ replace: true },
325+
);
326+
} else {
327+
setSearchParams(
328+
(prev) => {
329+
prev.delete(key);
330+
return prev;
331+
},
332+
{ replace: true },
333+
);
347334
}
348335
}, [value]);
349336
};

0 commit comments

Comments
 (0)