Skip to content

Commit

Permalink
fix: add route.to for navigation
Browse files Browse the repository at this point in the history
Fixes #630
  • Loading branch information
tannerlinsley committed Jul 10, 2023
1 parent 6c19fe5 commit 572caa5
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion examples/react/basic-ssr-streaming/src/routes/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Posts() {
return (
<li key={post.id} className="whitespace-nowrap">
<Link
to={postIdRoute.fullPath}
to={postIdRoute.to}
params={{
postId: post.id,
}}
Expand Down
2 changes: 1 addition & 1 deletion examples/react/basic-ssr-with-html/src/routes/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const postsRoute = new Route({
return (
<li key={post.id} className="whitespace-nowrap">
<Link
to={postIdRoute.fullPath}
to={postIdRoute.to}
params={{
postId: post.id,
}}
Expand Down
2 changes: 1 addition & 1 deletion examples/react/basic-ssr/src/routes/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const postsRoute = new Route({
return (
<li key={post.id} className="whitespace-nowrap">
<Link
to={postIdRoute.fullPath}
to={postIdRoute.to}
params={{
postId: post.id,
}}
Expand Down
4 changes: 2 additions & 2 deletions examples/react/basic/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const rootRoute = RootRoute.withRouterContext<RouterContext>()({
Home
</Link>{' '}
<Link
to="/posts"
to={'/posts'}
activeProps={{
className: 'font-bold',
}}
Expand Down Expand Up @@ -144,7 +144,7 @@ const postsRoute = new Route({
return (
<li key={post.id} className="whitespace-nowrap">
<Link
to={postRoute.fullPath}
to={postRoute.to}
params={{
postId: post.id,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const invoicesRoute = new Route({
<Spinner />
) : (
<MatchRoute
to={invoiceRoute.fullPath}
to={invoiceRoute.to}
params={{
invoiceId: invoice.id,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ import { z } from 'zod'
import { fetchInvoiceById, patchInvoice } from '../../../mockTodos'
import { InvoiceFields } from '../../../components/InvoiceFields'
import { invoicesLoader, invoicesRoute } from '.'
import { useNavigate, Link, Route } from '@tanstack/router'
import {
useNavigate,
Link,
Route,
RegisteredRoutesInfo,
} from '@tanstack/router'
import { Loader } from '@tanstack/react-loaders'
import { Action, useAction } from '@tanstack/react-actions'
import { invoicesIndexRoute } from './invoices'

class InvoiceNotFoundError extends Error {}

Expand Down
8 changes: 4 additions & 4 deletions examples/react/kitchen-sink-single-file/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const indexRoute = new Route({
<div className={`text-lg`}>Welcome Home!</div>
<hr className={`my-2`} />
<Link
to={invoiceRoute.fullPath}
to={invoiceRoute.to}
params={{
invoiceId: 3,
}}
Expand Down Expand Up @@ -359,7 +359,7 @@ const invoicesRoute = new Route({
<Spinner />
) : (
<MatchRoute
to={invoiceRoute.fullPath}
to={invoiceRoute.to}
params={{
invoiceId: invoice.id,
}}
Expand Down Expand Up @@ -686,7 +686,7 @@ const usersRoute = new Route({
<pre className="text-sm">
{user.name}{' '}
<MatchRoute
to={userRoute.fullPath}
to={userRoute.to}
search={(d) => ({
...d,
userId: user.id,
Expand Down Expand Up @@ -794,7 +794,7 @@ const authenticatedRoute = new Route({
console.log('tanner', error)
if (error === AuthError) {
throw redirect({
to: loginRoute.fullPath,
to: loginRoute.to,
search: {
// Use latestLocation (not currentLocation) to get the live url
// (as opposed to the committed url, which is technically async
Expand Down
2 changes: 1 addition & 1 deletion examples/react/start-basic/src/app/routes/posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const postsRoute = new Route({
return (
<li key={post.id} className="whitespace-nowrap">
<Link
to={postIdRoute.fullPath}
to={postIdRoute.to}
params={{
postId: post.id,
}}
Expand Down
8 changes: 4 additions & 4 deletions examples/react/start-kitchen-sink-single-file/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ const indexRoute = new Route({
<div className={`text-lg`}>Welcome Home!</div>
<hr className={`my-2`} />
<Link
to={invoiceRoute.fullPath}
to={invoiceRoute.to}
params={{
invoiceId: 3,
}}
Expand Down Expand Up @@ -367,7 +367,7 @@ const invoicesRoute = new Route({
<Spinner />
) : (
<MatchRoute
to={invoiceRoute.fullPath}
to={invoiceRoute.to}
params={{
invoiceId: invoice.id,
}}
Expand Down Expand Up @@ -691,7 +691,7 @@ const usersRoute = new Route({
<pre className="text-sm">
{user.name}{' '}
<MatchRoute
to={userRoute.fullPath}
to={userRoute.to}
search={(d) => ({
...d,
userId: user.id,
Expand Down Expand Up @@ -789,7 +789,7 @@ const authenticatedRoute = new Route({
onLoadError: (error) => {
if (error === AuthError) {
router.navigate({
to: loginRoute.fullPath,
to: loginRoute.to,
search: {
// Use latestLocation (not currentLocation) to get the live url
// (as opposed to the committed url, which is technically async
Expand Down
2 changes: 1 addition & 1 deletion examples/react/with-react-query/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const postsRoute = new Route({
return (
<li key={post.id} className="whitespace-nowrap">
<Link
to={postRoute.fullPath}
to={postRoute.to}
params={{
postId: post.id,
}}
Expand Down
9 changes: 3 additions & 6 deletions packages/router/src/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ export type ParsePathParams<T extends string> = Split<T>[number] extends infer U
: never
: never

type Join<T> = T extends []
type Join<T, Delimiter extends string = '/'> = T extends []
? ''
: T extends [infer L extends string]
? L
: T extends [infer L extends string, ...infer Tail extends [...string[]]]
? CleanPath<`${L}/${Join<Tail>}`>
? CleanPath<`${L}${Delimiter}${Join<Tail>}`>
: never

export type RelativeToPathAutoComplete<
Expand Down Expand Up @@ -286,10 +286,7 @@ export type CheckPath<
: CheckPathError<TRoutesInfo, Exclude<TPath, TRoutesInfo['routePaths']>>

export type CheckPathError<TRoutesInfo extends AnyRoutesInfo, TInvalids> = {
Error: `${TInvalids extends string
? TInvalids
: never} is not a valid route path.`
'Valid Route Paths': TRoutesInfo['routePaths']
to: TRoutesInfo['routePaths']
}

export type CheckId<TRoutesInfo extends AnyRoutesInfo, TPath, TPass> = Exclude<
Expand Down
17 changes: 10 additions & 7 deletions packages/router/src/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ export class Route<
__types!: {
parentRoute: TParentRoute
path: TPath
to: TrimPathRight<TFullPath>
fullPath: TFullPath
customId: TCustomId
id: TId
Expand Down Expand Up @@ -514,6 +515,7 @@ export class Route<
// customId!: TCustomId
path!: TPath
fullPath!: TFullPath
to!: TrimPathRight<TFullPath>

// Optional
children?: TChildren
Expand Down Expand Up @@ -611,6 +613,7 @@ export class Route<
this.id = id as TId
// this.customId = customId as TCustomId
this.fullPath = fullPath as TFullPath
this.to = fullPath as TrimPathRight<TFullPath>
}

addChildren = <TNewChildren extends AnyRoute[]>(
Expand Down Expand Up @@ -771,17 +774,17 @@ type ResolveFullPath<

type RoutePrefix<
TPrefix extends string,
TId extends string,
> = string extends TId
TPath extends string,
> = string extends TPath
? RootRouteId
: TId extends string
: TPath extends string
? TPrefix extends RootRouteId
? TId extends '/'
? TPath extends '/'
? '/'
: `/${TrimPath<TId>}`
: `${TPrefix}/${TId}` extends '/'
: `/${TrimPath<TPath>}`
: `${TPrefix}/${TPath}` extends '/'
? '/'
: `/${TrimPathLeft<`${TrimPathRight<TPrefix>}/${TrimPath<TId>}`>}`
: `/${TrimPathLeft<`${TrimPathRight<TPrefix>}/${TrimPath<TPath>}`>}`
: never

export type TrimPath<T extends string> = '' extends T
Expand Down

0 comments on commit 572caa5

Please sign in to comment.