14
14
* limitations under the License.
15
15
*/
16
16
17
- import { useEffect , useMemo } from 'react'
17
+ import { useMemo , useRef } from 'react'
18
18
import { useHistory , useLocation } from 'react-router-dom'
19
+ import { getUrlWithSearchParams } from '@Common/Helper'
19
20
import { DEFAULT_BASE_PAGE_SIZE , EXCLUDED_FALSY_VALUES , SortingOrder } from '../../Constants'
20
21
import { DEFAULT_PAGE_NUMBER , URL_FILTER_KEYS } from './constants'
21
22
import { UseUrlFiltersProps , UseUrlFiltersReturnType } from './types'
@@ -47,7 +48,38 @@ const useUrlFilters = <T = string, K = unknown>({
47
48
} : UseUrlFiltersProps < T , K > = { } ) : UseUrlFiltersReturnType < T , K > => {
48
49
const location = useLocation ( )
49
50
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 ( )
51
83
52
84
const getParsedSearchParams : UseUrlFiltersProps < T , K > [ 'parseSearchParams' ] = ( searchParamsToParse ) => {
53
85
if ( parseSearchParams ) {
@@ -136,7 +168,9 @@ const useUrlFilters = <T = string, K = unknown>({
136
168
137
169
const clearFilters = ( ) => {
138
170
history . replace ( { search : '' } )
139
- setItemInLocalStorageIfKeyExists ( localStorageKey , '' )
171
+ if ( localStorageKey ) {
172
+ localStorage . removeItem ( localStorageKey )
173
+ }
140
174
}
141
175
142
176
const updateSearchParams = ( paramsToSerialize : Partial < K > ) => {
@@ -160,21 +194,6 @@ const useUrlFilters = <T = string, K = unknown>({
160
194
_resetPageNumber ( )
161
195
}
162
196
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
-
178
197
return {
179
198
pageSize,
180
199
changePage,
0 commit comments