-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathsidebar.tsx
591 lines (566 loc) · 20 KB
/
sidebar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
'use client'
import { TooltipPortal } from '@radix-ui/react-tooltip'
import { AnimatePresence, m } from 'framer-motion'
import {
ArrowLeftToLine,
ArrowRightToLine,
Database as DbIcon,
Download,
Loader,
LogOut,
MoreVertical,
PackagePlus,
Pencil,
PlugIcon,
RadioIcon,
Trash2,
Upload,
} from 'lucide-react'
import Link from 'next/link'
import { useParams, useRouter } from 'next/navigation'
import { useState } from 'react'
import { Button } from '~/components/ui/button'
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '~/components/ui/dialog'
import { Tooltip, TooltipContent, TooltipTrigger } from '~/components/ui/tooltip'
import { useDatabaseDeleteMutation } from '~/data/databases/database-delete-mutation'
import { useDatabaseUpdateMutation } from '~/data/databases/database-update-mutation'
import { useDatabasesQuery } from '~/data/databases/databases-query'
import { useDeployWaitlistCreateMutation } from '~/data/deploy-waitlist/deploy-waitlist-create-mutation'
import { useIsOnDeployWaitlistQuery } from '~/data/deploy-waitlist/deploy-waitlist-query'
import { Database } from '~/lib/db'
import { downloadFile, titleToKebabCase } from '~/lib/util'
import { cn } from '~/lib/utils'
import { useApp } from './app-provider'
import { CodeBlock } from './code-block'
import { LiveShareIcon } from './live-share-icon'
import { useIsLocked } from './lock-provider'
import SignInButton from './sign-in-button'
import ThemeDropdown from './theme-dropdown'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from './ui/dropdown-menu'
export default function Sidebar() {
const {
user,
signOut,
focusRef,
isSignInDialogOpen,
setIsSignInDialogOpen,
setIsRenameDialogOpen,
isLegacyDomain,
liveShare,
} = useApp()
let { id: currentDatabaseId } = useParams<{ id: string }>()
const router = useRouter()
const { data: databases, isLoading: isLoadingDatabases } = useDatabasesQuery()
const [showSidebar, setShowSidebar] = useState(true)
return (
<>
<Dialog
open={isSignInDialogOpen}
onOpenChange={(open) => {
setIsSignInDialogOpen(open)
}}
>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>Sign in to create a database</DialogTitle>
<div className="py-2 border-b" />
</DialogHeader>
<h2 className="font-bold">Why do I need to sign in?</h2>
<p>
Even though your Postgres databases run{' '}
<a
className="underline"
href="https://pglite.dev"
target="_blank"
rel="noopener noreferrer"
>
directly in the browser
</a>
, we still need to connect to an API that runs the large language model (required for
all database interactions).
</p>
<p>We ask you to sign in to prevent API abuse.</p>
<div className="flex justify-center items-center my-3">
<SignInButton />
</div>
</DialogContent>
</Dialog>
<AnimatePresence initial={false} mode="popLayout">
{showSidebar && (
<m.div
className="max-w-72 w-full h-full flex flex-col gap-2 items-stretch p-4 bg-card"
variants={{
hidden: { opacity: 0, x: '-100%' },
show: { opacity: 1, x: 0 },
}}
transition={{ duration: 0.25 }}
initial="hidden"
animate="show"
exit={{ opacity: 0, transition: { duration: 0 } }}
>
<div className="flex justify-between">
<Tooltip>
<TooltipTrigger asChild>
<m.div layout="position" layoutId="sidebar-collapse">
<Button
variant={'ghost'}
size={'icon'}
onClick={() => {
setShowSidebar(false)
}}
>
<ArrowLeftToLine size={14} />
</Button>
</m.div>
</TooltipTrigger>
<TooltipContent side="right">
<p>Close sidebar</p>
</TooltipContent>
</Tooltip>
<m.div layout="position" layoutId="new-database-button">
<Button
onClick={() => {
if (!user) {
setIsSignInDialogOpen(true)
} else {
if (liveShare.isLiveSharing) {
liveShare.stop()
}
router.push('/')
focusRef.current?.focus()
}
}}
className="gap-2"
>
<PackagePlus size={14} />
New database
</Button>
</m.div>
</div>
{databases && databases.length > 0 ? (
<m.div
className="flex-1 flex flex-col items-stretch overflow-y-auto overflow-x-hidden"
transition={{ staggerChildren: 0.03 }}
initial="hidden"
animate="show"
>
{databases.map((database) => (
<m.div
key={database.id}
layout="position"
layoutId={`database-menu-item-${database.id}`}
variants={{
hidden: { opacity: 0, x: -20 },
show: { opacity: 1, x: 0 },
}}
>
<DatabaseMenuItem
database={database}
isActive={database.id === currentDatabaseId}
/>
</m.div>
))}
</m.div>
) : (
<div className="flex-1 flex flex-col gap-2 my-10 mx-5 items-center text-base text-neutral-400 opacity-75">
{isLoadingDatabases ? (
<Loader className="animate-spin" size={48} strokeWidth={0.75} />
) : (
<>
<DbIcon size={48} strokeWidth={0.75} />
<span>No databases</span>
{!isLegacyDomain && (
<a
className="mt-2 underline cursor-pointer text-xs text-primary/50"
onClick={() => setIsRenameDialogOpen(true)}
>
Where did my databases go?
</a>
)}
</>
)}
</div>
)}
<m.div layout="position" layoutId="theme-dropdown">
<ThemeDropdown className="w-full" />
</m.div>
{user && (
<m.div layout="position" layoutId="sign-out-button">
<Button
className="w-full gap-2"
variant="secondary"
onClick={async () => {
await signOut()
}}
>
<LogOut size={18} strokeWidth={2} />
Sign out
</Button>
</m.div>
)}
</m.div>
)}
</AnimatePresence>
{!showSidebar && (
<div className="flex flex-col pl-4 py-4 justify-between">
<div className="flex flex-col gap-2">
<Tooltip>
<TooltipTrigger asChild>
<m.div layout="position" layoutId="sidebar-collapse">
<Button
variant={'ghost'}
size="icon"
onClick={() => {
setShowSidebar(true)
}}
>
<ArrowRightToLine size={14} />
</Button>
</m.div>
</TooltipTrigger>
<TooltipContent side="right">
<p>Open sidebar</p>
</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger asChild>
<m.div layout="position" layoutId="new-database-button">
<Button
size={'icon'}
onClick={() => {
if (!user) {
setIsSignInDialogOpen(true)
} else {
router.push('/')
focusRef.current?.focus()
}
}}
>
<PackagePlus size={14} />
</Button>
</m.div>
</TooltipTrigger>
<TooltipContent side="right">
<p>New database</p>
</TooltipContent>
</Tooltip>
</div>
<div className="flex flex-col gap-2">
<Tooltip>
<TooltipTrigger asChild>
<m.div layout="position" layoutId="theme-dropdown">
<ThemeDropdown iconOnly />
</m.div>
</TooltipTrigger>
<TooltipContent side="right">
<p>Toggle theme</p>
</TooltipContent>
</Tooltip>
{user && (
<Tooltip>
<TooltipTrigger asChild>
<m.div layout="position" layoutId="sign-out-button">
<Button
size={'icon'}
variant="secondary"
onClick={async () => {
await signOut()
}}
>
<LogOut size={16} strokeWidth={2} />
</Button>
</m.div>
</TooltipTrigger>
<TooltipContent side="right">
<p>Sign out</p>
</TooltipContent>
</Tooltip>
)}
</div>
</div>
)}
</>
)
}
type DatabaseMenuItemProps = {
database: Database
isActive: boolean
}
function DatabaseMenuItem({ database, isActive }: DatabaseMenuItemProps) {
const router = useRouter()
const { user, dbManager, liveShare } = useApp()
const [isPopoverOpen, setIsPopoverOpen] = useState(false)
const { mutateAsync: deleteDatabase } = useDatabaseDeleteMutation()
const { mutateAsync: updateDatabase } = useDatabaseUpdateMutation()
const [isRenaming, setIsRenaming] = useState(false)
const [isDeployDialogOpen, setIsDeployDialogOpen] = useState(false)
const { data: isOnDeployWaitlist } = useIsOnDeployWaitlistQuery()
const { mutateAsync: joinDeployWaitlist } = useDeployWaitlistCreateMutation()
const isLocked = useIsLocked(database.id, true)
return (
<>
<Dialog
open={isDeployDialogOpen}
onOpenChange={(open) => {
setIsDeployDialogOpen(open)
}}
>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>Deployments are in Private Alpha</DialogTitle>
<div className="py-2 border-b" />
</DialogHeader>
<h2 className="font-bold">What are deployments?</h2>
<p>
Deploy your database to a serverless PGlite instance so that it can be accessed outside
the browser using any Postgres client:
</p>
<CodeBlock
className="language-curl bg-neutral-800"
language="curl"
hideLineNumbers
theme="dark"
>
{`psql "postgres://postgres:<password>@<your-unique-server>/postgres"`}
</CodeBlock>
<div className="flex justify-center items-center mt-3">
<AnimatePresence initial={false}>
{!isOnDeployWaitlist ? (
<button
className="px-4 py-3 bg-foreground text-background rounded-md"
onClick={async () => {
await joinDeployWaitlist()
}}
>
Join Private Alpha
</button>
) : (
<m.div
className="px-4 py-3 border-2 rounded-md text-center border-dashed"
variants={{
hidden: { scale: 0 },
show: { scale: 1 },
}}
initial="hidden"
animate="show"
>
<h3 className="font-medium mb-2">🎉 You're on the waitlist!</h3>
<p>We'll send you an email when you have access to deploy.</p>
</m.div>
)}
</AnimatePresence>
</div>
</DialogContent>
</Dialog>
<Link
data-active={isActive || isPopoverOpen}
className={cn(
'group text-sm w-full relative justify-start bg-card hover:bg-muted/50 flex gap-2 px-3 h-10 items-center rounded-md overflow-hidden data-[active=true]:bg-accent transition'
)}
href={`/db/${database.id}`}
>
{liveShare.isLiveSharing && liveShare.databaseId === database.id && (
<Tooltip>
<TooltipTrigger asChild>
<RadioIcon size={18} />
</TooltipTrigger>
<TooltipPortal>
<TooltipContent side="bottom">
<p>Shared</p>
</TooltipContent>
</TooltipPortal>
</Tooltip>
)}
<span className="text-nowrap grow truncate">{database.name ?? 'My database'}</span>
<DropdownMenu
modal={false}
onOpenChange={(open) => {
setIsPopoverOpen(open)
if (!open) {
setIsRenaming(false)
}
}}
open={isPopoverOpen}
>
<DropdownMenuTrigger
className="group/trigger outline-none"
onClick={(e) => {
e.preventDefault()
setIsPopoverOpen(true)
}}
>
<MoreVertical
size={16}
className={cn(
isActive
? 'text-muted-foreground'
: 'text-transparent group-hover:text-muted-foreground focus-visible:text-muted-foreground group-focus/trigger:text-muted-foreground',
'group-data-[state=open]/trigger:text-foreground',
'transition'
)}
/>
</DropdownMenuTrigger>
<DropdownMenuContent side="right" align="start">
{isRenaming ? (
<form
className="w-72"
onSubmit={async (e) => {
e.preventDefault()
if (e.target instanceof HTMLFormElement) {
const formData = new FormData(e.target)
const name = formData.get('name')
if (typeof name === 'string') {
await updateDatabase({ ...database, name })
}
}
setIsPopoverOpen(false)
setIsRenaming(false)
}}
>
<input
name="name"
className="flex-grow w-full p-2 outline-none text-base bg-inherit placeholder:text-neutral-400"
placeholder={`Rename ${database.name}`}
defaultValue={database.name ?? undefined}
autoComplete="off"
autoFocus
/>
</form>
) : (
<div className="flex flex-col items-stretch w-32">
<DropdownMenuItem
disabled={isLocked}
className="gap-3"
onSelect={async (e) => {
e.preventDefault()
setIsRenaming(true)
}}
>
<Pencil
size={16}
strokeWidth={2}
className="flex-shrink-0 text-muted-foreground"
/>
<span>Rename</span>
</DropdownMenuItem>
<DropdownMenuItem
disabled={isLocked}
className="gap-3"
onSelect={async (e) => {
e.preventDefault()
if (!dbManager) {
throw new Error('dbManager is not available')
}
const db = await dbManager.getDbInstance(database.id)
const dumpBlob = await db.dumpDataDir()
const fileName = `${titleToKebabCase(database.name ?? 'My Database')}-${Date.now()}`
const file = new File([dumpBlob], fileName, { type: dumpBlob.type })
downloadFile(file)
}}
>
<Download
size={16}
strokeWidth={2}
className="flex-shrink-0 text-muted-foreground"
/>
<span>Download</span>
</DropdownMenuItem>
<DropdownMenuItem
className="bg-inherit justify-start hover:bg-neutral-200 flex gap-3"
onClick={async (e) => {
e.preventDefault()
setIsDeployDialogOpen(true)
setIsPopoverOpen(false)
}}
disabled={user === undefined || isLocked}
>
<Upload
size={16}
strokeWidth={2}
className="flex-shrink-0 text-muted-foreground"
/>
<span>Deploy</span>
</DropdownMenuItem>
<LiveShareMenuItem
databaseId={database.id}
isActive={isActive}
setIsPopoverOpen={setIsPopoverOpen}
disabled={user === undefined || isLocked}
/>
<DropdownMenuSeparator />
<DropdownMenuItem
disabled={isLocked}
className="gap-3"
onSelect={async (e) => {
e.preventDefault()
setIsPopoverOpen(false)
await deleteDatabase({ id: database.id })
if (isActive) {
router.push('/')
}
}}
>
<Trash2
size={16}
strokeWidth={2}
className="flex-shrink-0 text-muted-foreground"
/>
<span>Delete</span>
</DropdownMenuItem>
</div>
)}
</DropdownMenuContent>
</DropdownMenu>
</Link>
</>
)
}
type ConnectMenuItemProps = {
databaseId: string
isActive: boolean
disabled?: boolean
setIsPopoverOpen: (open: boolean) => void
}
function LiveShareMenuItem(props: ConnectMenuItemProps) {
const { liveShare, user } = useApp()
const router = useRouter()
if (liveShare.isLiveSharing && liveShare.databaseId === props.databaseId) {
return (
<DropdownMenuItem
className="bg-inherit justify-start hover:bg-neutral-200 flex gap-3"
onClick={async (e) => {
e.preventDefault()
liveShare.stop()
props.setIsPopoverOpen(false)
}}
>
<PlugIcon size={16} strokeWidth={2} className="flex-shrink-0 text-muted-foreground" />
<span>Stop sharing</span>
</DropdownMenuItem>
)
}
return (
<DropdownMenuItem
disabled={props.disabled}
className="bg-inherit justify-start hover:bg-neutral-200 flex gap-3"
onClick={async (e) => {
e.preventDefault()
if (liveShare.isLiveSharing) {
liveShare.stop()
}
liveShare.start(props.databaseId, { captureMigrations: true })
router.push(`/db/${props.databaseId}`)
props.setIsPopoverOpen(false)
}}
>
<LiveShareIcon size={16} className="flex-shrink-0 text-muted-foreground" />
<span>Live Share</span>
</DropdownMenuItem>
)
}