'use client' import { useActionState } from 'react' import { useFormStatus } from 'react-dom' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Textarea } from '@/components/ui/textarea' import { cn } from '@/lib/utils' import { debugProps } from '@/lib/debug' type FieldErrors = Record type ActionResult = { error?: string | FieldErrors; success?: boolean } | undefined function SubmitButton({ label }: { label: string }) { const { pending } = useFormStatus() return ( ) } function getFieldError(error: string | FieldErrors | undefined, field: string): string | undefined { if (!error || typeof error === 'string') return undefined return (error as FieldErrors)[field]?.[0] } function getGlobalError(error: string | FieldErrors | undefined): string | undefined { if (typeof error === 'string') return error return undefined } interface ProductFormProps { action: (_prevState: unknown, formData: FormData) => Promise submitLabel: string defaultValues?: { id?: string name?: string code?: string | null description?: string repo_url?: string definition_of_done?: string } } export function ProductForm({ action, submitLabel, defaultValues }: ProductFormProps) { const [state, formAction] = useActionState(action, undefined) const fieldError = (field: string) => getFieldError(state?.error, field) const globalError = getGlobalError(state?.error) return (
{defaultValues?.id && ( )}
{fieldError('code') && (

{fieldError('code')}

)}
{fieldError('name') && (

{fieldError('name')}

)}