2
2
// The `queryParam` variable returned from this method are stateful and will be set to the query param on page load
3
3
4
4
import { useRouter } from 'next/router'
5
- import { useEffect , useState } from 'react'
5
+ import { useState , useEffect } from 'react'
6
6
import { parseDebug } from '@/search/components/hooks/useQuery'
7
7
8
8
type UseQueryParamReturn < T extends string | boolean > = {
@@ -23,69 +23,48 @@ export function useQueryParam(
23
23
) : UseQueryParamReturn < any > {
24
24
const router = useRouter ( )
25
25
26
- // Determine the initial value of the query param
27
- let initialQueryParam = ''
28
- const paramValue = router . query [ queryParamKey ]
29
-
30
- if ( paramValue ) {
31
- if ( Array . isArray ( paramValue ) ) {
32
- initialQueryParam = paramValue [ 0 ]
33
- } else {
34
- initialQueryParam = paramValue
35
- }
36
- }
37
-
38
- const debugValue = parseDebug ( router . query . debug )
39
-
40
- // Return type will be set based on overloads
41
- const [ queryParamString , setQueryParamState ] = useState < string > ( initialQueryParam )
42
- const [ debug ] = useState < boolean > ( debugValue )
26
+ const [ queryParamString , setQueryParamState ] = useState < string > ( '' )
27
+ const [ debug , setDebug ] = useState < boolean > ( false )
28
+ const queryParam : string | boolean = isBoolean ? queryParamString === 'true' : queryParamString
43
29
44
- // If the query param changes in the URL, update the state
30
+ // Only set the initial query param values on page load, the rest of the time we use React state
45
31
useEffect ( ( ) => {
32
+ let initialQueryParam = ''
46
33
const paramValue = router . query [ queryParamKey ]
47
-
48
34
if ( paramValue ) {
49
- if ( Array . isArray ( paramValue ) ) {
50
- setQueryParamState ( paramValue [ 0 ] )
51
- } else {
52
- setQueryParamState ( paramValue )
53
- }
35
+ initialQueryParam = Array . isArray ( paramValue ) ? paramValue [ 0 ] : paramValue
54
36
}
55
- } , [ router . query , queryParamKey ] )
56
-
57
- // Determine the type of queryParam based on isBoolean
58
- const queryParam : string | boolean = isBoolean ? queryParamString === 'true' : queryParamString
37
+ setQueryParamState ( initialQueryParam )
38
+ setDebug ( parseDebug ( router . query . debug || '' ) || false )
39
+ } , [ queryParamKey , router . pathname ] )
59
40
60
41
const setQueryParam = ( value : string | boolean ) => {
61
- const { pathname, hash, search } = window . location
62
-
63
- let newValue : string = value as string
64
-
65
- // If it's a false boolean or empty string, just remove the query param
66
- if ( ! value ) {
67
- newValue = ''
68
- } else if ( typeof value === 'boolean' ) {
69
- newValue = 'true'
70
- }
71
-
72
- const params = new URLSearchParams ( search )
42
+ const newValue = typeof value === 'boolean' ? ( value ? 'true' : '' ) : value
43
+ const [ asPathWithoutHash ] = router . asPath . split ( '#' )
44
+ const [ asPathRoot , asPathQuery = '' ] = asPathWithoutHash . split ( '?' )
45
+ const currentParams = new URLSearchParams ( asPathQuery )
73
46
if ( newValue ) {
74
- params . set ( queryParamKey , newValue )
47
+ currentParams . set ( queryParamKey , newValue )
75
48
} else {
76
- params . delete ( queryParamKey )
49
+ currentParams . delete ( queryParamKey )
50
+ }
51
+ const paramsString = currentParams . toString ( ) ? `?${ currentParams . toString ( ) } ` : ''
52
+ let newUrl = `${ asPathRoot } ${ paramsString } `
53
+ if ( asPathRoot !== '/' && router . locale ) {
54
+ newUrl = `${ router . locale } ${ asPathRoot } ${ paramsString } `
55
+ }
56
+ if ( ! newUrl . startsWith ( '/' ) ) {
57
+ newUrl = `/${ newUrl } `
77
58
}
78
59
79
- const newSearch = params . toString ( )
80
- const newUrl = newSearch ? `${ pathname } ?${ newSearch } ${ hash } ` : `${ pathname } ${ hash } `
60
+ router . replace ( newUrl , undefined , { shallow : true , locale : router . locale , scroll : false } )
81
61
82
- window . history . replaceState ( { } , '' , newUrl )
83
62
setQueryParamState ( newValue )
84
63
}
85
64
86
65
return {
87
66
debug,
88
67
queryParam : queryParam as any , // Type will be set based on overloads
89
- setQueryParam : setQueryParam as any ,
68
+ setQueryParam,
90
69
}
91
70
}
0 commit comments