"use client"; import { useState, type FormEvent } from "react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { Loader2Icon } from "lucide-react"; export type NominationActionResult = { readonly success: boolean; readonly error?: string; }; interface NominationNoteDialogProps { readonly open: boolean; readonly onOpenChange: (open: boolean) => void; readonly title: string; readonly description: string; readonly fieldId: string; readonly fieldLabel: string; readonly placeholder: string; readonly submitLabel: string; readonly successMessage: string; readonly required: boolean; readonly submitVariant?: "default" | "destructive" | "outline" | "secondary" | "ghost" | "link"; readonly onSubmit: (note: string) => Promise; } export function NominationNoteDialog({ open, onOpenChange, title, description, fieldId, fieldLabel, placeholder, submitLabel, successMessage, required, submitVariant = "default", onSubmit, }: NominationNoteDialogProps) { const [note, setNote] = useState(""); const [pending, setPending] = useState(false); const [error, setError] = useState(null); function handleOpenChange(nextOpen: boolean) { onOpenChange(nextOpen); if (!nextOpen) { setNote(""); setError(null); } } async function handleSubmit(event: FormEvent) { event.preventDefault(); const trimmedNote = note.trim(); if (required && !trimmedNote) { setError("A reason is required."); return; } setPending(true); setError(null); try { const result = await onSubmit(trimmedNote); if (result.success) { toast.success(successMessage); handleOpenChange(false); } else { setError(result.error ?? "Could not complete this action."); } } catch (caughtError) { setError( caughtError instanceof Error ? caughtError.message : "Could not complete this action.", ); } finally { setPending(false); } } return ( {title} {description}
void handleSubmit(event)}>