@@ -14,7 +14,7 @@ import {
14
14
Text ,
15
15
Tray ,
16
16
} from 'opub-ui' ;
17
- import React , { useEffect , useReducer , useRef , useState } from 'react' ;
17
+ import React , { useEffect , useMemo , useReducer , useRef , useState } from 'react' ;
18
18
19
19
import BreadCrumbs from '@/components/BreadCrumbs' ;
20
20
import { Icons } from '@/components/icons' ;
@@ -149,7 +149,8 @@ const queryReducer = (state: QueryParams, action: Action): QueryParams => {
149
149
const useUrlParams = (
150
150
queryParams : QueryParams ,
151
151
setQueryParams : React . Dispatch < Action > ,
152
- setVariables : ( vars : string ) => void
152
+ setVariables : ( vars : string ) => void ,
153
+ lockedFilters : Record < string , string [ ] >
153
154
) => {
154
155
const router = useRouter ( ) ;
155
156
@@ -165,6 +166,13 @@ const useUrlParams = (
165
166
}
166
167
} ) ;
167
168
169
+ // Merge locked filters with URL filters
170
+ Object . entries ( lockedFilters ) . forEach ( ( [ category , values ] ) => {
171
+ if ( values . length > 0 ) {
172
+ filters [ category ] = Array . from ( new Set ( [ ...( filters [ category ] || [ ] ) , ...values ] ) ) ;
173
+ }
174
+ } ) ;
175
+
168
176
const initialParams : QueryParams = {
169
177
pageSize : sizeParam ? Number ( sizeParam ) : 9 ,
170
178
currentPage : pageParam ? Number ( pageParam ) : 1 ,
@@ -173,7 +181,7 @@ const useUrlParams = (
173
181
} ;
174
182
175
183
setQueryParams ( { type : 'INITIALIZE' , payload : initialParams } ) ;
176
- } , [ setQueryParams ] ) ;
184
+ } , [ setQueryParams , lockedFilters ] ) ;
177
185
178
186
useEffect ( ( ) => {
179
187
const filtersString = Object . entries ( queryParams . filters )
@@ -234,6 +242,7 @@ interface ListingProps {
234
242
categoryImage ?: string ;
235
243
placeholder : string ;
236
244
redirectionURL : string ;
245
+ lockedFilters ?: Record < string , string [ ] > ;
237
246
}
238
247
239
248
const ListingComponent : React . FC < ListingProps > = ( {
@@ -245,6 +254,7 @@ const ListingComponent: React.FC<ListingProps> = ({
245
254
categoryImage,
246
255
placeholder,
247
256
redirectionURL,
257
+ lockedFilters = { } ,
248
258
} ) => {
249
259
const [ facets , setFacets ] = useState < {
250
260
results : any [ ] ;
@@ -259,7 +269,10 @@ const ListingComponent: React.FC<ListingProps> = ({
259
269
const count = facets ?. total ?? 0 ;
260
270
const datasetDetails = facets ?. results ?? [ ] ;
261
271
262
- useUrlParams ( queryParams , setQueryParams , setVariables ) ;
272
+ // Stabilize lockedFilters reference to prevent infinite loops
273
+ const stableLockedFilters = useMemo ( ( ) => lockedFilters , [ JSON . stringify ( lockedFilters ) ] ) ;
274
+
275
+ useUrlParams ( queryParams , setQueryParams , setVariables , stableLockedFilters ) ;
263
276
const latestFetchId = useRef ( 0 ) ;
264
277
265
278
useEffect ( ( ) => {
@@ -389,6 +402,7 @@ const ListingComponent: React.FC<ListingProps> = ({
389
402
options = { filterOptions }
390
403
setSelectedOptions = { handleFilterChange }
391
404
selectedOptions = { queryParams . filters }
405
+ lockedFilters = { stableLockedFilters }
392
406
/>
393
407
</ div >
394
408
@@ -484,6 +498,7 @@ const ListingComponent: React.FC<ListingProps> = ({
484
498
options = { filterOptions }
485
499
setSelectedOptions = { handleFilterChange }
486
500
selectedOptions = { queryParams . filters }
501
+ lockedFilters = { stableLockedFilters }
487
502
/>
488
503
</ Tray >
489
504
</ div >
@@ -497,14 +512,18 @@ const ListingComponent: React.FC<ListingProps> = ({
497
512
( [ category , values ] ) =>
498
513
values
499
514
. filter ( ( value ) => category !== 'sort' )
500
- . map ( ( value ) => (
501
- < Pill
502
- key = { `${ category } -${ value } ` }
503
- onRemove = { ( ) => handleRemoveFilter ( category , value ) }
504
- >
505
- { value }
506
- </ Pill >
507
- ) )
515
+ . map ( ( value ) => {
516
+ // Check if this filter value is locked
517
+ const isLocked = stableLockedFilters [ category ] ?. includes ( value ) ;
518
+ return (
519
+ < Pill
520
+ key = { `${ category } -${ value } ` }
521
+ onRemove = { isLocked ? undefined : ( ) => handleRemoveFilter ( category , value ) }
522
+ >
523
+ { value }
524
+ </ Pill >
525
+ ) ;
526
+ } )
508
527
) }
509
528
</ div >
510
529
) }
0 commit comments