"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { createAsset } from "@/actions/assets"; import { useToast } from "@/components/ui/use-toast"; import { ShotPriority } from "@prisma/client"; const assetSchema = z.object({ assetCode: z .string() .min(1, "Asset code is required") .max(30) .regex(/^[A-Z0-9_\-]+$/i, "Alphanumeric, dash, underscore only"), name: z.string().min(1, "Name is required"), description: z.string().optional(), priority: z.nativeEnum(ShotPriority), dueDate: z.string().optional(), }); type AssetFormValues = z.infer; interface Artist { id: string; name: string | null; email: string; } interface NewAssetDialogProps { projectId: string; open: boolean; onClose: () => void; onSuccess?: () => void; } export function NewAssetDialog({ projectId, open, onClose, onSuccess }: NewAssetDialogProps) { const [isSubmitting, setIsSubmitting] = useState(false); const { toast } = useToast(); const router = useRouter(); const { register, handleSubmit, setValue, reset, formState: { errors }, } = useForm({ resolver: zodResolver(assetSchema), defaultValues: { priority: "NORMAL" }, }); const onSubmit = async (data: AssetFormValues) => { setIsSubmitting(true); try { await createAsset({ projectId, status: "WAITING", ...data }); toast({ title: "Asset created" }); reset(); router.refresh(); onSuccess?.(); onClose(); } catch (err) { toast({ title: "Failed to create asset", description: (err as Error).message, variant: "destructive", }); } finally { setIsSubmitting(false); } }; return ( !o && onClose()}> New Asset
{errors.assetCode && (

{errors.assetCode.message}

)}
{errors.name && (

{errors.name.message}

)}