2024-05-26 19:02:47 -07:00
|
|
|
"use client"
|
2024-04-23 01:53:37 -04:00
|
|
|
|
|
|
|
import {
|
|
|
|
Dialog,
|
|
|
|
DialogContent,
|
|
|
|
DialogDescription,
|
|
|
|
DialogHeader,
|
|
|
|
DialogTitle,
|
|
|
|
DialogTrigger,
|
2024-05-26 19:02:47 -07:00
|
|
|
} from "@/components/ui/dialog"
|
|
|
|
import Image from "next/image"
|
|
|
|
import { useState } from "react"
|
|
|
|
import { set, z } from "zod"
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
|
|
|
import { useForm } from "react-hook-form"
|
2024-04-23 01:53:37 -04:00
|
|
|
|
|
|
|
import {
|
|
|
|
Form,
|
|
|
|
FormControl,
|
|
|
|
FormDescription,
|
|
|
|
FormField,
|
|
|
|
FormItem,
|
|
|
|
FormLabel,
|
|
|
|
FormMessage,
|
2024-05-26 19:02:47 -07:00
|
|
|
} from "@/components/ui/form"
|
|
|
|
import { Input } from "@/components/ui/input"
|
2024-04-23 01:53:37 -04:00
|
|
|
import {
|
|
|
|
Select,
|
|
|
|
SelectContent,
|
|
|
|
SelectItem,
|
|
|
|
SelectTrigger,
|
|
|
|
SelectValue,
|
2024-05-26 19:02:47 -07:00
|
|
|
} from "@/components/ui/select"
|
|
|
|
import { useUser } from "@clerk/nextjs"
|
|
|
|
import { createSandbox } from "@/lib/actions"
|
|
|
|
import { useRouter } from "next/navigation"
|
|
|
|
import { Loader2 } from "lucide-react"
|
|
|
|
import { Button } from "../ui/button"
|
2024-04-23 01:53:37 -04:00
|
|
|
|
|
|
|
const data: {
|
2024-08-18 06:57:26 -07:00
|
|
|
id: string
|
2024-05-26 19:02:47 -07:00
|
|
|
name: string
|
|
|
|
icon: string
|
|
|
|
description: string
|
|
|
|
disabled: boolean
|
2024-04-23 01:53:37 -04:00
|
|
|
}[] = [
|
|
|
|
{
|
2024-08-18 06:57:26 -07:00
|
|
|
id: "reactjs",
|
2024-04-23 01:53:37 -04:00
|
|
|
name: "React",
|
|
|
|
icon: "/project-icons/react.svg",
|
|
|
|
description: "A JavaScript library for building user interfaces",
|
2024-05-09 12:03:47 -07:00
|
|
|
disabled: false,
|
2024-04-23 01:53:37 -04:00
|
|
|
},
|
|
|
|
{
|
2024-08-18 06:57:26 -07:00
|
|
|
id: "vanillajs",
|
|
|
|
name: "HTML/JS",
|
|
|
|
icon: "/project-icons/more.svg",
|
|
|
|
description: "More coming soon, feel free to contribute on GitHub",
|
|
|
|
disabled: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "nextjs",
|
|
|
|
name: "NextJS",
|
2024-04-23 01:53:37 -04:00
|
|
|
icon: "/project-icons/node.svg",
|
|
|
|
description: "A JavaScript runtime built on the V8 JavaScript engine",
|
2024-05-09 12:03:47 -07:00
|
|
|
disabled: false,
|
2024-04-23 01:53:37 -04:00
|
|
|
},
|
2024-05-09 12:03:47 -07:00
|
|
|
{
|
2024-08-18 06:57:26 -07:00
|
|
|
id: "streamlit",
|
|
|
|
name: "Streamlit",
|
2024-05-09 12:03:47 -07:00
|
|
|
icon: "/project-icons/python.svg",
|
2024-08-18 06:57:26 -07:00
|
|
|
description: "A JavaScript runtime built on the V8 JavaScript engine",
|
|
|
|
disabled: false,
|
|
|
|
}
|
2024-05-26 19:02:47 -07:00
|
|
|
]
|
2024-04-23 01:53:37 -04:00
|
|
|
|
|
|
|
const formSchema = z.object({
|
2024-05-09 12:03:47 -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"]),
|
2024-05-26 19:02:47 -07:00
|
|
|
})
|
2024-04-23 01:53:37 -04:00
|
|
|
|
|
|
|
export default function NewProjectModal({
|
|
|
|
open,
|
|
|
|
setOpen,
|
|
|
|
}: {
|
2024-05-26 19:02:47 -07:00
|
|
|
open: boolean
|
|
|
|
setOpen: (open: boolean) => void
|
2024-04-23 01:53:37 -04:00
|
|
|
}) {
|
2024-09-15 08:05:53 -07:00
|
|
|
const [selected, setSelected] = useState("reactjs")
|
2024-05-26 19:02:47 -07:00
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const router = useRouter()
|
2024-04-27 16:22:35 -04:00
|
|
|
|
2024-05-26 19:02:47 -07:00
|
|
|
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-05-26 19:02:47 -07:00
|
|
|
})
|
2024-04-23 01:53:37 -04:00
|
|
|
|
2024-04-27 16:22:35 -04:00
|
|
|
async function onSubmit(values: z.infer<typeof formSchema>) {
|
2024-05-26 19:02:47 -07:00
|
|
|
if (!user.isSignedIn) return
|
2024-04-27 16:22:35 -04:00
|
|
|
|
2024-05-26 19:02:47 -07:00
|
|
|
const sandboxData = { type: selected, userId: user.user.id, ...values }
|
|
|
|
setLoading(true)
|
2024-04-27 16:22:35 -04:00
|
|
|
|
2024-05-26 19:02:47 -07:00
|
|
|
const id = await createSandbox(sandboxData)
|
|
|
|
router.push(`/code/${id}`)
|
2024-04-23 01:53:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-05-09 22:16:56 -07:00
|
|
|
<Dialog
|
|
|
|
open={open}
|
|
|
|
onOpenChange={(open: boolean) => {
|
2024-05-26 19:02:47 -07:00
|
|
|
if (!loading) setOpen(open)
|
2024-05-09 22:16:56 -07:00
|
|
|
}}
|
|
|
|
>
|
2024-04-23 01:53:37 -04:00
|
|
|
<DialogContent>
|
|
|
|
<DialogHeader>
|
|
|
|
<DialogTitle>Create A Sandbox</DialogTitle>
|
|
|
|
</DialogHeader>
|
|
|
|
<div className="grid grid-cols-2 w-full gap-2 mt-2">
|
|
|
|
{data.map((item) => (
|
|
|
|
<button
|
2024-05-09 21:05:21 -07:00
|
|
|
disabled={item.disabled || loading}
|
2024-04-23 01:53:37 -04:00
|
|
|
key={item.id}
|
|
|
|
onClick={() => setSelected(item.id)}
|
|
|
|
className={`${
|
|
|
|
selected === item.id ? "border-foreground" : "border-border"
|
2024-05-09 12:03:47 -07:00
|
|
|
} 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 disabled:opacity-50 disabled:cursor-not-allowed`}
|
2024-04-23 01:53:37 -04:00
|
|
|
>
|
|
|
|
<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}>
|
2024-05-26 19:02:47 -07:00
|
|
|
<form autoComplete="off" onSubmit={form.handleSubmit(onSubmit)}>
|
2024-04-23 01:53:37 -04:00
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="name"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem className="mb-4">
|
|
|
|
<FormLabel>Name</FormLabel>
|
|
|
|
<FormControl>
|
2024-05-09 21:05:21 -07:00
|
|
|
<Input
|
|
|
|
disabled={loading}
|
|
|
|
placeholder="My Project"
|
|
|
|
{...field}
|
|
|
|
/>
|
2024-04-23 01:53:37 -04:00
|
|
|
</FormControl>
|
|
|
|
<FormMessage />
|
|
|
|
</FormItem>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
<FormField
|
|
|
|
control={form.control}
|
|
|
|
name="visibility"
|
|
|
|
render={({ field }) => (
|
|
|
|
<FormItem className="mb-8">
|
|
|
|
<FormLabel>Visibility</FormLabel>
|
|
|
|
<Select
|
2024-05-09 21:05:21 -07:00
|
|
|
disabled={loading}
|
2024-04-23 01:53:37 -04:00
|
|
|
onValueChange={field.onChange}
|
|
|
|
defaultValue={field.value}
|
|
|
|
>
|
|
|
|
<FormControl>
|
|
|
|
<SelectTrigger>
|
|
|
|
<SelectValue />
|
|
|
|
</SelectTrigger>
|
|
|
|
</FormControl>
|
|
|
|
<SelectContent>
|
|
|
|
<SelectItem value="public">Public</SelectItem>
|
|
|
|
<SelectItem value="private">Private</SelectItem>
|
|
|
|
</SelectContent>
|
|
|
|
</Select>
|
2024-05-26 19:02:47 -07:00
|
|
|
<FormDescription>
|
|
|
|
Note: All sandboxes cannot be seen by the public. Private
|
|
|
|
sandboxes cannot be accessed by shared users that you add,
|
|
|
|
while public sandboxes can.
|
|
|
|
</FormDescription>
|
2024-04-23 01:53:37 -04:00
|
|
|
<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 ? (
|
|
|
|
<>
|
2024-05-26 21:41:20 -07:00
|
|
|
<Loader2 className="animate-spin mr-2 h-4 w-4" /> Creating
|
|
|
|
project...
|
2024-04-27 16:22:35 -04:00
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
"Submit"
|
|
|
|
)}
|
2024-04-30 22:48:36 -04:00
|
|
|
</Button>
|
2024-04-23 01:53:37 -04:00
|
|
|
</form>
|
|
|
|
</Form>
|
|
|
|
</DialogContent>
|
|
|
|
</Dialog>
|
2024-05-26 19:02:47 -07:00
|
|
|
)
|
2024-04-23 01:53:37 -04:00
|
|
|
}
|