Skip to content

Commit 0d27160

Browse files
committed
feat: migrate from react router to tanstack router
1 parent 76388cf commit 0d27160

File tree

23 files changed

+693
-204
lines changed

23 files changed

+693
-204
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ lerna-debug.log*
1111
node_modules
1212
*.local
1313

14+
# TanStack Router
15+
routeTree.gen.ts
16+
1417
# Build files
1518
.nyc_output
1619
.junit
@@ -35,3 +38,6 @@ storybook-static/
3538
*.njsproj
3639
*.sln
3740
*.sw?
41+
42+
# Ai
43+
.claude/

apps/web/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
"@package/ui": "workspace:*",
2929
"@tanstack/react-form": "1.19.3",
3030
"@tanstack/react-query": "5.83.0",
31+
"@tanstack/react-router": "^1.103.2",
3132
"clsx": "2.1.1",
3233
"immer": "10.1.1",
3334
"jotai": "2.13.1",
3435
"jotai-effect": "2.1.0",
3536
"react": "catalog:react19",
3637
"react-dom": "catalog:react19",
3738
"react-intl": "7.1.11",
38-
"react-router-dom": "7.8.2",
3939
"zod": "catalog:validation"
4040
},
4141
"devDependencies": {
@@ -46,6 +46,7 @@
4646
"@package/storybook": "workspace:*",
4747
"@storybook/react": "catalog:storybook",
4848
"@tailwindcss/vite": "catalog:tailwind",
49+
"@tanstack/router-plugin": "^1.103.0",
4950
"@vitejs/plugin-basic-ssl": "catalog:vite",
5051
"@vitejs/plugin-react": "catalog:vite",
5152
"babel-plugin-react-compiler": "catalog:react19",

apps/web/src/classes/app-session/UseAppSessionContent.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { useMemo } from "react";
22
import { useFetchPersonalProfileQuery } from "@package/api";
3-
import { useNavigate } from "react-router-dom";
3+
import { useNavigate } from "@tanstack/react-router";
44
import type { IAppSessionContent } from "./IAppSessionContent";
55
import { useAuth } from "~/core/auth/UseAuth";
66

7-
export function useAppSessionContent(aboutRoute: string = "/about"): IAppSessionContent {
7+
export function useAppSessionContent(aboutRoute: string = "/version"): IAppSessionContent {
88
const navigate = useNavigate();
99
const { logout } = useAuth();
1010
const { data: profileInfo } = useFetchPersonalProfileQuery();
@@ -30,7 +30,7 @@ export function useAppSessionContent(aboutRoute: string = "/about"): IAppSession
3030
}
3131

3232
function handleOnAbout(): void {
33-
navigate(aboutRoute);
33+
navigate({ to: aboutRoute as "/version" });
3434
}
3535

3636
return {
Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
11
import type { ReactElement } from "react";
22
import { useQueryClient } from "@tanstack/react-query";
3-
import { RouterProvider, Route, createHashRouter, createRoutesFromElements, Outlet } from "react-router-dom";
4-
import { PrivateRouteLogic } from "./logic/PrivateRouteLogic";
5-
import { PublicRouteLogic } from "./logic/PublicRouteLogic";
6-
import { PrivateRoutes } from "./PrivateRoutes";
7-
import { PublicRoutes } from "./PublicRoutes";
3+
import { RouterProvider, createHashHistory, createRouter } from "@tanstack/react-router";
4+
import { useAuth } from "../auth/UseAuth";
5+
import { routeTree } from "../../routeTree.gen";
6+
7+
const hashHistory = createHashHistory();
8+
9+
const router = createRouter({
10+
routeTree,
11+
history: hashHistory,
12+
context: {
13+
queryClient: undefined!,
14+
authStatus: undefined!,
15+
},
16+
defaultPreload: "intent",
17+
defaultPreloadStaleTime: 0,
18+
});
19+
20+
declare module "@tanstack/react-router" {
21+
interface Register {
22+
router: typeof router;
23+
}
24+
}
825

926
export function AppRoutes(): ReactElement {
1027
const queryClient = useQueryClient();
28+
const { status } = useAuth();
1129

12-
return (
13-
<RouterProvider
14-
router={createHashRouter(
15-
createRoutesFromElements(
16-
<Route path="/" element={<Outlet />}>
17-
<Route element={<PublicRouteLogic />}>
18-
{PublicRoutes(queryClient).map((route) => (
19-
<Route key={route.path} {...route} />
20-
))}
21-
</Route>
22-
<Route element={<PrivateRouteLogic />}>
23-
{PrivateRoutes(queryClient).map((route) => (
24-
<Route key={route.path} {...route} />
25-
))}
26-
</Route>
27-
<Route path="*" lazy={() => import("../../pages/not-found/NotFoundRoute")} />
28-
</Route>
29-
)
30-
)}
31-
/>
32-
);
30+
return <RouterProvider router={router} context={{ queryClient, authStatus: status }} />;
3331
}

apps/web/src/pages/about/UseAboutPage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { useNavigate } from "react-router-dom";
1+
import { useRouter } from "@tanstack/react-router";
22
import type { AboutViewProps } from "~/components/views/about/AboutView";
33
import { useAppInfo } from "~/core/config/UseAppInfo";
44

55
export function useAboutPage(serverVersion: string): AboutViewProps {
66
const { appName } = useAppInfo();
7-
const navigate = useNavigate();
7+
const router = useRouter();
88

99
function handleOnBack(): void {
10-
navigate(-1);
10+
router.history.back();
1111
}
1212

1313
return { appVersion: import.meta.env.VITE_APP_VERSION, serverVersion, appName, onBack: handleOnBack };

apps/web/src/pages/about/modal/UseAboutModalPage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { useNavigate } from "react-router-dom";
1+
import { useRouter } from "@tanstack/react-router";
22
import { useAboutPage } from "../UseAboutPage";
33
import type { AboutModalProps } from "~/components/feedback/about-modal/AboutModal";
44

55
export function useAboutModalPage(serverVersion: string): AboutModalProps {
6-
const navigate = useNavigate();
6+
const router = useRouter();
77
const aboutProps = useAboutPage(serverVersion);
88

99
function handleOnClose(): void {
10-
navigate(-1);
10+
router.history.back();
1111
}
1212

1313
return {

apps/web/src/pages/forgot-password/UseForgotPasswordPage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { addToast } from "@heroui/react";
22
import { usePostForgotPasswordMutate } from "@package/api";
33
import { useIntl } from "react-intl";
4-
import { useNavigate } from "react-router-dom";
4+
import { useNavigate } from "@tanstack/react-router";
55
import type { FormForgotPassword } from "~/components/forms/forgot-password/ForgotPasswordForm";
66
import type { ForgotPasswordViewProps } from "~/components/views/forgot-password/ForgotPasswordView";
77
import { useAppInfo } from "~/core/config/UseAppInfo";
@@ -14,15 +14,15 @@ export function useForgotPasswordPage(): ForgotPasswordViewProps {
1414

1515
function handleOnBack(): void {
1616
console.log("handleBack");
17-
navigate("/login");
17+
navigate({ to: "/login" });
1818
}
1919

2020
async function handleOnSubmit(data: FormForgotPassword): Promise<void> {
2121
const { email } = data;
2222

2323
try {
2424
await submit(email);
25-
navigate("/");
25+
navigate({ to: "/" });
2626
} catch {
2727
addToast({
2828
title: intl.formatMessage({

apps/web/src/pages/login/UseLoginPage.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState } from "react";
22
import { useIntl } from "react-intl";
3-
import { useNavigate } from "react-router-dom";
3+
import { useNavigate } from "@tanstack/react-router";
44
import type { LoginViewProps } from "../../components/views/login/LoginView";
55
import type { FormLogin } from "~/components/forms/login/LoginForm";
66
import { useAuth } from "~/core/auth/UseAuth";
@@ -22,7 +22,7 @@ export function useLoginPage(): LoginViewProps {
2222
password: data.password,
2323
rememberMe: data.remember,
2424
});
25-
navigate("/");
25+
navigate({ to: "/" });
2626
} catch {
2727
setLoginError(
2828
intl.formatMessage({
@@ -35,15 +35,15 @@ export function useLoginPage(): LoginViewProps {
3535
}
3636

3737
function handleOnAbout(): void {
38-
navigate("/about");
38+
navigate({ to: "/about" });
3939
}
4040

4141
function handleForgotPassword(): void {
42-
navigate("/forgot-password", { state: { something: "Incoming!" } });
42+
navigate({ to: "/forgot-password" });
4343
}
4444

4545
function handleSignUp(): void {
46-
navigate("/sign-up");
46+
navigate({ to: "/sign-up" });
4747
}
4848

4949
return {

apps/web/src/pages/not-found/UseNotFoundPage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { useNavigate } from "react-router-dom";
1+
import { useNavigate } from "@tanstack/react-router";
22

33
export function useNotFoundPage(): {
44
handleClick: () => void;
55
} {
66
const navigate = useNavigate();
77

88
function handleClick(): void {
9-
navigate("/login");
9+
navigate({ to: "/login" });
1010
}
1111

1212
return {

apps/web/src/pages/sign-up/UseSignUpPage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { addToast } from "@heroui/react";
22
import { usePostSelfRegister } from "@package/api";
33
import { useIntl } from "react-intl";
4-
import { useNavigate } from "react-router-dom";
4+
import { useNavigate } from "@tanstack/react-router";
55
import type { FormSignUp } from "~/components/forms/sign-up/SignUpForm";
66
import type { SignUpViewProps } from "~/components/views/sign-up/SignUpView";
77
import { useAppInfo } from "~/core/config/UseAppInfo";
@@ -13,13 +13,13 @@ export function useSignUpPage(): SignUpViewProps {
1313
const { isPending, mutateAsync: submit } = usePostSelfRegister();
1414

1515
function handleOnBack(): void {
16-
navigate("/login");
16+
navigate({ to: "/login" });
1717
}
1818

1919
async function handleOnSubmit(data: FormSignUp): Promise<void> {
2020
try {
2121
await submit(data);
22-
navigate("/");
22+
navigate({ to: "/" });
2323
} catch {
2424
addToast({
2525
title: intl.formatMessage({

0 commit comments

Comments
 (0)