Skip to content

Commit 079cf1c

Browse files
authored
Merge pull request #419 from devtron-labs/fix/local-storage-filters
fix: local storage filters updating twice
2 parents 3a0609c + 002678e commit 079cf1c

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
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": "1.1.0",
3+
"version": "1.1.0-patch-1",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Hooks/useUrlFilters/useUrlFilters.ts

Lines changed: 37 additions & 18 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 { 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,38 @@ 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+
const locationSearchParams = new URLSearchParams(location.search)
56+
if (!isAlreadyReadFromLocalStorage.current && localStorageKey) {
57+
if (!location.search) {
58+
isAlreadyReadFromLocalStorage.current = true
59+
const localStorageValue = localStorage.getItem(localStorageKey)
60+
if (localStorageValue) {
61+
try {
62+
const localSearchString = getUrlWithSearchParams('', JSON.parse(localStorageValue))
63+
const localSearchParams = new URLSearchParams(localSearchString.split('?')[1] ?? '')
64+
65+
history.replace({ search: localSearchParams.toString() })
66+
return localSearchParams
67+
} catch {
68+
localStorage.removeItem(localStorageKey)
69+
}
70+
}
71+
} else {
72+
setItemInLocalStorageIfKeyExists(
73+
localStorageKey,
74+
JSON.stringify(parseSearchParams(locationSearchParams)),
75+
)
76+
}
77+
}
78+
79+
return locationSearchParams
80+
}
81+
82+
const searchParams = getSearchParams()
5183

5284
const getParsedSearchParams: UseUrlFiltersProps<T, K>['parseSearchParams'] = (searchParamsToParse) => {
5385
if (parseSearchParams) {
@@ -136,7 +168,9 @@ const useUrlFilters = <T = string, K = unknown>({
136168

137169
const clearFilters = () => {
138170
history.replace({ search: '' })
139-
setItemInLocalStorageIfKeyExists(localStorageKey, '')
171+
if (localStorageKey) {
172+
localStorage.removeItem(localStorageKey)
173+
}
140174
}
141175

142176
const updateSearchParams = (paramsToSerialize: Partial<K>) => {
@@ -160,21 +194,6 @@ const useUrlFilters = <T = string, K = unknown>({
160194
_resetPageNumber()
161195
}
162196

163-
useEffect(() => {
164-
if (!localStorageKey) {
165-
return
166-
}
167-
// if we have search string, set secondary params in local storage accordingly
168-
if (location.search) {
169-
setItemInLocalStorageIfKeyExists(localStorageKey, JSON.stringify(parsedParams))
170-
return
171-
}
172-
const localStorageValue = localStorage.getItem(localStorageKey)
173-
if (localStorageValue) {
174-
updateSearchParams(JSON.parse(localStorageValue))
175-
}
176-
}, [])
177-
178197
return {
179198
pageSize,
180199
changePage,

0 commit comments

Comments
 (0)