11'use client' ;
22
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' ;
65import { ConfigName , configs , rules } from '@graphql-eslint/eslint-plugin' ;
76import { asArray } from '@graphql-tools/utils' ;
87import { GraphQLEditor } from './graphql-editor' ;
@@ -30,14 +29,28 @@ const operationsRulesOptions = Object.entries(rules)
3029const schemaConfigsOptions = schemaConfigs . map ( name => ( { key : name , name } ) ) ;
3130const operationsConfigsOptions = operationsConfigs . map ( name => ( { key : name , name } ) ) ;
3231
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 ) ;
3937
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 ] ;
4154}
4255
4356export const ClientPage : FC < {
@@ -46,21 +59,15 @@ export const ClientPage: FC<{
4659 children : ReactNode ;
4760 headingClass : string ;
4861} > = ( { 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 (
5565 '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' ,
6367 ) ;
68+ const [ operationRule , setOperationRule ] = useSetterSearchParams ( 'or' ) ;
69+ const [ schema , setSchema ] = useSetterSearchParams ( 's' , defaultSchema ) ;
70+ const [ operation , setOperation ] = useSetterSearchParams ( 'o' , defaultOperation ) ;
6471
6572 return (
6673 < >
0 commit comments