|
1 |
| -import { createFileRoute, useNavigate, Link } from "@tanstack/react-router" |
| 1 | +import { |
| 2 | + Link, |
| 3 | + Outlet, |
| 4 | + createFileRoute, |
| 5 | + useNavigate, |
| 6 | +} from "@tanstack/react-router" |
2 | 7 | import { useEffect, useState } from "react"
|
3 |
| -import { Outlet } from "@tanstack/react-router" |
| 8 | +import { useLiveQuery } from "@tanstack/react-db" |
4 | 9 | 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" |
14 | 10 | import { projectCollection } from "@/lib/collections"
|
15 | 11 |
|
16 |
| -export const Route = createFileRoute("/_authenticated")({ |
| 12 | +export const Route = createFileRoute(`/_authenticated`)({ |
17 | 13 | component: AuthenticatedLayout,
|
18 | 14 | ssr: false,
|
19 | 15 | })
|
20 | 16 |
|
21 | 17 | function AuthenticatedLayout() {
|
22 | 18 | const { data: session, isPending } = authClient.useSession()
|
23 |
| - console.log({ session, isPending }) |
24 | 19 | const navigate = useNavigate()
|
25 | 20 | 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(``) |
42 | 22 |
|
43 |
| - const { data: notDefault } = useLiveQuery(newQuery) |
44 |
| - const { data: countData } = useLiveQuery(countQuery) |
45 |
| - console.log({ notDefault, countData }) |
46 | 23 | const { data: projects, isLoading } = useLiveQuery((q) =>
|
47 | 24 | q.from({ projectCollection })
|
48 | 25 | )
|
49 | 26 |
|
50 | 27 | 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(), |
54 | 37 | })
|
55 | 38 | }
|
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 |
| - } |
72 | 39 | }, [session, projects, isLoading])
|
73 | 40 |
|
74 | 41 | const handleLogout = async () => {
|
75 | 42 | await authClient.signOut()
|
76 |
| - navigate({ to: "/login" }) |
| 43 | + navigate({ to: `/login` }) |
77 | 44 | }
|
78 | 45 |
|
79 | 46 | const handleCreateProject = () => {
|
80 | 47 | if (newProjectName.trim() && session) {
|
81 | 48 | projectCollection.insert({
|
82 | 49 | id: Math.floor(Math.random() * 100000),
|
83 | 50 | name: newProjectName.trim(),
|
84 |
| - description: "", |
| 51 | + description: ``, |
85 | 52 | owner_id: session.user.id,
|
86 | 53 | shared_user_ids: [],
|
87 | 54 | created_at: new Date(),
|
| 55 | + updated_at: new Date(), |
88 | 56 | })
|
89 |
| - setNewProjectName("") |
| 57 | + setNewProjectName(``) |
90 | 58 | setShowNewProjectForm(false)
|
91 | 59 | }
|
92 | 60 | }
|
@@ -154,7 +122,7 @@ function AuthenticatedLayout() {
|
154 | 122 | type="text"
|
155 | 123 | value={newProjectName}
|
156 | 124 | onChange={(e) => setNewProjectName(e.target.value)}
|
157 |
| - onKeyDown={(e) => e.key === "Enter" && handleCreateProject()} |
| 125 | + onKeyDown={(e) => e.key === `Enter` && handleCreateProject()} |
158 | 126 | placeholder="Project name"
|
159 | 127 | className="w-full px-2 py-1 border border-gray-300 rounded text-sm"
|
160 | 128 | />
|
|
0 commit comments