Skip to content

Commit 8a394aa

Browse files
committed
feat: save filters from url
1 parent ad0b691 commit 8a394aa

File tree

3 files changed

+24
-16
lines changed

3 files changed

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

src/Common/Hooks/useUrlFilters/useUrlFilters.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,31 @@ const useUrlFilters = <T = string, K = unknown>({
5252
const isAlreadyReadFromLocalStorage = useRef<boolean>(false)
5353

5454
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)
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+
}
6770
}
71+
} else {
72+
setItemInLocalStorageIfKeyExists(
73+
localStorageKey,
74+
JSON.stringify(parseSearchParams(locationSearchParams)),
75+
)
6876
}
6977
}
7078

71-
return new URLSearchParams(location.search)
79+
return locationSearchParams
7280
}
7381

7482
const searchParams = getSearchParams()

0 commit comments

Comments
 (0)