forked from vercel/next.js
/
hooks-client.ts
67 lines (57 loc) · 2.13 KB
/
hooks-client.ts
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
// useLayoutSegments() // Only the segments for the current place. ['children', 'dashboard', 'children', 'integrations'] -> /dashboard/integrations (/dashboard/layout.js would get ['children', 'dashboard', 'children', 'integrations'])
import { useContext } from 'react'
import {
SearchParamsContext,
// ParamsContext,
PathnameContext,
// LayoutSegmentsContext,
} from './hooks-client-context'
import {
AppRouterContext,
LayoutRouterContext,
} from '../../shared/lib/app-router-context'
/**
* Get the current search params. For example useSearchParams() would return {"foo": "bar"} when ?foo=bar
*/
export function useSearchParams() {
return useContext(SearchParamsContext)
}
/**
* Get an individual search param. For example useSearchParam("foo") would return "bar" when ?foo=bar
*/
export function useSearchParam(key: string): string | string[] {
const params = useContext(SearchParamsContext)
return params[key]
}
// TODO-APP: Move the other router context over to this one
/**
* Get the router methods. For example router.push('/dashboard')
*/
export function useRouter(): import('../../shared/lib/app-router-context').AppRouterInstance {
return useContext(AppRouterContext)
}
// TODO-APP: getting all params when client-side navigating is non-trivial as it does not have route matchers so this might have to be a server context instead.
// export function useParams() {
// return useContext(ParamsContext)
// }
/**
* Get the current pathname. For example usePathname() on /dashboard?foo=bar would return "/dashboard"
*/
export function usePathname(): string {
return useContext(PathnameContext)
}
// TODO-APP: define what should be provided through context.
// export function useLayoutSegments() {
// return useContext(LayoutSegmentsContext)
// }
// TODO-APP: Expand description when the docs are written for it.
/**
* Get the current segment one level down from the layout.
*/
export function useSelectedLayoutSegment(
parallelRouteKey: string = 'children'
): string {
const { tree } = useContext(LayoutRouterContext)
const segment = tree[1][parallelRouteKey][0]
return Array.isArray(segment) ? segment[1] : segment
}