1- import { useCallback , useMemo } from 'react' ;
2- import { useSearchParams } from 'react-router' ;
3- import { QueryFilters } from "../types" ;
1+ import { useCallback , useEffect , useState } from 'react' ;
2+ import { useSearchParams } from 'react-router' ;
3+ import { QueryFilters } from "../types" ;
44
5- type FilterCondition = {
6- operator : string ;
7- value : string | string [ ] ;
8- } ;
9-
10- const debounce = < T extends ( ...args : any [ ] ) => void > ( func : T , delay : number ) => {
5+ const debounce = ( func : Function , delay : number ) => {
116 let timerId : ReturnType < typeof setTimeout > ;
12- return ( ...args : Parameters < T > ) => {
7+ return ( ...args : any [ ] ) => {
138 if ( timerId ) clearTimeout ( timerId ) ;
149 timerId = setTimeout ( ( ) => func ( ...args ) , delay ) ;
1510 } ;
@@ -20,31 +15,32 @@ export const useFilterQueryParamSync = (): [
2015 ( updates : Partial < QueryFilters > , replace ?: boolean ) => void
2116] => {
2217 const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
18+ const [ queryParams , setQueryParams ] = useState < Partial < QueryFilters > > ( { } ) ;
2319
24- const queryParams = useMemo ( ( ) => {
25- const parsedParams : Partial < QueryFilters > & Record < string , any > = { } ;
20+ useEffect ( ( ) => {
21+ const parsedParams : Partial < QueryFilters > = { } ;
2622
2723 searchParams . forEach ( ( value , key ) => {
2824 if ( key . startsWith ( 'filterFields[' ) ) {
2925 const match = key . match ( / ^ f i l t e r F i e l d s \[ ( .+ ) \] \[ ( .+ ) \] $ / ) ;
3026 if ( match ) {
31- const [ , fieldName , operator ] = match ;
32-
27+ const [ _ , fieldName , operator ] = match ;
3328 if ( ! parsedParams . filterFields ) {
3429 parsedParams . filterFields = { } ;
3530 }
36-
37- ( parsedParams . filterFields as Record < string , FilterCondition > ) [ fieldName ] = {
31+ // @ts -ignore
32+ parsedParams . filterFields [ fieldName ] = {
3833 operator,
3934 value : value . includes ( ',' ) ? value . split ( ',' ) : value
4035 } ;
4136 }
4237 } else {
38+ // @ts -ignore - Handle non-filterFields params
4339 parsedParams [ key ] = value ;
4440 }
4541 } ) ;
4642
47- return parsedParams ;
43+ setQueryParams ( parsedParams ) ;
4844 } , [ searchParams ] ) ;
4945
5046 const debouncedSetSearchParams = useCallback (
@@ -56,22 +52,22 @@ export const useFilterQueryParamSync = (): [
5652
5753 const updateSearchParams = useCallback (
5854 ( updates : Partial < QueryFilters > , replace : boolean = false ) => {
59- // Create new params based on current URL state
6055 const newParams = replace ? new URLSearchParams ( ) : new URLSearchParams ( searchParams ) ;
6156
57+ // Clear existing filter fields if replacing
6258 if ( replace ) {
63- // Clean up existing filter fields
64- Array . from ( newParams . keys ( ) ) . forEach ( ( key ) => {
59+ searchParams . forEach ( ( _ , key ) => {
6560 if ( key . startsWith ( 'filterFields[' ) ) {
6661 newParams . delete ( key ) ;
6762 }
6863 } ) ;
6964 }
7065
66+ // Update params
7167 Object . entries ( updates ) . forEach ( ( [ key , value ] ) => {
7268 if ( key === 'filterFields' && value ) {
7369 Object . entries ( value ) . forEach ( ( [ field , condition ] ) => {
74- if ( condition && typeof condition === 'object' && 'operator' in condition ) {
70+ if ( condition ) {
7571 const paramKey = `filterFields[${ field } ][${ condition . operator } ]` ;
7672 if ( Array . isArray ( condition . value ) ) {
7773 newParams . set ( paramKey , condition . value . join ( ',' ) ) ;
@@ -80,7 +76,7 @@ export const useFilterQueryParamSync = (): [
8076 }
8177 }
8278 } ) ;
83- } else if ( value !== undefined && value !== null ) {
79+ } else if ( value !== undefined ) {
8480 newParams . set ( key , String ( value ) ) ;
8581 }
8682 } ) ;
0 commit comments