2024-04-30 22:48:36 -04:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
import {
|
|
|
|
Dialog,
|
|
|
|
DialogContent,
|
|
|
|
DialogHeader,
|
|
|
|
DialogTitle,
|
|
|
|
} from "@/components/ui/dialog"
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
|
|
import { useForm } from "react-hook-form"
|
2024-10-21 13:57:45 -06:00
|
|
|
import { z } from "zod"
|
2024-04-30 22:48:36 -04:00
|
|
|
|
2024-10-21 13:57:45 -06:00
|
|
|
import { Button } from "@/components/ui/button"
|
2024-04-30 22:48:36 -04:00
|
|
|
import {
|
|
|
|
Form,
|
|
|
|
FormControl,
|
|
|
|
FormField,
|
|
|
|
FormItem,
|
|
|
|
FormMessage,
|
|
|
|
} from "@/components/ui/form"
|
|
|
|
import { Input } from "@/components/ui/input"
|
2024-05-01 01:29:16 -04:00
|
|
|
import { shareSandbox } from "@/lib/actions"
|
2024-10-21 13:57:45 -06:00
|
|
|
import { Sandbox } from "@/lib/types"
|
|
|
|
import { DialogDescription } from "@radix-ui/react-dialog"
|
|
|
|
import { Link, Loader2, UserPlus } from "lucide-react"
|
|
|
|
import { useState } from "react"
|
2024-05-01 01:29:16 -04:00
|
|
|
import { toast } from "sonner"
|
2024-05-01 02:22:02 -04:00
|
|
|
import SharedUser from "./sharedUser"
|
2024-04-30 22:48:36 -04:00
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
email: z.string().email(),
|
|
|
|
})
|
|
|
|
|
|
|
|
export default function ShareSandboxModal({
|
|
|
|
open,
|
|
|
|
setOpen,
|
|
|
|
data,
|
2024-05-01 01:53:49 -04:00
|
|
|
shared,
|
2024-04-30 22:48:36 -04:00
|
|
|
}: {
|
|
|
|
open: boolean
|
|
|
|
setOpen: (open: boolean) => void
|
|
|
|
data: Sandbox
|
2024-05-01 01:53:49 -04:00
|
|
|
shared: {
|
|
|
|
id: string
|
|
|
|
name: string
|
2024-11-10 23:40:10 -05:00
|
|
|
avatarUrl: string
|
2024-05-01 01:53:49 -04:00
|
|
|
}[]
|
2024-04-30 22:48:36 -04:00
|
|
|
}) {
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
defaultValues: {
|
|
|
|
email: "",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
async function onSubmit(values: z.infer<typeof formSchema>) {
|
2024-05-01 01:29:16 -04:00
|
|
|
setLoading(true)
|
|
|
|
const res = await shareSandbox(data.id, values.email)
|
2024-05-01 01:53:49 -04:00
|
|
|
if (!res.success) {
|
|
|
|
toast.error(res.message)
|
|
|
|
} else {
|
|
|
|
toast.success("Shared successfully.")
|
2024-05-01 01:29:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setLoading(false)
|
2024-04-30 22:48:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
|
|
<DialogContent className="p-0">
|
2024-05-01 02:22:02 -04:00
|
|
|
<div className={`p-6 ${shared.length > 0 ? "pb-3" : null} space-y-6`}>
|
2024-04-30 22:48:36 -04:00
|
|
|
<DialogHeader>
|
|
|
|
<DialogTitle>Share Sandbox</DialogTitle>
|
2024-05-01 02:22:02 -04:00
|
|
|
{data.visibility === "private" ? (
|
|
|
|
<DialogDescription className="text-sm text-muted-foreground">
|
|
|
|
This sandbox is private. Making it public will allow shared
|
2024-05-26 18:37:36 -07:00
|
|
|
users to view and collaborate. You can still share & manage
|
|
|
|
access below.
|
2024-05-01 02:22:02 -04:00
|
|
|
</DialogDescription>
|
|
|
|
) : null}
|
2024-04-30 22:48:36 -04:00
|
|
|
</DialogHeader>
|
2024-05-06 22:59:49 -07:00
|
|
|
<div className="flex space-x-4 w-full">
|
|
|
|
<Form {...form}>
|
2024-05-26 18:37:36 -07:00
|
|
|
<form
|
2024-05-26 21:41:20 -07:00
|
|
|
autoComplete="off"
|
2024-05-26 18:37:36 -07:00
|
|
|
onSubmit={form.handleSubmit(onSubmit)}
|
|
|
|
className="flex w-full"
|
|
|
|
>
|
2024-05-06 22:59:49 -07:00
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="email"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem className="mr-4 w-full">
|
|
|
|
<FormControl>
|
|
|
|
<Input
|
|
|
|
placeholder="yourfriend@domain.com"
|
|
|
|
{...field}
|
|
|
|
className="w-full"
|
|
|
|
/>
|
|
|
|
</FormControl>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<Button disabled={loading} type="submit" className="">
|
|
|
|
{loading ? (
|
|
|
|
<>
|
2024-05-26 18:37:36 -07:00
|
|
|
<Loader2 className="animate-spin mr-2 h-4 w-4" />{" "}
|
|
|
|
Loading...
|
2024-05-06 22:59:49 -07:00
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<UserPlus className="mr-2 h-4 w-4" /> Share
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
</form>
|
|
|
|
</Form>
|
2024-05-26 18:37:36 -07:00
|
|
|
<Button
|
|
|
|
onClick={() => {
|
|
|
|
navigator.clipboard.writeText(
|
|
|
|
`${process.env.NEXT_PUBLIC_APP_URL}/code/${data.id}`
|
|
|
|
)
|
|
|
|
toast.success("Link copied to clipboard.")
|
|
|
|
}}
|
|
|
|
size="icon"
|
|
|
|
disabled={loading}
|
|
|
|
variant="outline"
|
|
|
|
className="shrink-0"
|
|
|
|
>
|
|
|
|
<Link className="h-4 w-4" />
|
2024-05-06 22:59:49 -07:00
|
|
|
</Button>
|
|
|
|
</div>
|
2024-04-30 22:48:36 -04:00
|
|
|
</div>
|
2024-05-01 02:22:02 -04:00
|
|
|
{shared.length > 0 ? (
|
|
|
|
<>
|
|
|
|
<div className="w-full h-[1px] mb- bg-border" />
|
|
|
|
<div className="p-6 pt-3">
|
|
|
|
<DialogHeader className="mb-6">
|
|
|
|
<DialogTitle>Manage Access</DialogTitle>
|
|
|
|
</DialogHeader>
|
|
|
|
<div className="space-y-2">
|
|
|
|
{shared.map((user) => (
|
2024-11-10 23:40:10 -05:00
|
|
|
<SharedUser
|
|
|
|
key={user.id}
|
|
|
|
user={user}
|
|
|
|
sandboxId={data.id}
|
|
|
|
/>
|
2024-05-01 02:22:02 -04:00
|
|
|
))}
|
2024-04-30 22:48:36 -04:00
|
|
|
</div>
|
2024-05-01 02:22:02 -04:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : null}
|
2024-04-30 22:48:36 -04:00
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
)
|
|
|
|
}
|