Skip to content

Commit 1b4c7ac

Browse files
committed
live share
1 parent 1ee31d4 commit 1b4c7ac

File tree

3 files changed

+83
-87
lines changed

3 files changed

+83
-87
lines changed

apps/postgres-new/app/(main)/db/[id]/page.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Workspace from '~/components/workspace'
88
export default function Page({ params }: { params: { id: string } }) {
99
const databaseId = params.id
1010
const router = useRouter()
11-
const { dbManager, liveShare } = useApp()
11+
const { dbManager } = useApp()
1212

1313
useEffect(() => {
1414
async function run() {
@@ -25,12 +25,5 @@ export default function Page({ params }: { params: { id: string } }) {
2525
run()
2626
}, [dbManager, databaseId, router])
2727

28-
// Cleanup live shared database when switching databases
29-
useEffect(() => {
30-
if (liveShare.isLiveSharing && liveShare.databaseId !== databaseId) {
31-
liveShare.stop()
32-
}
33-
}, [liveShare, databaseId])
34-
3528
return <Workspace databaseId={databaseId} visibility="local" />
3629
}

apps/postgres-new/components/chat.tsx

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ export default function Chat() {
245245
)}
246246
ref={scrollRef}
247247
>
248-
<LiveShareOverlay />
248+
<LiveShareOverlay databaseId={databaseId} />
249249
<m.div
250250
key={databaseId}
251251
className="flex flex-col gap-4 w-full max-w-4xl p-10"
@@ -334,7 +334,7 @@ export default function Chat() {
334334
<div className="h-full w-full max-w-4xl flex flex-col gap-10 justify-center items-center">
335335
{user ? (
336336
<>
337-
<LiveShareOverlay />
337+
<LiveShareOverlay databaseId={databaseId} />
338338
<m.h3
339339
layout
340340
className="text-2xl font-light text-center"
@@ -533,76 +533,76 @@ export default function Chat() {
533533
)
534534
}
535535

536-
function LiveShareOverlay() {
536+
function LiveShareOverlay(props: { databaseId: string }) {
537537
const { liveShare } = useApp()
538538

539-
if (!liveShare.isLiveSharing) {
540-
return null
541-
}
542-
543-
return (
544-
<div className="h-full w-full max-w-4xl flex flex-col gap-10 p-10 absolute backdrop-blur-sm bg-card/90 z-10">
545-
<div className="flex items-center justify-center h-full flex-col gap-y-7">
546-
<div className="w-full text-left">
547-
<p className="text-lg">Access your in-browser database</p>
548-
<p className="text-xs text-muted-foreground">
549-
Closing the window will stop the Live Share session
550-
</p>
539+
if (liveShare.isLiveSharing && liveShare.databaseId === props.databaseId) {
540+
return (
541+
<div className="h-full w-full max-w-4xl flex flex-col gap-10 p-10 absolute backdrop-blur-sm bg-card/90 z-10">
542+
<div className="flex items-center justify-center h-full flex-col gap-y-7">
543+
<div className="w-full text-left">
544+
<p className="text-lg">Access your in-browser database</p>
545+
<p className="text-xs text-muted-foreground">
546+
Closing the window will stop the Live Share session
547+
</p>
548+
</div>
549+
<Tabs defaultValue="uri" className="w-full justify-between bg-muted/50 rounded-md border">
550+
<TabsList className="w-full flex justify-start bg-transparent px-3">
551+
<TabsTrigger
552+
value="uri"
553+
className="hover:text-foreground data-[state=active]:border-b-2 data-[state=active]:border-foreground data-[state=active]:bg-none! data-[state=active]:text-foreground data-[state=active]:shadow-none rounded-none relative cursor-pointer text-foreground-lighter flex items-center space-x-2 text-center transition focus:outline-none focus-visible:ring focus-visible:ring-foreground-muted focus-visible:border-foreground-muted text-xs px-2.5 py-1"
554+
>
555+
URI
556+
</TabsTrigger>
557+
<TabsTrigger
558+
value="psql"
559+
className="hover:text-foreground data-[state=active]:border-b-2 data-[state=active]:border-foreground data-[state=active]:bg-none! data-[state=active]:text-foreground data-[state=active]:shadow-none rounded-none relative cursor-pointer text-foreground-lighter flex items-center space-x-2 text-center transition focus:outline-none focus-visible:ring focus-visible:ring-foreground-muted focus-visible:border-foreground-muted text-xs px-2.5 py-1"
560+
>
561+
PSQL
562+
</TabsTrigger>
563+
</TabsList>
564+
<TabsContent value="uri" className="px-2 pb-2">
565+
<CopyableField
566+
value={`postgres://postgres@${liveShare.databaseId}.${process.env.NEXT_PUBLIC_BROWSER_PROXY_DOMAIN}/postgres?sslmode=require`}
567+
/>
568+
</TabsContent>
569+
<TabsContent value="psql" className="px-2 pb-2">
570+
<CopyableField
571+
value={`psql "postgres://postgres@${liveShare.databaseId}.${process.env.NEXT_PUBLIC_BROWSER_PROXY_DOMAIN}/postgres?sslmode=require"`}
572+
/>
573+
</TabsContent>
574+
</Tabs>
575+
576+
{liveShare.clientIp ? (
577+
<p className="text-sm text-muted-foreground flex items-center gap-2">
578+
<span className="relative flex h-3 w-3">
579+
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-primary opacity-75"></span>
580+
<span className="relative inline-flex rounded-full h-3 w-3 bg-primary"></span>
581+
</span>
582+
<span>
583+
Connected from <span className="text-card-foreground">{liveShare.clientIp}</span>
584+
</span>
585+
</p>
586+
) : (
587+
<p className="text-sm text-muted-foreground flex items-center gap-2">
588+
<span className="relative inline-flex rounded-full h-3 w-3 bg-muted-foreground"></span>
589+
<span>No client connected</span>
590+
</p>
591+
)}
592+
<Button
593+
className="w-full gap-2"
594+
variant="outline"
595+
onClick={() => {
596+
liveShare.stop()
597+
}}
598+
>
599+
<PlugIcon size={16} />
600+
<span>Stop sharing database</span>
601+
</Button>
551602
</div>
552-
<Tabs defaultValue="uri" className="w-full justify-between bg-muted/50 rounded-md border">
553-
<TabsList className="w-full flex justify-start bg-transparent px-3">
554-
<TabsTrigger
555-
value="uri"
556-
className="hover:text-foreground data-[state=active]:border-b-2 data-[state=active]:border-foreground data-[state=active]:bg-none! data-[state=active]:text-foreground data-[state=active]:shadow-none rounded-none relative cursor-pointer text-foreground-lighter flex items-center space-x-2 text-center transition focus:outline-none focus-visible:ring focus-visible:ring-foreground-muted focus-visible:border-foreground-muted text-xs px-2.5 py-1"
557-
>
558-
URI
559-
</TabsTrigger>
560-
<TabsTrigger
561-
value="psql"
562-
className="hover:text-foreground data-[state=active]:border-b-2 data-[state=active]:border-foreground data-[state=active]:bg-none! data-[state=active]:text-foreground data-[state=active]:shadow-none rounded-none relative cursor-pointer text-foreground-lighter flex items-center space-x-2 text-center transition focus:outline-none focus-visible:ring focus-visible:ring-foreground-muted focus-visible:border-foreground-muted text-xs px-2.5 py-1"
563-
>
564-
PSQL
565-
</TabsTrigger>
566-
</TabsList>
567-
<TabsContent value="uri" className="px-2 pb-2">
568-
<CopyableField
569-
value={`postgres://postgres@${liveShare.databaseId}.${process.env.NEXT_PUBLIC_BROWSER_PROXY_DOMAIN}/postgres?sslmode=require`}
570-
/>
571-
</TabsContent>
572-
<TabsContent value="psql" className="px-2 pb-2">
573-
<CopyableField
574-
value={`psql "postgres://postgres@${liveShare.databaseId}.${process.env.NEXT_PUBLIC_BROWSER_PROXY_DOMAIN}/postgres?sslmode=require"`}
575-
/>
576-
</TabsContent>
577-
</Tabs>
578-
579-
{liveShare.clientIp ? (
580-
<p className="text-sm text-muted-foreground flex items-center gap-2">
581-
<span className="relative flex h-3 w-3">
582-
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-primary opacity-75"></span>
583-
<span className="relative inline-flex rounded-full h-3 w-3 bg-primary"></span>
584-
</span>
585-
<span>
586-
Connected from <span className="text-card-foreground">{liveShare.clientIp}</span>
587-
</span>
588-
</p>
589-
) : (
590-
<p className="text-sm text-muted-foreground flex items-center gap-2">
591-
<span className="relative inline-flex rounded-full h-3 w-3 bg-muted-foreground"></span>
592-
<span>No client connected</span>
593-
</p>
594-
)}
595-
<Button
596-
className="w-full gap-2"
597-
variant="outline"
598-
onClick={() => {
599-
liveShare.stop()
600-
}}
601-
>
602-
<PlugIcon size={16} />
603-
<span>Stop sharing database</span>
604-
</Button>
605603
</div>
606-
</div>
607-
)
604+
)
605+
606+
return null
607+
}
608608
}

apps/postgres-new/components/sidebar.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ function DatabaseMenuItem({ database, isActive }: DatabaseMenuItemProps) {
372372
)}
373373
href={`/db/${database.id}`}
374374
>
375-
{isActive && liveShare.isLiveSharing && (
375+
{liveShare.isLiveSharing && liveShare.databaseId === database.id && (
376376
<Tooltip>
377377
<TooltipTrigger asChild>
378378
<RadioIcon size={18} />
@@ -545,34 +545,37 @@ type ConnectMenuItemProps = {
545545
function LiveShareMenuItem(props: ConnectMenuItemProps) {
546546
const { liveShare, user } = useApp()
547547

548-
if (!liveShare.isLiveSharing) {
548+
if (liveShare.isLiveSharing && liveShare.databaseId === props.databaseId) {
549549
return (
550550
<DropdownMenuItem
551-
disabled={!user}
552551
className="bg-inherit justify-start hover:bg-neutral-200 flex gap-3"
553552
onClick={async (e) => {
554553
e.preventDefault()
555-
liveShare.start(props.databaseId)
554+
liveShare.stop()
556555
props.setIsPopoverOpen(false)
557556
}}
558557
>
559-
<LiveShareIcon size={16} className="flex-shrink-0 text-muted-foreground" />
560-
<span>Live Share</span>
558+
<PlugIcon size={16} strokeWidth={2} className="flex-shrink-0 text-muted-foreground" />
559+
<span>Stop sharing</span>
561560
</DropdownMenuItem>
562561
)
563562
}
564563

565564
return (
566565
<DropdownMenuItem
566+
disabled={!user}
567567
className="bg-inherit justify-start hover:bg-neutral-200 flex gap-3"
568568
onClick={async (e) => {
569569
e.preventDefault()
570-
liveShare.stop()
570+
if (liveShare.isLiveSharing) {
571+
liveShare.stop()
572+
}
573+
liveShare.start(props.databaseId)
571574
props.setIsPopoverOpen(false)
572575
}}
573576
>
574-
<PlugIcon size={16} strokeWidth={2} className="flex-shrink-0 text-muted-foreground" />
575-
<span>Stop sharing</span>
577+
<LiveShareIcon size={16} className="flex-shrink-0 text-muted-foreground" />
578+
<span>Live Share</span>
576579
</DropdownMenuItem>
577580
)
578581
}

0 commit comments

Comments
 (0)