Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

_tutorial._db.projects.index.tsx

32 lines875 Bytes

1import { createFileRoute, notFound, redirect } from "@tanstack/react-router";
2import { createIsomorphicFn } from "@tanstack/react-start";
3import type { ProjectRecord } from "@/db/schema";
4import { API, getDataFromApi } from "@/local-api";
5​
6const getFirstProject = createIsomorphicFn().client(async () => {
7 const [firstProject] = await getDataFromApi<ProjectRecord[]>(
8 API["/api/projects"].GET,
9 );
10 return firstProject;
11});
12​
13export const Route = createFileRoute("/_tutorial/_db/projects/")({
14 beforeLoad: async () => {
15 const firstProject = await getFirstProject();
16 const id = firstProject?.id;
17​
18 if (!id) {
19 throw notFound();
20 }
21​
22 throw redirect({
23 to: "/projects/$projectId",
24 params: {
25 projectId: id,
26 },
27 });
28 },
29 notFoundComponent: () => {
30 return <div>No projects were found in the database.</div>;
31 },
32});