@@ -35,9 +35,10 @@ import {
35
35
import {
36
36
getMarketplaceListCondition ,
37
37
getMarketplaceListFilterType ,
38
+ updateSearchParams ,
38
39
} from './utils'
39
40
import { useInstalledPluginList } from '@/service/use-plugins'
40
- import { noop } from 'lodash-es'
41
+ import { debounce , noop } from 'lodash-es'
41
42
42
43
export type MarketplaceContextValue = {
43
44
intersected : boolean
@@ -96,6 +97,7 @@ type MarketplaceContextProviderProps = {
96
97
searchParams ?: SearchParams
97
98
shouldExclude ?: boolean
98
99
scrollContainerId ?: string
100
+ showSearchParams ?: boolean
99
101
}
100
102
101
103
export function useMarketplaceContext ( selector : ( value : MarketplaceContextValue ) => any ) {
@@ -107,6 +109,7 @@ export const MarketplaceContextProvider = ({
107
109
searchParams,
108
110
shouldExclude,
109
111
scrollContainerId,
112
+ showSearchParams,
110
113
} : MarketplaceContextProviderProps ) => {
111
114
const { data, isSuccess } = useInstalledPluginList ( ! shouldExclude )
112
115
const exclude = useMemo ( ( ) => {
@@ -159,7 +162,10 @@ export const MarketplaceContextProvider = ({
159
162
type : getMarketplaceListFilterType ( activePluginTypeRef . current ) ,
160
163
page : pageRef . current ,
161
164
} )
162
- history . pushState ( { } , '' , `/${ searchParams ?. language ? `?language=${ searchParams ?. language } ` : '' } ` )
165
+ const url = new URL ( window . location . href )
166
+ if ( searchParams ?. language )
167
+ url . searchParams . set ( 'language' , searchParams ?. language )
168
+ history . replaceState ( { } , '' , url )
163
169
}
164
170
else {
165
171
if ( shouldExclude && isSuccess ) {
@@ -182,7 +188,31 @@ export const MarketplaceContextProvider = ({
182
188
resetPlugins ( )
183
189
} , [ exclude , queryMarketplaceCollectionsAndPlugins , resetPlugins ] )
184
190
191
+ const debouncedUpdateSearchParams = useMemo ( ( ) => debounce ( ( ) => {
192
+ updateSearchParams ( {
193
+ query : searchPluginTextRef . current ,
194
+ category : activePluginTypeRef . current ,
195
+ tags : filterPluginTagsRef . current ,
196
+ } )
197
+ } , 500 ) , [ ] )
198
+
199
+ const handleUpdateSearchParams = useCallback ( ( debounced ?: boolean ) => {
200
+ if ( ! showSearchParams )
201
+ return
202
+ if ( debounced ) {
203
+ debouncedUpdateSearchParams ( )
204
+ }
205
+ else {
206
+ updateSearchParams ( {
207
+ query : searchPluginTextRef . current ,
208
+ category : activePluginTypeRef . current ,
209
+ tags : filterPluginTagsRef . current ,
210
+ } )
211
+ }
212
+ } , [ debouncedUpdateSearchParams , showSearchParams ] )
213
+
185
214
const handleQueryPlugins = useCallback ( ( debounced ?: boolean ) => {
215
+ handleUpdateSearchParams ( debounced )
186
216
if ( debounced ) {
187
217
queryPluginsWithDebounced ( {
188
218
query : searchPluginTextRef . current ,
@@ -207,17 +237,18 @@ export const MarketplaceContextProvider = ({
207
237
page : pageRef . current ,
208
238
} )
209
239
}
210
- } , [ exclude , queryPluginsWithDebounced , queryPlugins ] )
240
+ } , [ exclude , queryPluginsWithDebounced , queryPlugins , handleUpdateSearchParams ] )
211
241
212
242
const handleQuery = useCallback ( ( debounced ?: boolean ) => {
213
243
if ( ! searchPluginTextRef . current && ! filterPluginTagsRef . current . length ) {
244
+ handleUpdateSearchParams ( debounced )
214
245
cancelQueryPluginsWithDebounced ( )
215
246
handleQueryMarketplaceCollectionsAndPlugins ( )
216
247
return
217
248
}
218
249
219
250
handleQueryPlugins ( debounced )
220
- } , [ handleQueryMarketplaceCollectionsAndPlugins , handleQueryPlugins , cancelQueryPluginsWithDebounced ] )
251
+ } , [ handleQueryMarketplaceCollectionsAndPlugins , handleQueryPlugins , cancelQueryPluginsWithDebounced , handleUpdateSearchParams ] )
221
252
222
253
const handleSearchPluginTextChange = useCallback ( ( text : string ) => {
223
254
setSearchPluginText ( text )
@@ -242,11 +273,9 @@ export const MarketplaceContextProvider = ({
242
273
activePluginTypeRef . current = type
243
274
setPage ( 1 )
244
275
pageRef . current = 1
245
- } , [ ] )
246
276
247
- useEffect ( ( ) => {
248
277
handleQuery ( )
249
- } , [ activePluginType , handleQuery ] )
278
+ } , [ handleQuery ] )
250
279
251
280
const handleSortChange = useCallback ( ( sort : PluginsSort ) => {
252
281
setSort ( sort )
0 commit comments