import React from 'react'; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { Loader2 } from "lucide-react"; import { useForum } from "@/contexts/useForum"; import { useAuth } from "@/contexts/useAuth"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { useToast } from "@/hooks/use-toast"; import { urlLoads } from "@/lib/utils/urlLoads"; const formSchema = z.object({ title: z.string().min(3, "Title must be at least 3 characters").max(50, "Title must be less than 50 characters"), description: z.string().min(10, "Description must be at least 10 characters").max(200, "Description must be less than 200 characters"), icon: z .string() .optional() .refine((val) => !val || val.length === 0 || URL.canParse(val), { message: "Must be a valid URL" }), }); export function CreateCellDialog() { const { createCell, isPostingCell } = useForum(); const { isAuthenticated } = useAuth(); const { toast } = useToast(); const [open, setOpen] = React.useState(false); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { title: "", description: "", icon: undefined, }, }); const onSubmit = async (values: z.infer) => { // Validate icon URL if provided if (values.icon && values.icon.trim()) { const ok = await urlLoads(values.icon, 5000); if (!ok) { toast({ title: "Icon URL Error", description: "Icon URL could not be loaded. Please check the URL and try again.", variant: "destructive", }); return; } } const cell = await createCell(values.title, values.description, values.icon || undefined); if (cell) { setOpen(false); form.reset(); } }; if (!isAuthenticated) return null; return ( Create a New Cell
( Title )} /> ( Description