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

Add tanstack query and router #14

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
847 changes: 833 additions & 14 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
},
"dependencies": {
"@tanstack/react-query": "^5.40.0",
"@tanstack/react-router": "^1.34.7",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@tanstack/router-devtools": "^1.34.7",
"@tanstack/router-vite-plugin": "^1.34.8",
"@types/react": "18.2.66",
"@types/react-dom": "18.2.22",
"@vitejs/plugin-react-swc": "3.5.0",
Expand Down
42 changes: 0 additions & 42 deletions packages/ui/src/App.css

This file was deleted.

30 changes: 3 additions & 27 deletions packages/ui/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
import reactLogo from "@/assets/react.svg";
import { Button } from "@/components";
import { useState } from "react";
import "./App.css";
import viteLogo from "/vite.svg";
import { router } from "@/shared/config/tanstackRouter";
import { RouterProvider } from "@tanstack/react-router";

function App() {
const [count, setCount] = useState(0);

return (
<>
<div className="flex flex-col items-center">
<a href="https://vitejs.dev" target="_blank" rel="noreferrer">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank" rel="noreferrer">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
<h1>Vite + React</h1>
<div className="card">
<Button onClick={() => setCount((count) => count + 1)}>count is {count}</Button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div>
<p className="read-the-docs">Click on the Vite and React logos to learn more</p>
</>
);
return <RouterProvider router={router} />;
}

export default App;
1 change: 0 additions & 1 deletion packages/ui/src/assets/react.svg

This file was deleted.

18 changes: 18 additions & 0 deletions packages/ui/src/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Link } from "@tanstack/react-router";

export default function Layout({ children }: { children: React.ReactNode }) {
return (
<div className="w-screen h-screen">
<div className="p-2 flex gap-2">
<Link to="/" className="[&.active]:font-bold">
Home
</Link>{" "}
<Link to="/about" className="[&.active]:font-bold">
About
</Link>
</div>
<hr />
<main className="p-2">{children}</main>
</div>
);
}
82 changes: 82 additions & 0 deletions packages/ui/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* prettier-ignore-start */

/* eslint-disable */

// @ts-nocheck

// noinspection JSUnusedGlobalSymbols

// This file is auto-generated by TanStack Router

import { createFileRoute } from '@tanstack/react-router'

// Import Routes

import { Route as rootRoute } from './routes/__root'

// Create Virtual Routes

const AboutLazyImport = createFileRoute('/about')()
const IndexLazyImport = createFileRoute('/')()

// Create/Update Routes

const AboutLazyRoute = AboutLazyImport.update({
path: '/about',
getParentRoute: () => rootRoute,
} as any).lazy(() => import('./routes/about.lazy').then((d) => d.Route))

const IndexLazyRoute = IndexLazyImport.update({
path: '/',
getParentRoute: () => rootRoute,
} as any).lazy(() => import('./routes/index.lazy').then((d) => d.Route))

// Populate the FileRoutesByPath interface

declare module '@tanstack/react-router' {
interface FileRoutesByPath {
'/': {
id: '/'
path: '/'
fullPath: '/'
preLoaderRoute: typeof IndexLazyImport
parentRoute: typeof rootRoute
}
'/about': {
id: '/about'
path: '/about'
fullPath: '/about'
preLoaderRoute: typeof AboutLazyImport
parentRoute: typeof rootRoute
}
}
}

// Create and export the route tree

export const routeTree = rootRoute.addChildren({
IndexLazyRoute,
AboutLazyRoute,
})

/* prettier-ignore-end */

/* ROUTE_MANIFEST_START
{
"routes": {
"__root__": {
"filePath": "__root.tsx",
"children": [
"/",
"/about"
]
},
"/": {
"filePath": "index.lazy.tsx"
},
"/about": {
"filePath": "about.lazy.tsx"
}
}
}
ROUTE_MANIFEST_END */
20 changes: 20 additions & 0 deletions packages/ui/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createRootRoute, Outlet } from "@tanstack/react-router";
import React from "react";

const TanStackRouterDevtools =
process.env.NODE_ENV === "production"
? () => null // Render nothing in production
: React.lazy(() =>
import("@tanstack/router-devtools").then((res) => ({
default: res.TanStackRouterDevtools,
}))
);

export const Route = createRootRoute({
component: () => (
<>
<Outlet />
<TanStackRouterDevtools />
</>
),
});
10 changes: 10 additions & 0 deletions packages/ui/src/routes/about.lazy.tsx
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a way to put a general Layout for all the routes, I think is creating a _layout.tsx in the root of routes folder
https://tanstack.com/router/latest/docs/framework/react/guide/routing-concepts#pathless--layout-routes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw that, but I made it using what i saw in the demented-games repo. should i do it this way instead?

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Layout from "@/layout";
import { createLazyFileRoute } from "@tanstack/react-router";

export const Route = createLazyFileRoute("/about")({
component: About,
});

function About() {
return <Layout>Hello /about!</Layout>;
}
14 changes: 14 additions & 0 deletions packages/ui/src/routes/index.lazy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Layout from "@/layout";
import { createLazyFileRoute } from "@tanstack/react-router";

export const Route = createLazyFileRoute("/")({
component: Index,
});

function Index() {
return (
<Layout>
<h3>Welcome Home!</h3>
</Layout>
);
}
10 changes: 10 additions & 0 deletions packages/ui/src/shared/config/tanstackRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { routeTree } from "@/routeTree.gen";
import { createRouter } from "@tanstack/react-router";

export const router = createRouter({ routeTree });

declare module "@tanstack/react-router" {
interface Register {
router: typeof router;
}
}
7 changes: 4 additions & 3 deletions packages/ui/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { defineConfig } from "vite";
import { TanStackRouterVite } from "@tanstack/router-vite-plugin";
import react from "@vitejs/plugin-react-swc";
import tsconfigPaths from "vite-tsconfig-paths";
import path from "path";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

// https://vitejs.dev/config/
export default defineConfig({
base: "./",
plugins: [react(), tsconfigPaths()],
plugins: [react(), tsconfigPaths(), TanStackRouterVite()],
resolve: {
alias: {
"@": path.resolve(__dirname, "./src"),
Expand Down