Skip to content

Commit c32ab25

Browse files
authored
Cleanup starter (#334)
1 parent b9ee0a6 commit c32ab25

File tree

2 files changed

+30
-59
lines changed

2 files changed

+30
-59
lines changed

examples/react/projects/src/routes/_authenticated.tsx

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,60 @@
1-
import { createFileRoute, useNavigate, Link } from "@tanstack/react-router"
1+
import {
2+
Link,
3+
Outlet,
4+
createFileRoute,
5+
useNavigate,
6+
} from "@tanstack/react-router"
27
import { useEffect, useState } from "react"
3-
import { Outlet } from "@tanstack/react-router"
8+
import { useLiveQuery } from "@tanstack/react-db"
49
import { authClient } from "@/lib/auth-client"
5-
import {
6-
useLiveQuery,
7-
createCollection,
8-
liveQueryCollectionOptions,
9-
createLiveQueryCollection,
10-
not,
11-
like,
12-
count,
13-
} from "@tanstack/react-db"
1410
import { projectCollection } from "@/lib/collections"
1511

16-
export const Route = createFileRoute("/_authenticated")({
12+
export const Route = createFileRoute(`/_authenticated`)({
1713
component: AuthenticatedLayout,
1814
ssr: false,
1915
})
2016

2117
function AuthenticatedLayout() {
2218
const { data: session, isPending } = authClient.useSession()
23-
console.log({ session, isPending })
2419
const navigate = useNavigate()
2520
const [showNewProjectForm, setShowNewProjectForm] = useState(false)
26-
const [newProjectName, setNewProjectName] = useState("")
27-
28-
const countQuery = createLiveQueryCollection({
29-
query: (q) =>
30-
q.from({ projects: projectCollection }).select(({ projects }) => ({
31-
count: count(projects.id),
32-
})),
33-
})
34-
const newQuery = createCollection(
35-
liveQueryCollectionOptions({
36-
query: (q) =>
37-
q
38-
.from({ projects: projectCollection })
39-
.where(({ projects }) => not(like(projects.name, `Default`))),
40-
})
41-
)
21+
const [newProjectName, setNewProjectName] = useState(``)
4222

43-
const { data: notDefault } = useLiveQuery(newQuery)
44-
const { data: countData } = useLiveQuery(countQuery)
45-
console.log({ notDefault, countData })
4623
const { data: projects, isLoading } = useLiveQuery((q) =>
4724
q.from({ projectCollection })
4825
)
4926

5027
useEffect(() => {
51-
if (!isPending && !session) {
52-
navigate({
53-
href: "/login",
28+
if (session && projects.length === 0 && !isLoading) {
29+
projectCollection.insert({
30+
id: Math.floor(Math.random() * 100000),
31+
name: `Default`,
32+
description: `Default project`,
33+
owner_id: session.user.id,
34+
shared_user_ids: [],
35+
created_at: new Date(),
36+
updated_at: new Date(),
5437
})
5538
}
56-
}, [session, isPending, navigate])
57-
58-
useEffect(() => {
59-
if (session && projects && !isLoading) {
60-
const hasProject = projects.length > 0
61-
if (!hasProject) {
62-
projectCollection.insert({
63-
id: Math.floor(Math.random() * 100000),
64-
name: "Default",
65-
description: "Default project",
66-
owner_id: session.user.id,
67-
shared_user_ids: [],
68-
created_at: new Date(),
69-
})
70-
}
71-
}
7239
}, [session, projects, isLoading])
7340

7441
const handleLogout = async () => {
7542
await authClient.signOut()
76-
navigate({ to: "/login" })
43+
navigate({ to: `/login` })
7744
}
7845

7946
const handleCreateProject = () => {
8047
if (newProjectName.trim() && session) {
8148
projectCollection.insert({
8249
id: Math.floor(Math.random() * 100000),
8350
name: newProjectName.trim(),
84-
description: "",
51+
description: ``,
8552
owner_id: session.user.id,
8653
shared_user_ids: [],
8754
created_at: new Date(),
55+
updated_at: new Date(),
8856
})
89-
setNewProjectName("")
57+
setNewProjectName(``)
9058
setShowNewProjectForm(false)
9159
}
9260
}
@@ -154,7 +122,7 @@ function AuthenticatedLayout() {
154122
type="text"
155123
value={newProjectName}
156124
onChange={(e) => setNewProjectName(e.target.value)}
157-
onKeyDown={(e) => e.key === "Enter" && handleCreateProject()}
125+
onKeyDown={(e) => e.key === `Enter` && handleCreateProject()}
158126
placeholder="Project name"
159127
className="w-full px-2 py-1 border border-gray-300 rounded text-sm"
160128
/>

examples/react/projects/src/routes/_authenticated/index.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ export const Route = createFileRoute(`/_authenticated/`)({
77
component: IndexRedirect,
88
ssr: false,
99
loader: async () => {
10-
console.log(1)
10+
const timeout = setTimeout(() => {
11+
window.location.href = `/login`
12+
}, 500)
1113
await projectCollection.preload()
1214
await todoCollection.preload()
13-
console.log(2)
15+
clearTimeout(timeout)
16+
1417
return null
1518
},
1619
})
@@ -20,10 +23,10 @@ function IndexRedirect() {
2023
const { data: projects } = useLiveQuery((q) => q.from({ projectCollection }))
2124

2225
useEffect(() => {
23-
if (projects && projects.length > 0) {
26+
if (projects.length > 0) {
2427
const firstProject = projects[0]
2528
navigate({
26-
to: "/project/$projectId",
29+
to: `/project/$projectId`,
2730
params: { projectId: firstProject.id.toString() },
2831
replace: true,
2932
})

0 commit comments

Comments
 (0)