Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

useMatch.tsx

120 lines3.4 KB

1import * as Solid from 'solid-js'
2import invariant from 'tiny-invariant'
3import { useRouterState } from './useRouterState'
4import { dummyMatchContext, matchContext } from './matchContext'
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
27​
28export type UseMatchRoute<out TFrom> = <
29 TRouter extends AnyRouter = RegisteredRouter,
30 TSelected = unknown,
31>(
32 opts?: UseMatchBaseOptions<TRouter, TFrom, true, true, TSelected>,
33) => Solid.Accessor<UseMatchResult<TRouter, TFrom, true, TSelected>>
34​
35export type UseMatchOptions<
36 TRouter extends AnyRouter,
37 TFrom extends string | undefined,
38 TStrict extends boolean,
39 TThrow extends boolean,
40 TSelected,
41> = StrictOrFrom<TRouter, TFrom, TStrict> &
42 UseMatchBaseOptions<TRouter, TFrom, TStrict, TThrow, TSelected>
43​
44export type UseMatchResult<
45 TRouter extends AnyRouter,
46 TFrom,
47 TStrict extends boolean,
48 TSelected,
49> = unknown extends TSelected
50 ? TStrict extends true
51 ? MakeRouteMatch<TRouter['routeTree'], TFrom, TStrict>
52 : MakeRouteMatchUnion<TRouter>
53 : TSelected
54​
55export function useMatch<
56 TRouter extends AnyRouter = RegisteredRouter,
57 const TFrom extends string | undefined = undefined,
58 TStrict extends boolean = true,
59 TThrow extends boolean = true,
60 TSelected = unknown,
61>(
62 opts: UseMatchOptions<
63 TRouter,
64 TFrom,
65 TStrict,
66 ThrowConstraint<TStrict, TThrow>,
67 TSelected
68 >,
69): Solid.Accessor<
70 ThrowOrOptional<UseMatchResult<TRouter, TFrom, TStrict, TSelected>, TThrow>
71> {
72 const nearestMatchId = Solid.useContext(
73 opts.from ? dummyMatchContext : matchContext,
74 )
75​
76 // Create a signal to track error state separately from the match
77 const matchState: Solid.Accessor<{
78 match: any
79 shouldThrowError: boolean
80 }> = useRouterState({
81 select: (state: any) => {
82 const match = state.matches.find((d: any) =>
83 opts.from ? opts.from === d.routeId : d.id === nearestMatchId(),
84 )
85​
86 if (match === undefined) {
87 // During navigation transitions, check if the match exists in pendingMatches
88 const pendingMatch = state.pendingMatches?.find((d: any) =>
89 opts.from ? opts.from === d.routeId : d.id === nearestMatchId(),
90 )
91​
92 // Determine if we should throw an error
93 const shouldThrowError =
94 !pendingMatch && !state.isTransitioning && (opts.shouldThrow ?? true)
95​
96 return { match: undefined, shouldThrowError }
97 }
98​
99 return {
100 match: opts.select ? opts.select(match) : match,
101 shouldThrowError: false,
102 }
103 },
104 } as any)
105​
106 // Use createEffect to throw errors outside the reactive selector context
107 // This allows error boundaries to properly catch the errors
108 Solid.createEffect(() => {
109 const state = matchState()
110 if (state.shouldThrowError) {
111 invariant(
112 false,
113 `Could not find ${opts.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`,
114 )
115 }
116 })
117​
118 // Return an accessor that extracts just the match value
119 return Solid.createMemo(() => matchState().match) as any