Open History to see the last commit
1import {
2 Link,
3 Outlet,
4 createFileRoute,
5 useNavigate,
6} from "@tanstack/react-router"
7import { useEffect, useState } from "react"
8import { useLiveQuery } from "@tanstack/react-db"
9import { authClient } from "@/lib/auth-client"
10import { projectCollection } from "@/lib/collections"
11
12export const Route = createFileRoute(`/_authenticated`)({
13 component: AuthenticatedLayout,
14 ssr: false,
15})
16
17function AuthenticatedLayout() {
18 const { data: session, isPending } = authClient.useSession()
19 const navigate = useNavigate()
20 const [showNewProjectForm, setShowNewProjectForm] = useState(false)
21 const [newProjectName, setNewProjectName] = useState(``)
22
23 const { data: projects, isLoading } = useLiveQuery((q) =>
24 q.from({ projectCollection })
25 )
26
27 useEffect(() => {
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(),
37 })
38 }
39 }, [session, projects, isLoading])
40
41 const handleLogout = async () => {
42 await authClient.signOut()
43 navigate({ to: `/login` })
44 }
45
46 const handleCreateProject = () => {
47 if (newProjectName.trim() && session) {
48 projectCollection.insert({
49 id: Math.floor(Math.random() * 100000),
50 name: newProjectName.trim(),
51 description: ``,
52 owner_id: session.user.id,
53 shared_user_ids: [],
54 created_at: new Date(),
55 updated_at: new Date(),
56 })
57 setNewProjectName(``)
58 setShowNewProjectForm(false)
59 }
60 }
61
62 if (isPending) {
63 return null
64 }
65
66 if (!session) {
67 return null
68 }
69
70 return (
71 <div className="min-h-screen bg-gray-50">
72 <header className="bg-white shadow border-b border-gray-200">
73 <div className="max-w-7xl mx-auto px-3 sm:px-6 lg:px-8">
74 <div className="flex justify-between items-center h-16">
75 <div className="flex-shrink-0">
76 <h1 className="text-xl font-semibold text-gray-900">
77 TanStack DB Projects Example
78 </h1>
79 </div>
80 <div className="flex items-center space-x-4">
81 <span className="text-sm text-gray-700">
82 {session.user.email}
83 </span>
84 <button
85 onClick={handleLogout}
86 className="text-sm font-medium text-gray-700 hover:text-gray-900 px-3 py-2 rounded-md hover:bg-gray-100 transition-colors"
87 >
88 Sign out
89 </button>
90 </div>
91 </div>
92 </div>
93 </header>
94 <div className="flex">
95 <aside className="w-64 bg-white shadow-sm border-r border-gray-200 min-h-screen">
96 <div className="p-4">
97 <div className="flex items-center justify-between mb-4">
98 <h2 className="text-lg font-medium text-gray-900">Projects</h2>
99 <button
100 onClick={() => setShowNewProjectForm(!showNewProjectForm)}
101 className="p-1 text-gray-500 hover:text-gray-700"
102 >
103 <svg
104 className="w-5 h-5"
105 fill="none"
106 stroke="currentColor"
107 viewBox="0 0 24 24"
108 >
109 <path
110 strokeLinecap="round"
111 strokeLinejoin="round"
112 strokeWidth={2}
113 d="M12 6v6m0 0v6m0-6h6m-6 0H6"
114 />
115 </svg>
116 </button>
117 </div>
118
119 {showNewProjectForm && (
120 <div className="mb-4 p-3 bg-gray-50 rounded-md">
121 <input
122 type="text"
123 value={newProjectName}
124 onChange={(e) => setNewProjectName(e.target.value)}
125 onKeyDown={(e) => e.key === `Enter` && handleCreateProject()}
126 placeholder="Project name"
127 className="w-full px-2 py-1 border border-gray-300 rounded text-sm"
128 />
129 <div className="flex gap-2 mt-2">
130 <button
131 onClick={handleCreateProject}
132 className="px-3 py-1 bg-blue-500 text-white rounded text-sm hover:bg-blue-600"
133 >
134 Create
135 </button>
136 <button
137 onClick={() => setShowNewProjectForm(false)}
138 className="px-3 py-1 bg-gray-300 text-gray-700 rounded text-sm hover:bg-gray-400"
139 >
140 Cancel
141 </button>
142 </div>
143 </div>
144 )}
145
146 <nav className="space-y-1">
147 {projects.map((project) => (
148 <Link
149 key={project.id}
150 to="/project/$projectId"
151 params={{ projectId: project.id.toString() }}
152 className="block px-3 py-2 text-sm font-medium text-gray-700 hover:bg-gray-100 rounded-md"
153 >
154 {project.name}
155 </Link>
156 ))}
157 </nav>
158 </div>
159 </aside>
160 <main className="flex-1">
161 <Outlet />
162 </main>
163 </div>
164 </div>
165 )
166}