2024-04-23 01:53:37 -04:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
import {
|
|
|
|
Dialog,
|
|
|
|
DialogContent,
|
|
|
|
DialogDescription,
|
|
|
|
DialogHeader,
|
|
|
|
DialogTitle,
|
|
|
|
DialogTrigger,
|
|
|
|
} from "@/components/ui/dialog"
|
|
|
|
import Image from "next/image"
|
|
|
|
import { useState } from "react"
|
2024-04-27 16:22:35 -04:00
|
|
|
import { set, z } from "zod"
|
2024-04-23 01:53:37 -04:00
|
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
|
|
import { useForm } from "react-hook-form"
|
|
|
|
|
|
|
|
import {
|
|
|
|
Form,
|
|
|
|
FormControl,
|
|
|
|
FormDescription,
|
|
|
|
FormField,
|
|
|
|
FormItem,
|
|
|
|
FormLabel,
|
|
|
|
FormMessage,
|
|
|
|
} from "@/components/ui/form"
|
|
|
|
import { Input } from "@/components/ui/input"
|
|
|
|
import {
|
|
|
|
Select,
|
|
|
|
SelectContent,
|
|
|
|
SelectItem,
|
|
|
|
SelectTrigger,
|
|
|
|
SelectValue,
|
|
|
|
} from "@/components/ui/select"
|
2024-04-27 16:22:35 -04:00
|
|
|
import { useUser } from "@clerk/nextjs"
|
|
|
|
import { createSandbox } from "@/lib/actions"
|
|
|
|
import { useRouter } from "next/navigation"
|
|
|
|
import { Loader2 } from "lucide-react"
|
2024-04-30 22:48:36 -04:00
|
|
|
import { Button } from "../ui/button"
|
2024-04-23 01:53:37 -04:00
|
|
|
|
|
|
|
type TOptions = "react" | "node"
|
|
|
|
|
|
|
|
const data: {
|
|
|
|
id: TOptions
|
|
|
|
name: string
|
|
|
|
icon: string
|
|
|
|
description: string
|
|
|
|
}[] = [
|
|
|
|
{
|
|
|
|
id: "react",
|
|
|
|
name: "React",
|
|
|
|
icon: "/project-icons/react.svg",
|
|
|
|
description: "A JavaScript library for building user interfaces",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "node",
|
|
|
|
name: "Node",
|
|
|
|
icon: "/project-icons/node.svg",
|
|
|
|
description: "A JavaScript runtime built on the V8 JavaScript engine",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
const formSchema = z.object({
|
2024-05-06 21:29:25 -07:00
|
|
|
name: z.string().min(1).max(16)
|
|
|
|
.refine((value) => /^[a-zA-Z0-9_]+$/.test(value), "Name must be alphanumeric and can contain underscores"),
|
2024-04-23 01:53:37 -04:00
|
|
|
visibility: z.enum(["public", "private"]),
|
|
|
|
})
|
|
|
|
|
|
|
|
export default function NewProjectModal({
|
|
|
|
open,
|
|
|
|
setOpen,
|
|
|
|
}: {
|
|
|
|
open: boolean
|
|
|
|
setOpen: (open: boolean) => void
|
|
|
|
}) {
|
|
|
|
const [selected, setSelected] = useState<TOptions>("react")
|
2024-04-27 16:22:35 -04:00
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const router = useRouter()
|
|
|
|
|
|
|
|
const user = useUser()
|
2024-04-23 01:53:37 -04:00
|
|
|
|
|
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
|
|
resolver: zodResolver(formSchema),
|
|
|
|
defaultValues: {
|
|
|
|
name: "",
|
|
|
|
visibility: "public",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2024-04-27 16:22:35 -04:00
|
|
|
async function onSubmit(values: z.infer<typeof formSchema>) {
|
|
|
|
if (!user.isSignedIn) return
|
|
|
|
|
|
|
|
const sandboxData = { type: selected, userId: user.user.id, ...values }
|
|
|
|
setLoading(true)
|
|
|
|
|
|
|
|
const id = await createSandbox(sandboxData)
|
|
|
|
router.push(`/code/${id}`)
|
2024-04-23 01:53:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Dialog open={open} onOpenChange={setOpen}>
|
|
|
|
<DialogContent>
|
|
|
|
<DialogHeader>
|
|
|
|
<DialogTitle>Create A Sandbox</DialogTitle>
|
|
|
|
</DialogHeader>
|
|
|
|
<div className="grid grid-cols-2 w-full gap-2 mt-2">
|
|
|
|
{data.map((item) => (
|
|
|
|
<button
|
|
|
|
key={item.id}
|
|
|
|
onClick={() => setSelected(item.id)}
|
|
|
|
className={`${
|
|
|
|
selected === item.id ? "border-foreground" : "border-border"
|
|
|
|
} rounded-md border bg-card text-card-foreground shadow text-left p-4 flex flex-col transition-all focus-visible:outline-none focus-visible:ring-offset-2 focus-visible:ring-offset-background focus-visible:ring-2 focus-visible:ring-ring`}
|
|
|
|
>
|
|
|
|
<div className="space-x-2 flex items-center justify-start w-full">
|
|
|
|
<Image alt="" src={item.icon} width={20} height={20} />
|
|
|
|
<div className="font-medium">{item.name}</div>
|
|
|
|
</div>
|
|
|
|
<div className="mt-2 text-muted-foreground text-sm">
|
|
|
|
{item.description}
|
|
|
|
</div>
|
|
|
|
</button>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Form {...form}>
|
|
|
|
<form onSubmit={form.handleSubmit(onSubmit)}>
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="name"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem className="mb-4">
|
|
|
|
<FormLabel>Name</FormLabel>
|
|
|
|
<FormControl>
|
|
|
|
<Input placeholder="My Project" {...field} />
|
|
|
|
</FormControl>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="visibility"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem className="mb-8">
|
|
|
|
<FormLabel>Visibility</FormLabel>
|
|
|
|
<Select
|
|
|
|
onValueChange={field.onChange}
|
|
|
|
defaultValue={field.value}
|
|
|
|
>
|
|
|
|
<FormControl>
|
|
|
|
<SelectTrigger>
|
|
|
|
<SelectValue />
|
|
|
|
</SelectTrigger>
|
|
|
|
</FormControl>
|
|
|
|
<SelectContent>
|
|
|
|
<SelectItem value="public">Public</SelectItem>
|
|
|
|
<SelectItem value="private">Private</SelectItem>
|
|
|
|
</SelectContent>
|
|
|
|
</Select>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
2024-04-30 22:48:36 -04:00
|
|
|
<Button disabled={loading} type="submit" className="w-full">
|
2024-04-27 16:22:35 -04:00
|
|
|
{loading ? (
|
|
|
|
<>
|
|
|
|
<Loader2 className="animate-spin mr-2 h-4 w-4" /> Loading...
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
"Submit"
|
|
|
|
)}
|
2024-04-30 22:48:36 -04:00
|
|
|
</Button>
|
2024-04-23 01:53:37 -04:00
|
|
|
</form>
|
|
|
|
</Form>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
|
|
|
)
|
|
|
|
}
|