Open History to see the last commit
1import * as Solid from 'solid-js'
2import { invariant, replaceEqualDeep } from '@tanstack/router-core'
3import { nearestMatchContext } from './matchContext'
4import { useRouter } from './useRouter'
5import type {
6 AnyRouter,
7 MakeRouteMatch,
8 MakeRouteMatchUnion,
9 RegisteredRouter,
10 StrictOrFrom,
11 ThrowConstraint,
12 ThrowOrOptional,
13} from '@tanstack/router-core'
14β
15export interface UseMatchBaseOptions<
16 TRouter extends AnyRouter,
17 TFrom,
18 TStrict extends boolean,
19 TThrow extends boolean,
20 TSelected,
21> {
22 select?: (
23 match: MakeRouteMatch<TRouter['routeTree'], TFrom, TStrict>,
24 ) => TSelected
25 shouldThrow?: TThrow
26}
27β
28export type UseMatchRoute<out TFrom> = <
29 TRouter extends AnyRouter = RegisteredRouter,
30 TSelected = unknown,
31 TThrow extends boolean = true,
32>(
33 opts?: UseMatchBaseOptions<TRouter, TFrom, true, TThrow, TSelected>,
34) => Solid.Accessor<
35 ThrowOrOptional<UseMatchResult<TRouter, TFrom, true, TSelected>, TThrow>
36>
37β
38export type UseMatchOptions<
39 TRouter extends AnyRouter,
40 TFrom extends string | undefined,
41 TStrict extends boolean,
42 TThrow extends boolean,
43 TSelected,
44> = StrictOrFrom<TRouter, TFrom, TStrict> &
45 UseMatchBaseOptions<TRouter, TFrom, TStrict, TThrow, TSelected>
46β
47export type UseMatchResult<
48 TRouter extends AnyRouter,
49 TFrom,
50 TStrict extends boolean,
51 TSelected,
52> = unknown extends TSelected
53 ? TStrict extends true
54 ? MakeRouteMatch<TRouter['routeTree'], TFrom, TStrict>
55 : MakeRouteMatchUnion<TRouter>
56 : TSelected
57β
58export function useMatch<
59 TRouter extends AnyRouter = RegisteredRouter,
60 const TFrom extends string | undefined = undefined,
61 TStrict extends boolean = true,
62 TThrow extends boolean = true,
63 TSelected = unknown,
64>(
65 opts: UseMatchOptions<
66 TRouter,
67 TFrom,
68 TStrict,
69 ThrowConstraint<TStrict, TThrow>,
70 TSelected
71 >,
72): Solid.Accessor<
73 ThrowOrOptional<UseMatchResult<TRouter, TFrom, TStrict, TSelected>, TThrow>
74> {
75 const router = useRouter<TRouter>()
76 const contextMatch = Solid.useContext(nearestMatchContext)
77 const nearestMatch = opts.from ? undefined : contextMatch
78β
79 const match = () => {
80 if (opts.from) {
81 return router.stores.getMatchStore(opts.from).get()
82 }
83β
84 return nearestMatch?.[1 /* match */]()
85 }
86β
87 Solid.createEffect(() => {
88 if (match() !== undefined) {
89 return
90 }
91β
92 if (opts.shouldThrow ?? true) {
93 if (process.env.NODE_ENV !== 'production') {
94 throw new Error(
95 `Invariant failed: Could not find ${opts.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`,
96 )
97 }
98β
99 invariant()
100 }
101 })
102β
103 return Solid.createMemo((prev: TSelected | undefined) => {
104 const selectedMatch = match()
105β
106 if (selectedMatch === undefined) {
107 return undefined
108 }
109β
110 const res = opts.select ? opts.select(selectedMatch) : selectedMatch
111 if (prev === undefined) return res as TSelected
112 return replaceEqualDeep(prev, res) as TSelected
113 }) as any
114}