From 269aab71df47616236d66878a07ba941967d0419 Mon Sep 17 00:00:00 2001 From: Arun Jain Date: Fri, 22 Nov 2024 11:37:35 +0530 Subject: [PATCH] fix: updating params twice in local filters --- package-lock.json | 4 +-- package.json | 2 +- .../Hooks/useUrlFilters/useUrlFilters.ts | 32 +++++++++++++++++-- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index c9b1e0ca2..5cbd76a3e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "0.6.5-patch-1", + "version": "0.6.5-patch-2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "0.6.5-patch-1", + "version": "0.6.5-patch-2", "license": "ISC", "dependencies": { "@types/react-dates": "^21.8.6", diff --git a/package.json b/package.json index 29ec6e9d1..056e17824 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "0.6.5-patch-1", + "version": "0.6.5-patch-2", "description": "Supporting common component library", "type": "module", "main": "dist/index.js", diff --git a/src/Common/Hooks/useUrlFilters/useUrlFilters.ts b/src/Common/Hooks/useUrlFilters/useUrlFilters.ts index 6abb0a39e..9fe124f3f 100644 --- a/src/Common/Hooks/useUrlFilters/useUrlFilters.ts +++ b/src/Common/Hooks/useUrlFilters/useUrlFilters.ts @@ -14,8 +14,9 @@ * limitations under the License. */ -import { useEffect, useMemo } from 'react' +import { useEffect, useMemo, useRef } from 'react' import { useHistory, useLocation } from 'react-router-dom' +import { getUrlWithSearchParams } from '@Common/Helper' import { DEFAULT_BASE_PAGE_SIZE, EXCLUDED_FALSY_VALUES, SortingOrder } from '../../Constants' import { DEFAULT_PAGE_NUMBER, URL_FILTER_KEYS } from './constants' import { UseUrlFiltersProps, UseUrlFiltersReturnType } from './types' @@ -47,7 +48,30 @@ const useUrlFilters = ({ }: UseUrlFiltersProps = {}): UseUrlFiltersReturnType => { const location = useLocation() const history = useHistory() - const searchParams = new URLSearchParams(location.search) + + const isAlreadyReadFromLocalStorage = useRef(false) + + const getSearchParams = () => { + if (!isAlreadyReadFromLocalStorage.current && !location.search && localStorageKey) { + isAlreadyReadFromLocalStorage.current = true + const localStorageValue = localStorage.getItem(localStorageKey) + if (localStorageValue) { + try { + const localSearchString = getUrlWithSearchParams('', JSON.parse(localStorageValue)) + const localSearchParams = new URLSearchParams(localSearchString.split('?')[1] ?? '') + + history.replace({ search: localSearchParams.toString() }) + return localSearchParams + } catch { + localStorage.removeItem(localStorageKey) + } + } + } + + return new URLSearchParams(location.search) + } + + const searchParams = getSearchParams() const getParsedSearchParams: UseUrlFiltersProps['parseSearchParams'] = (searchParamsToParse) => { if (parseSearchParams) { @@ -136,7 +160,9 @@ const useUrlFilters = ({ const clearFilters = () => { history.replace({ search: '' }) - setItemInLocalStorageIfKeyExists(localStorageKey, '') + if (localStorageKey) { + localStorage.removeItem(localStorageKey) + } } const updateSearchParams = (paramsToSerialize: Partial) => {