Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

_authenticated.tsx

152 lines4.9 KB

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