133 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-04-30 22:48:36 -04:00
"use client"
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog"
import { z } from "zod"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import {
Form,
FormControl,
FormField,
FormItem,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Loader2, UserPlus, X } from "lucide-react"
2024-05-01 02:22:02 -04:00
import { useState } from "react"
import { Sandbox } from "@/lib/types"
2024-04-30 22:48:36 -04:00
import { Button } from "@/components/ui/button"
2024-05-01 01:29:16 -04:00
import { shareSandbox } from "@/lib/actions"
import { toast } from "sonner"
2024-05-01 02:22:02 -04:00
import SharedUser from "./sharedUser"
import { DialogDescription } from "@radix-ui/react-dialog"
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-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
users to view and collaborate.
</DialogDescription>
) : null}
2024-04-30 22:48:36 -04:00
</DialogHeader>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="flex">
<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 ? (
<>
<Loader2 className="animate-spin mr-2 h-4 w-4" /> Loading...
</>
) : (
<>
<UserPlus className="mr-2 h-4 w-4" /> Share
</>
)}
</Button>
</form>
</Form>
</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) => (
<SharedUser key={user.id} user={user} sandboxId={data.id} />
))}
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>
)
}