"use client"; import type { Dispatch, FormEvent, SetStateAction } from "react"; import { useState } from "react"; import { SaveIcon } from "@/components/icons"; import { Button, useToast } from "@/components/shadcn"; import { Input } from "@/components/shadcn/input/input"; interface EditNameFormProps { currentValue: string; label: string; successMessage: string; placeholder?: string; helperText?: string; validate?: (value: string) => string | null; setIsOpen: Dispatch>; onSave: (value: string) => Promise; } export function EditNameForm({ currentValue, label, successMessage, placeholder, helperText, validate, setIsOpen, onSave, }: EditNameFormProps) { const [value, setValue] = useState(currentValue); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const { toast } = useToast(); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); const trimmed = value.trim(); if (validate) { const validationError = validate(trimmed); if (validationError) { setError(validationError); return; } } setError(null); setIsLoading(true); const result = (await onSave(trimmed)) as Record; setIsLoading(false); const errors = result?.errors as Array<{ detail: string }> | undefined; if (errors && errors.length > 0) { toast({ variant: "destructive", title: "Oops! Something went wrong", description: `${errors[0].detail}`, }); } else if (result?.error) { toast({ variant: "destructive", title: "Oops! Something went wrong", description: String(result.error), }); } else { toast({ title: "Success!", description: successMessage }); setIsOpen(false); } }; return (
Current {label.toLowerCase()}:{" "} {currentValue || "—"}
{ setValue(e.target.value); if (error) setError(null); }} placeholder={placeholder ?? currentValue} disabled={isLoading} aria-invalid={!!error} /> {error &&

{error}

} {helperText && !error && (

{helperText}

)}
); }