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 { useForumActions, usePermissions } from '@/hooks'; 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, 'Cell name must be at least 3 characters') .max(50, 'Cell name must be less than 50 characters'), description: z .string() .min(10, 'Description must be at least 10 characters') .max(500, 'Description must be less than 500 characters'), icon: z .string() .optional() .refine( val => { if (!val) return true; return urlLoads(val); }, { message: 'Icon must be a valid URL', } ), }); interface CreateCellDialogProps { open?: boolean; onOpenChange?: (open: boolean) => void; } export function CreateCellDialog({ open: externalOpen, onOpenChange, }: CreateCellDialogProps = {}) { const { createCell, isCreatingCell } = useForumActions(); const { canCreateCell } = usePermissions(); const { toast } = useToast(); const [internalOpen, setInternalOpen] = React.useState(false); const open = externalOpen ?? internalOpen; const setOpen = onOpenChange ?? setInternalOpen; const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { title: '', description: '', icon: '', }, }); const onSubmit = async (values: z.infer) => { if (!canCreateCell) { toast({ title: 'Permission Denied', description: 'You need to verify Ordinal ownership to create cells.', variant: 'destructive', }); return; } // ✅ All validation handled in hook const cell = await createCell( values.title, values.description, values.icon ); if (cell) { form.reset(); setOpen(false); } }; // Handle keyboard shortcuts const handleKeyDown = (e: React.KeyboardEvent) => { if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') { e.preventDefault(); if (!isCreatingCell && canCreateCell) { form.handleSubmit(onSubmit)(); } } }; return ( Create New Cell
( Cell Name )} /> ( Description