|
4 | 4 | K8sResourceKindReference, |
5 | 5 | PrometheusResponse, |
6 | 6 | } from '@openshift-console/dynamic-plugin-sdk'; |
7 | | -import { useState, useCallback, useEffect } from 'react'; |
| 7 | +import { useCallback, useEffect, useRef, useState } from 'react'; |
8 | 8 | import { useTranslation } from 'react-i18next'; |
9 | | -import { useLocation, useNavigate } from 'react-router'; |
| 9 | +import { useSearchParams } from 'react-router'; |
10 | 10 | import { ALL_NAMESPACES_KEY } from '../../consts'; |
11 | 11 | import { adjustToStartOfWeek } from '../pipelines-metrics/utils'; |
12 | 12 |
|
@@ -286,64 +286,51 @@ export const useQueryParams = (param) => { |
286 | 286 | loadFormat, |
287 | 287 | value, |
288 | 288 | } = 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); |
314 | 291 |
|
315 | | - //Loads Url Params Data |
316 | 292 | 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 | + ); |
336 | 310 | } |
337 | 311 | } |
338 | | - }, [isLoaded]); |
339 | | - //Updating Url Params when values of filter changes |
| 312 | + initializedRef.current = true; |
| 313 | + }, []); |
| 314 | + |
340 | 315 | 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 | + ); |
347 | 334 | } |
348 | 335 | }, [value]); |
349 | 336 | }; |
|
0 commit comments