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

_authenticated.tsx

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