'use client' import { useState } from 'react' import { Input } from '@/components/ui/input' import { Button } from '@/components/ui/button' import { Check, Loader2, Pencil, Trash2, X } from 'lucide-react' import { toast } from '@/hooks/use-toast' import { useTranslations } from 'next-intl' interface TransactionNoteEditorProps { transactionId: string initialNote?: string onSave: (id: string, note: string) => Promise onDelete: (id: string) => Promise } export function TransactionNoteEditor({ transactionId, initialNote = '', onSave, onDelete }: TransactionNoteEditorProps) { const t = useTranslations('TransactionNoteEditor'); const [isEditing, setIsEditing] = useState(false) const [noteText, setNoteText] = useState(initialNote) const [isSaving, setIsSaving] = useState(false) const handleSave = async () => { const trimmedNote = noteText.trim() if (trimmedNote.length > 200) { toast({ title: t('noteTooLongTitle'), description: t('noteTooLongDescription'), variant: 'destructive' }) return } setIsSaving(true) try { await onSave(transactionId, trimmedNote) setIsEditing(false) } catch (error) { toast({ title: t('errorSavingNoteTitle'), description: t('pleaseTryAgainDescription'), variant: 'destructive' }) // Revert to initial value on error setNoteText(initialNote) } finally { setIsSaving(false) } } const handleDelete = async () => { setIsSaving(true) try { await onDelete(transactionId) setNoteText(initialNote) setIsEditing(false) } catch (error) { toast({ title: t('errorDeletingNoteTitle'), description: t('pleaseTryAgainDescription'), variant: 'destructive' }) } finally { setIsSaving(false) } } if (isEditing) { return (
setNoteText(e.target.value)} placeholder={t('addNotePlaceholder')} className="w-64" maxLength={200} />
{initialNote && ( )}
) } return (
{noteText && ( {noteText} )}
) }