Skip to content

Commit 3826c8e

Browse files
authored
Merge pull request #418 from devtron-labs/fix/local-url-filters
fix: updating params twice in local filters
2 parents 8d6bdb3 + 9baecbf commit 3826c8e

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
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: 37 additions & 15 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,18 +194,6 @@ const useUrlFilters = <T = string, K = unknown>({
160194
_resetPageNumber()
161195
}
162196

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

0 commit comments

Comments
 (0)