1
1
'use client' ;
2
2
3
- import { FC , ReactNode , useRef } from 'react' ;
4
- import debounce from 'lodash.debounce' ;
5
- import { StringParam , useQueryParam , withDefault } from 'use-query-params' ;
3
+ import { FC , ReactNode , useCallback } from 'react' ;
4
+ import { ReadonlyURLSearchParams , usePathname , useRouter , useSearchParams } from 'next/navigation' ;
6
5
import { ConfigName , configs , rules } from '@graphql-eslint/eslint-plugin' ;
7
6
import { asArray } from '@graphql-tools/utils' ;
8
7
import { GraphQLEditor } from './graphql-editor' ;
@@ -30,14 +29,28 @@ const operationsRulesOptions = Object.entries(rules)
30
29
const schemaConfigsOptions = schemaConfigs . map ( name => ( { key : name , name } ) ) ;
31
30
const operationsConfigsOptions = operationsConfigs . map ( name => ( { key : name , name } ) ) ;
32
31
33
- function useDebouncedQueryParams < TypeToEncode , TypeFromDecode = TypeToEncode > (
34
- ...args : Parameters < typeof useQueryParam < TypeToEncode , TypeFromDecode > >
35
- ) : ReturnType < typeof useQueryParam < TypeToEncode , TypeFromDecode > > {
36
- const [ query , setQuery ] = useQueryParam ( ...args ) ;
37
- const fn = useRef < typeof setQuery > ( ) ;
38
- fn . current ||= debounce ( setQuery , 500 ) ;
32
+ // Get a new searchParams string by merging the current
33
+ // searchParams with a provided key/value pair
34
+ const createQueryString = ( searchParams : ReadonlyURLSearchParams , name : string , value : string ) => {
35
+ const params = new URLSearchParams ( searchParams . toString ( ) ) ;
36
+ params . set ( name , value ) ;
39
37
40
- return [ query , fn . current ] ;
38
+ return '?' + params . toString ( ) ;
39
+ } ;
40
+
41
+ function useSetterSearchParams (
42
+ paramKey : string ,
43
+ defaultValue = '' ,
44
+ ) : [ string , ( value : string ) => void ] {
45
+ const pathname = usePathname ( ) ;
46
+ const router = useRouter ( ) ;
47
+ const searchParams = useSearchParams ( ) ;
48
+
49
+ const handleChange = useCallback ( ( value : string ) => {
50
+ router . push ( pathname + createQueryString ( searchParams , paramKey , value ) ) ;
51
+ } , [ ] ) ;
52
+
53
+ return [ searchParams . get ( paramKey ) ?? defaultValue , handleChange ] ;
41
54
}
42
55
43
56
export const ClientPage : FC < {
@@ -46,21 +59,15 @@ export const ClientPage: FC<{
46
59
children : ReactNode ;
47
60
headingClass : string ;
48
61
} > = ( { defaultSchema, defaultOperation, children, headingClass } ) => {
49
- const [ schemaConfig , setSchemaConfig ] = useDebouncedQueryParams (
50
- 'sc' ,
51
- withDefault ( StringParam , 'schema-recommended' ) ,
52
- ) ;
53
- const [ schemaRule , setSchemaRule ] = useDebouncedQueryParams < string > ( 'sr' ) ;
54
- const [ operationConfig , setOperationConfig ] = useDebouncedQueryParams (
62
+ const [ schemaConfig , setSchemaConfig ] = useSetterSearchParams ( 'sc' , 'schema-recommended' ) ;
63
+ const [ schemaRule , setSchemaRule ] = useSetterSearchParams ( 'sr' ) ;
64
+ const [ operationConfig , setOperationConfig ] = useSetterSearchParams (
55
65
'oc' ,
56
- withDefault ( StringParam , 'operations-recommended' ) ,
57
- ) ;
58
- const [ operationRule , setOperationRule ] = useDebouncedQueryParams < string > ( 'or' ) ;
59
- const [ schema , setSchema ] = useDebouncedQueryParams ( 's' , withDefault ( StringParam , defaultSchema ) ) ;
60
- const [ operation , setOperation ] = useDebouncedQueryParams (
61
- 'o' ,
62
- withDefault ( StringParam , defaultOperation ) ,
66
+ 'operations-recommended' ,
63
67
) ;
68
+ const [ operationRule , setOperationRule ] = useSetterSearchParams ( 'or' ) ;
69
+ const [ schema , setSchema ] = useSetterSearchParams ( 's' , defaultSchema ) ;
70
+ const [ operation , setOperation ] = useSetterSearchParams ( 'o' , defaultOperation ) ;
64
71
65
72
return (
66
73
< >
0 commit comments