From 269aab71df47616236d66878a07ba941967d0419 Mon Sep 17 00:00:00 2001 From: Arun Jain Date: Fri, 22 Nov 2024 11:37:35 +0530 Subject: [PATCH 1/5] 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) => { From d5411ee2fb54186b97ef2dcb6f2b0d98c1e31b21 Mon Sep 17 00:00:00 2001 From: Arun Jain Date: Fri, 22 Nov 2024 11:39:25 +0530 Subject: [PATCH 2/5] chore: remove use effect --- src/Common/Hooks/useUrlFilters/useUrlFilters.ts | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/Common/Hooks/useUrlFilters/useUrlFilters.ts b/src/Common/Hooks/useUrlFilters/useUrlFilters.ts index 9fe124f3f..0d42e586e 100644 --- a/src/Common/Hooks/useUrlFilters/useUrlFilters.ts +++ b/src/Common/Hooks/useUrlFilters/useUrlFilters.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { useEffect, useMemo, useRef } from 'react' +import { 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' @@ -186,18 +186,6 @@ const useUrlFilters = ({ _resetPageNumber() } - useEffect(() => { - // if we have search string, set secondary params in local storage accordingly - if (location.search) { - localStorage.setItem(localStorageKey, JSON.stringify(parsedParams)) - return - } - const localStorageValue = localStorage.getItem(localStorageKey) - if (localStorageValue) { - updateSearchParams(JSON.parse(localStorageValue)) - } - }, []) - return { pageSize, changePage, From ad0b691448666b258e82d7e582afe2143355139f Mon Sep 17 00:00:00 2001 From: Arun Jain Date: Fri, 22 Nov 2024 11:49:19 +0530 Subject: [PATCH 3/5] chore: version bump --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5cbd76a3e..79ffd90e3 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-2", + "version": "0.6.5-patch-1-beta-1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "0.6.5-patch-2", + "version": "0.6.5-patch-1-beta-1", "license": "ISC", "dependencies": { "@types/react-dates": "^21.8.6", diff --git a/package.json b/package.json index 056e17824..c73815fbf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "0.6.5-patch-2", + "version": "0.6.5-patch-1-beta-1", "description": "Supporting common component library", "type": "module", "main": "dist/index.js", From 8a394aa891e120fd8214613885b9d061e03e1618 Mon Sep 17 00:00:00 2001 From: Arun Jain Date: Fri, 22 Nov 2024 12:51:07 +0530 Subject: [PATCH 4/5] feat: save filters from url --- package-lock.json | 4 +-- package.json | 2 +- .../Hooks/useUrlFilters/useUrlFilters.ts | 34 ++++++++++++------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 79ffd90e3..56d940c9b 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-beta-1", + "version": "0.6.5-patch-1-beta-2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "0.6.5-patch-1-beta-1", + "version": "0.6.5-patch-1-beta-2", "license": "ISC", "dependencies": { "@types/react-dates": "^21.8.6", diff --git a/package.json b/package.json index c73815fbf..76b72ce0b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "0.6.5-patch-1-beta-1", + "version": "0.6.5-patch-1-beta-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 0d42e586e..f6b66aebb 100644 --- a/src/Common/Hooks/useUrlFilters/useUrlFilters.ts +++ b/src/Common/Hooks/useUrlFilters/useUrlFilters.ts @@ -52,23 +52,31 @@ const useUrlFilters = ({ 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) + const locationSearchParams = new URLSearchParams(location.search) + if (!isAlreadyReadFromLocalStorage.current && localStorageKey) { + if (!location.search) { + 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) + } } + } else { + setItemInLocalStorageIfKeyExists( + localStorageKey, + JSON.stringify(parseSearchParams(locationSearchParams)), + ) } } - return new URLSearchParams(location.search) + return locationSearchParams } const searchParams = getSearchParams() From 9baecbf663c12f0eba2ff03e51dd3526bc284e59 Mon Sep 17 00:00:00 2001 From: Arun Jain Date: Fri, 22 Nov 2024 12:55:11 +0530 Subject: [PATCH 5/5] chore: version bump --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 56d940c9b..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-beta-2", + "version": "0.6.5-patch-2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "0.6.5-patch-1-beta-2", + "version": "0.6.5-patch-2", "license": "ISC", "dependencies": { "@types/react-dates": "^21.8.6", diff --git a/package.json b/package.json index 76b72ce0b..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-beta-2", + "version": "0.6.5-patch-2", "description": "Supporting common component library", "type": "module", "main": "dist/index.js",