Skip to content

Commit 269aab7

Browse files
committed
fix: updating params twice in local filters
1 parent 8d6bdb3 commit 269aab7

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.6.5-patch-1",
3+
"version": "0.6.5-patch-2",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Hooks/useUrlFilters/useUrlFilters.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useEffect, useMemo } from 'react'
17+
import { useEffect, useMemo, useRef } from 'react'
1818
import { useHistory, useLocation } from 'react-router-dom'
19+
import { getUrlWithSearchParams } from '@Common/Helper'
1920
import { DEFAULT_BASE_PAGE_SIZE, EXCLUDED_FALSY_VALUES, SortingOrder } from '../../Constants'
2021
import { DEFAULT_PAGE_NUMBER, URL_FILTER_KEYS } from './constants'
2122
import { UseUrlFiltersProps, UseUrlFiltersReturnType } from './types'
@@ -47,7 +48,30 @@ const useUrlFilters = <T = string, K = unknown>({
4748
}: UseUrlFiltersProps<T, K> = {}): UseUrlFiltersReturnType<T, K> => {
4849
const location = useLocation()
4950
const history = useHistory()
50-
const searchParams = new URLSearchParams(location.search)
51+
52+
const isAlreadyReadFromLocalStorage = useRef<boolean>(false)
53+
54+
const getSearchParams = () => {
55+
if (!isAlreadyReadFromLocalStorage.current && !location.search && localStorageKey) {
56+
isAlreadyReadFromLocalStorage.current = true
57+
const localStorageValue = localStorage.getItem(localStorageKey)
58+
if (localStorageValue) {
59+
try {
60+
const localSearchString = getUrlWithSearchParams('', JSON.parse(localStorageValue))
61+
const localSearchParams = new URLSearchParams(localSearchString.split('?')[1] ?? '')
62+
63+
history.replace({ search: localSearchParams.toString() })
64+
return localSearchParams
65+
} catch {
66+
localStorage.removeItem(localStorageKey)
67+
}
68+
}
69+
}
70+
71+
return new URLSearchParams(location.search)
72+
}
73+
74+
const searchParams = getSearchParams()
5175

5276
const getParsedSearchParams: UseUrlFiltersProps<T, K>['parseSearchParams'] = (searchParamsToParse) => {
5377
if (parseSearchParams) {
@@ -136,7 +160,9 @@ const useUrlFilters = <T = string, K = unknown>({
136160

137161
const clearFilters = () => {
138162
history.replace({ search: '' })
139-
setItemInLocalStorageIfKeyExists(localStorageKey, '')
163+
if (localStorageKey) {
164+
localStorage.removeItem(localStorageKey)
165+
}
140166
}
141167

142168
const updateSearchParams = (paramsToSerialize: Partial<K>) => {

0 commit comments

Comments
 (0)