Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@tanstack/router WIP #1009

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"@hookform/resolvers": "^3.1.1",
"@neodrag/react": "^2.0.3",
"@tanstack/react-query": "^4.29.14",
"@tanstack/router": "0.0.1-beta.86",
"@tanstack/router-devtools": "0.0.1-beta.86",
"@types/deep-equal": "^1.0.1",
"@zxch3n/tidy": "github:hackworthltd/tidy#e07fdef2ae7bf593701817113dd47b4cd56c7a97",
"axios": "^1.4.0",
Expand All @@ -36,7 +38,6 @@
"react-cookie": "^4.1.1",
"react-dom": "^18.2.0",
"react-hook-form": "^7.44.3",
"react-router-dom": "^6.13.0",
"reactflow": "^11.7.2",
"universal-cookie": "^4.0.4",
"uuid": "^9.0.0",
Expand Down
101 changes: 59 additions & 42 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 5 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect } from "react";
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
import { RouterProvider } from "@tanstack/router";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { CookiesProvider, useCookies } from "react-cookie";
import { CookieSetOptions } from "universal-cookie";
import { v4 as uuidv4 } from "uuid";

import "@/index.css";

import { ChooseSession, Edit, NoMatch } from "@/components";
import { router } from "@/router";

const queryClient = new QueryClient();

Expand Down Expand Up @@ -37,18 +37,9 @@ const App = (): JSX.Element => {

return (
<CookiesProvider>
<BrowserRouter>
<QueryClientProvider client={queryClient}>
<Routes>
<Route path="/" element={<Navigate to="/sessions" />} />
<Route path="/sessions">
<Route index element={<ChooseSession />} />
<Route path=":sessionId" element={<Edit />} />
</Route>
<Route path="*" element={<NoMatch />} />
</Routes>
</QueryClientProvider>
</BrowserRouter>
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>
</CookiesProvider>
);
};
Expand Down
16 changes: 12 additions & 4 deletions src/components/ChooseSession/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ import {
getGetSessionListQueryKey,
useDeleteSession,
} from "@/primer-api";
import { useNavigate } from "react-router-dom";
import { AnyRoute, useNavigate } from "@tanstack/router";
import { useQueryClient } from "@tanstack/react-query";

const ChooseSession = (): JSX.Element => {
export interface ChooseSessionProps {
route: AnyRoute;
}

const ChooseSession = (p: ChooseSessionProps): JSX.Element => {
const [cookies] = useCookies(["id"]);

// NOTE: pagination in our API is 1-indexed.
Expand Down Expand Up @@ -67,10 +71,14 @@ const ChooseSession = (): JSX.Element => {
setPage(meta.lastPage);
}

const navigate = useNavigate();
const navigate = useNavigate({ from: p.route.id });
const newSession = useCreateSession({
mutation: {
onSuccess: (newSessionID: Uuid) => navigate(`/sessions/${newSessionID}`),
onSuccess: (newSessionId: Uuid) =>
navigate({
to: "/sessions/$sessionId",
params: { sessionId: newSessionId },
}),
},
});

Expand Down
14 changes: 9 additions & 5 deletions src/components/Edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
useRef,
useState,
} from "react";
import { useParams } from "react-router-dom";
import { AnyRoute, useParams } from "@tanstack/router";
import { ReactFlowProvider } from "reactflow";
import {
useGetAvailableActions,
Expand Down Expand Up @@ -54,13 +54,17 @@ import {
// hardcoded values (for now)
const initialLevel: Level = "Expert";

const Edit = (): JSX.Element => {
const params = useParams();
const sessionId = params["sessionId"];
export interface EditProps {
route: AnyRoute;
}

const Edit = (p: EditProps): JSX.Element => {
const { sessionId } = useParams({ from: p.route.id });
if (!sessionId) {
return (
<Error string={"No sessionId parameter: " + JSON.stringify(params)} />
<Error
string={"No sessionId parameter: " + JSON.stringify(p.route.path)}
/>
);
}
// This hook is *technically* conditional.
Expand Down
7 changes: 3 additions & 4 deletions src/components/SessionPreview/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import "@/index.css";

import { StarIcon, TrashIcon, UserPlusIcon } from "@heroicons/react/24/outline";

import { Link } from "react-router-dom";
import { Link } from "@tanstack/router";

import { SessionMeta } from "@/Types";
import { BinaryTreePlaceholder } from "@/components";
Expand All @@ -21,8 +20,8 @@ export const SessionPreview = ({
<div className="flex flex-col divide-y divide-grey-quaternary rounded-lg bg-white-primary text-center drop-shadow-md">
<div className="flex flex-1 flex-col">
<Link
to={`/sessions/${session.id}`}
key={session.id}
to="/sessions/$sessionId"
params={{ sessionId: session.id }}
className="group rounded-t-lg hover:text-blue-primary"
>
<BinaryTreePlaceholder className="mx-auto h-16 w-16 shrink-0 fill-current text-white-primary group-hover:text-blue-primary md:h-48 md:w-48" />
Expand Down
30 changes: 30 additions & 0 deletions src/router.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Outlet, RootRoute, Router } from "@tanstack/router";
import { TanStackRouterDevtools } from "@tanstack/router-devtools";

import { indexRoute } from "@/routes";
import { sessionsRoute } from "@/routes/sessions";
import { sessionIdRoute } from "@/routes/sessions/sessionId";
import { catchAllRoute } from "@/routes/catchAll";

export const rootRoute = new RootRoute({
component: () => (
<>
<Outlet />
<TanStackRouterDevtools />
</>
),
});

const routeTree = rootRoute.addChildren([
indexRoute,
sessionsRoute.addChildren([sessionIdRoute]),
catchAllRoute,
]);

export const router = new Router({ routeTree });

declare module "@tanstack/router" {
interface Register {
router: typeof router;
}
}
9 changes: 9 additions & 0 deletions src/routes/catchAll/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Route } from "@tanstack/router";
import { rootRoute } from "@/router";
import { NoMatch } from "@/components";

export const catchAllRoute = new Route({
getParentRoute: () => rootRoute,
path: "*",
component: NoMatch,
});
11 changes: 11 additions & 0 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Navigate, Route } from "@tanstack/router";

import { rootRoute } from "@/router";

const Root = (): JSX.Element => <Navigate from="/" to="/sessions" />;

export const indexRoute = new Route({
getParentRoute: () => rootRoute,
path: "/",
component: Root,
});
12 changes: 12 additions & 0 deletions src/routes/sessions/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Route } from "@tanstack/router";

import { rootRoute } from "@/router";
import { ChooseSession } from "@/components";

const SessionsRoute = (): JSX.Element => <ChooseSession route={sessionsRoute} />;

export const sessionsRoute = new Route({
getParentRoute: () => rootRoute,
path: "sessions",
component: SessionsRoute,
});
12 changes: 12 additions & 0 deletions src/routes/sessions/sessionId/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Route } from "@tanstack/router";

import { sessionsRoute } from "@/routes/sessions";
import { Edit } from "@/components";

const SessionIdRoute = (): JSX.Element => <Edit route={sessionIdRoute} />;

export const sessionIdRoute = new Route({
getParentRoute: () => sessionsRoute,
path: "$sessionId",
component: SessionIdRoute,
});