-
Notifications
You must be signed in to change notification settings - Fork 68.3k
Expand file tree
/
Copy pathuseQueryParam.ts
More file actions
74 lines (65 loc) · 2.73 KB
/
Copy pathuseQueryParam.ts
File metadata and controls
74 lines (65 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// A generic hook for getting and setting a query parameter without reloading the page
// The `queryParam` variable returned from this method are stateful and will be set to the query param on page load
import { useRouter } from 'next/router'
import { useState, useEffect } from 'react'
import { parseDebug } from '@/search/components/hooks/useQuery'
type UseQueryParamReturn<T extends string | boolean> = {
debug: boolean
queryParam: T
setQueryParam: (value: T) => void
}
// Overloads so we can use this for a boolean or string query param
export function useQueryParam(queryParamKey: string, isBoolean: true): UseQueryParamReturn<boolean>
export function useQueryParam(queryParamKey: string, isBoolean?: false): UseQueryParamReturn<string>
export function useQueryParam(
queryParamKey: string,
isBoolean?: boolean,
): UseQueryParamReturn<boolean> | UseQueryParamReturn<string> {
const router = useRouter()
const [queryParamString, setQueryParamState] = useState<string>('')
const [debug, setDebug] = useState<boolean>(false)
const queryParam: string | boolean = isBoolean ? queryParamString === 'true' : queryParamString
// Only set the initial query param values on page load, the rest of the time we use React state
useEffect(() => {
let initialQueryParam = ''
const paramValue = router.query[queryParamKey]
if (paramValue) {
initialQueryParam = Array.isArray(paramValue) ? paramValue[0] : paramValue
}
setQueryParamState(initialQueryParam)
setDebug(parseDebug(router.query.debug || '') || false)
}, [queryParamKey, router.pathname])
const setQueryParam = (value: string | boolean) => {
const newValue = typeof value === 'boolean' ? (value ? 'true' : '') : value
const [asPathWithoutHash] = router.asPath.split('#')
const [asPathRoot, asPathQuery = ''] = asPathWithoutHash.split('?')
const currentParams = new URLSearchParams(asPathQuery)
if (newValue) {
currentParams.set(queryParamKey, newValue)
} else {
currentParams.delete(queryParamKey)
}
const paramsString = currentParams.toString() ? `?${currentParams.toString()}` : ''
let newUrl = `${asPathRoot}${paramsString}`
if (asPathRoot !== '/' && router.locale) {
newUrl = `${router.locale}${asPathRoot}${paramsString}`
}
if (!newUrl.startsWith('/')) {
newUrl = `/${newUrl}`
}
router.replace(newUrl, undefined, { shallow: true, locale: router.locale, scroll: false })
setQueryParamState(newValue)
}
if (isBoolean) {
return {
debug,
queryParam: queryParam as boolean,
setQueryParam: setQueryParam as (value: boolean) => void,
}
}
return {
debug,
queryParam: queryParam as string,
setQueryParam: setQueryParam as (value: string) => void,
}
}