-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Matches.tsx
576 lines (513 loc) · 16.6 KB
/
Matches.tsx
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import * as React from 'react'
import invariant from 'tiny-invariant'
import warning from 'tiny-warning'
import { CatchBoundary, ErrorComponent } from './CatchBoundary'
import { useRouterState } from './useRouterState'
import { useRouter } from './useRouter'
import { ResolveRelativePath, ToOptions } from './link'
import {
AnyRoute,
ReactNode,
RootSearchSchema,
StaticDataRouteOption,
UpdatableStaticRouteOption,
rootRouteId,
} from './route'
import {
AllParams,
FullSearchSchema,
ParseRoute,
RouteById,
RouteByPath,
RouteIds,
RoutePaths,
} from './routeInfo'
import { RegisteredRouter, RouterState } from './router'
import { DeepPartial, Expand, NoInfer, StrictOrFrom, pick } from './utils'
import {
CatchNotFound,
DefaultGlobalNotFound,
NotFoundError,
isNotFound,
} from './not-found'
export const matchContext = React.createContext<string | undefined>(undefined)
export interface RouteMatch<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TRouteId extends RouteIds<TRouteTree> = ParseRoute<TRouteTree>['id'],
TReturnIntersection extends boolean = false,
> {
id: string
routeId: TRouteId
pathname: string
params: TReturnIntersection extends false
? RouteById<TRouteTree, TRouteId>['types']['allParams']
: Expand<Partial<AllParams<TRouteTree>>>
status: 'pending' | 'success' | 'error'
isFetching: boolean
showPending: boolean
error: unknown
paramsError: unknown
searchError: unknown
updatedAt: number
loadPromise?: Promise<void>
loaderData?: RouteById<TRouteTree, TRouteId>['types']['loaderData']
routeContext: RouteById<TRouteTree, TRouteId>['types']['routeContext']
context: RouteById<TRouteTree, TRouteId>['types']['allContext']
search: TReturnIntersection extends false
? Exclude<
RouteById<TRouteTree, TRouteId>['types']['fullSearchSchema'],
RootSearchSchema
>
: Expand<
Partial<Omit<FullSearchSchema<TRouteTree>, keyof RootSearchSchema>>
>
fetchCount: number
abortController: AbortController
cause: 'preload' | 'enter' | 'stay'
loaderDeps: RouteById<TRouteTree, TRouteId>['types']['loaderDeps']
preload: boolean
invalid: boolean
pendingPromise?: Promise<void>
meta?: JSX.IntrinsicElements['meta'][]
links?: JSX.IntrinsicElements['link'][]
scripts?: JSX.IntrinsicElements['script'][]
headers?: Record<string, string>
notFoundError?: NotFoundError
staticData: StaticDataRouteOption
}
export type AnyRouteMatch = RouteMatch<any, any>
export function Matches() {
const router = useRouter()
const matchId = useRouterState({
select: (s) => {
return getRenderedMatches(s)[0]?.id
},
})
return (
<matchContext.Provider value={matchId}>
<CatchBoundary
getResetKey={() => router.state.resolvedLocation.state?.key!}
errorComponent={ErrorComponent}
onCatch={(error) => {
warning(
false,
`The following error wasn't caught by any route! 👇 At the very least, consider setting an 'errorComponent' in your RootRoute!`,
)
console.error(error)
}}
>
{matchId ? <Match matchId={matchId} /> : null}
</CatchBoundary>
</matchContext.Provider>
)
}
function SafeFragment(props: any) {
return <>{props.children}</>
}
export function Match({ matchId }: { matchId: string }) {
const router = useRouter()
const routeId = useRouterState({
select: (s) =>
getRenderedMatches(s).find((d) => d.id === matchId)?.routeId as string,
})
invariant(
routeId,
`Could not find routeId for matchId "${matchId}". Please file an issue!`,
)
const route = router.routesById[routeId]!
const PendingComponent = (route.options.pendingComponent ??
router.options.defaultPendingComponent) as any
const pendingElement = PendingComponent ? <PendingComponent /> : null
const routeErrorComponent =
route.options.errorComponent ??
router.options.defaultErrorComponent ??
ErrorComponent
const routeNotFoundComponent = route.isRoot
? // If it's the root route, use the globalNotFound option, with fallback to the notFoundRoute's component
route.options.notFoundComponent ??
router.options.notFoundRoute?.options.component
: route.options.notFoundComponent
const ResolvedSuspenseBoundary =
route.options.wrapInSuspense ??
PendingComponent ??
route.options.component?.preload ??
route.options.pendingComponent?.preload ??
(route.options.errorComponent as any)?.preload
? React.Suspense
: SafeFragment
const ResolvedCatchBoundary = routeErrorComponent
? CatchBoundary
: SafeFragment
const ResolvedNotFoundBoundary = routeNotFoundComponent
? CatchNotFound
: SafeFragment
return (
<matchContext.Provider value={matchId}>
<ResolvedSuspenseBoundary fallback={pendingElement}>
<ResolvedCatchBoundary
getResetKey={() => router.state.resolvedLocation.state?.key!}
errorComponent={routeErrorComponent}
onCatch={(error) => {
// Forward not found errors (we don't want to show the error component for these)
if (isNotFound(error)) throw error
warning(false, `Error in route match: ${matchId}`)
console.error(error)
}}
>
<ResolvedNotFoundBoundary
fallback={(error) => {
// If the current not found handler doesn't exist or doesn't handle global not founds, forward it up the tree
if (!routeNotFoundComponent || (error.global && !route.isRoot))
throw error
return React.createElement(routeNotFoundComponent, {
data: error.data,
})
}}
>
<MatchInner matchId={matchId!} pendingElement={pendingElement} />
</ResolvedNotFoundBoundary>
</ResolvedCatchBoundary>
</ResolvedSuspenseBoundary>
</matchContext.Provider>
)
}
function MatchInner({
matchId,
pendingElement,
}: {
matchId: string
pendingElement: any
}): any {
const router = useRouter()
const routeId = useRouterState({
select: (s) =>
getRenderedMatches(s).find((d) => d.id === matchId)?.routeId as string,
})
const route = router.routesById[routeId]!
const match = useRouterState({
select: (s) =>
pick(getRenderedMatches(s).find((d) => d.id === matchId)!, [
'status',
'error',
'showPending',
'loadPromise',
'notFoundError',
]),
})
// If a global not-found is found, and it's the root route, render the global not-found component.
if (match.notFoundError) {
if (routeId === rootRouteId && !route.options.notFoundComponent)
return <DefaultGlobalNotFound />
invariant(
route.options.notFoundComponent,
'Route matched with notFoundError should have a notFoundComponent',
)
return <route.options.notFoundComponent data={match.notFoundError} />
}
if (match.status === 'error') {
if (isServerSideError(match.error)) {
const deserializeError =
router.options.errorSerializer?.deserialize ?? defaultDeserializeError
throw deserializeError(match.error.data)
} else {
throw match.error
}
}
if (match.status === 'pending') {
if (match.showPending) {
return pendingElement
}
throw match.loadPromise
}
if (match.status === 'success') {
let Comp = route.options.component ?? router.options.defaultComponent
if (Comp) {
return <Comp />
}
return <Outlet />
}
invariant(
false,
'Idle routeMatch status encountered during rendering! You should never see this. File an issue!',
)
}
export const Outlet = React.memo(function Outlet() {
const matchId = React.useContext(matchContext)
const childMatchId = useRouterState({
select: (s) => {
const matches = getRenderedMatches(s)
const index = matches.findIndex((d) => d.id === matchId)
return matches[index + 1]?.id
},
})
if (!childMatchId) {
return null
}
return <Match matchId={childMatchId} />
})
export interface MatchRouteOptions {
pending?: boolean
caseSensitive?: boolean
includeSearch?: boolean
fuzzy?: boolean
}
export type UseMatchRouteOptions<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RoutePaths<TRouteTree> = RoutePaths<TRouteTree>,
TTo extends string = '',
TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
TMaskTo extends string = '',
Options extends ToOptions<
TRouteTree,
TFrom,
TTo,
TMaskFrom,
TMaskTo
> = ToOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
RelaxedOptions = Omit<Options, 'search' | 'params'> &
DeepPartial<Pick<Options, 'search' | 'params'>>,
> = RelaxedOptions & MatchRouteOptions
export function useMatchRoute<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
>() {
useRouterState({ select: (s) => [s.location, s.resolvedLocation] })
const { matchRoute } = useRouter()
return React.useCallback(
<
TFrom extends RoutePaths<TRouteTree> = RoutePaths<TRouteTree>,
TTo extends string = '',
TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
TMaskTo extends string = '',
TResolved extends string = ResolveRelativePath<TFrom, NoInfer<TTo>>,
>(
opts: UseMatchRouteOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
): false | RouteById<TRouteTree, TResolved>['types']['allParams'] => {
const { pending, caseSensitive, fuzzy, includeSearch, ...rest } = opts
return matchRoute(rest as any, {
pending,
caseSensitive,
fuzzy,
includeSearch,
})
},
[],
)
}
export type MakeMatchRouteOptions<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RoutePaths<TRouteTree> = RoutePaths<TRouteTree>,
TTo extends string = '',
TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
TMaskTo extends string = '',
> = UseMatchRouteOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
// If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
children?:
| ((
params?: RouteByPath<
TRouteTree,
ResolveRelativePath<TFrom, NoInfer<TTo>>
>['types']['allParams'],
) => ReactNode)
| React.ReactNode
}
export function MatchRoute<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RoutePaths<TRouteTree> = RoutePaths<TRouteTree>,
TTo extends string = '',
TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
TMaskTo extends string = '',
>(
props: MakeMatchRouteOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
): any {
const matchRoute = useMatchRoute()
const params = matchRoute(props as any)
if (typeof props.children === 'function') {
return (props.children as any)(params)
}
return !!params ? props.children : null
}
export function getRenderedMatches<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
>(state: RouterState<TRouteTree>) {
return state.pendingMatches?.some((d) => d.showPending)
? state.pendingMatches
: state.matches
}
export function useMatch<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
TReturnIntersection extends boolean = false,
TRouteMatchState = RouteMatch<TRouteTree, TFrom, TReturnIntersection>,
TSelected = TRouteMatchState,
>(
opts: StrictOrFrom<TFrom, TReturnIntersection> & {
select?: (match: TRouteMatchState) => TSelected
},
): TSelected {
const router = useRouter()
const nearestMatchId = React.useContext(matchContext)
const nearestMatchRouteId = getRenderedMatches(router.state).find(
(d) => d.id === nearestMatchId,
)?.routeId
const matchRouteId = (() => {
const matches = getRenderedMatches(router.state)
const match = opts?.from
? matches.find((d) => d.routeId === opts?.from)
: matches.find((d) => d.id === nearestMatchId)
return match!.routeId
})()
if (opts?.strict ?? true) {
invariant(
nearestMatchRouteId == matchRouteId,
`useMatch("${
matchRouteId as string
}") is being called in a component that is meant to render the '${nearestMatchRouteId}' route. Did you mean to 'useMatch("${
matchRouteId as string
}", { strict: false })' or 'useRoute("${
matchRouteId as string
}")' instead?`,
)
}
const matchSelection = useRouterState({
select: (state) => {
const match = getRenderedMatches(state).find(
(d) => d.id === nearestMatchId,
)
invariant(
match,
`Could not find ${
opts?.from
? `an active match from "${opts.from}"`
: 'a nearest match!'
}`,
)
return opts?.select ? opts.select(match as any) : match
},
})
return matchSelection as any
}
export function useMatches<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TRouteId extends RouteIds<TRouteTree> = ParseRoute<TRouteTree>['id'],
TReturnIntersection extends boolean = false,
TRouteMatch = RouteMatch<TRouteTree, TRouteId, TReturnIntersection>,
T = TRouteMatch[],
>(opts?: {
select?: (matches: TRouteMatch[]) => T
experimental_returnIntersection?: TReturnIntersection
}): T {
return useRouterState({
select: (state) => {
const matches = getRenderedMatches(state)
return opts?.select
? opts.select(matches as TRouteMatch[])
: (matches as T)
},
})
}
export function useParentMatches<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TRouteId extends RouteIds<TRouteTree> = ParseRoute<TRouteTree>['id'],
TReturnIntersection extends boolean = false,
TRouteMatch = RouteMatch<TRouteTree, TRouteId, TReturnIntersection>,
T = TRouteMatch[],
>(opts?: {
select?: (matches: TRouteMatch[]) => T
experimental_returnIntersection?: TReturnIntersection
}): T {
const contextMatchId = React.useContext(matchContext)
return useMatches({
select: (matches) => {
matches = matches.slice(
0,
matches.findIndex((d) => d.id === contextMatchId),
)
return opts?.select
? opts.select(matches as TRouteMatch[])
: (matches as T)
},
})
}
export function useChildMatches<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TRouteId extends RouteIds<TRouteTree> = ParseRoute<TRouteTree>['id'],
TReturnIntersection extends boolean = false,
TRouteMatch = RouteMatch<TRouteTree, TRouteId, TReturnIntersection>,
T = TRouteMatch[],
>(opts?: {
select?: (matches: TRouteMatch[]) => T
experimental_returnIntersection?: TReturnIntersection
}): T {
const contextMatchId = React.useContext(matchContext)
return useMatches({
select: (matches) => {
matches = matches.slice(
matches.findIndex((d) => d.id === contextMatchId) + 1,
)
return opts?.select
? opts.select(matches as TRouteMatch[])
: (matches as T)
},
})
}
export function useLoaderDeps<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<
TRouteTree,
TFrom
>,
TSelected = Required<TRouteMatch>['loaderDeps'],
>(
opts: StrictOrFrom<TFrom> & {
select?: (match: TRouteMatch) => TSelected
},
): TSelected {
return useMatch({
...opts,
select: (s) => {
return typeof opts.select === 'function'
? opts.select(s?.loaderDeps)
: s?.loaderDeps
},
})
}
export function useLoaderData<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
TRouteMatch extends RouteMatch<TRouteTree, TFrom> = RouteMatch<
TRouteTree,
TFrom
>,
TSelected = Required<TRouteMatch>['loaderData'],
>(
opts: StrictOrFrom<TFrom> & {
select?: (match: TRouteMatch) => TSelected
},
): TSelected {
return useMatch({
...opts,
select: (s) => {
return typeof opts.select === 'function'
? opts.select(s?.loaderData)
: s?.loaderData
},
})
}
export function isServerSideError(error: unknown): error is {
__isServerError: true
data: Record<string, any>
} {
if (!(typeof error === 'object' && error && 'data' in error)) return false
if (!('__isServerError' in error && error.__isServerError)) return false
if (!(typeof error.data === 'object' && error.data)) return false
return error.__isServerError === true
}
export function defaultDeserializeError(serializedData: Record<string, any>) {
if ('name' in serializedData && 'message' in serializedData) {
const error = new Error(serializedData.message)
error.name = serializedData.name
return error
}
return serializedData.data
}