import { useState } from 'react'; import { Plus, Pencil, Save, X, Trash2 } from 'lucide-react'; import { useUi } from '../../context/ui-context.jsx'; import { useAsync, useAction } from '../../hooks/useAsync.js'; import { listNotes, createNote, updateNote, deleteNote } from '../../services/notes.js'; import { Button } from '../../components/Button.jsx'; function formatDateTime(value) { return String(value).slice(0, 16).replace('T', ' '); } export default function NotasPage() { const { t, pushToast, openDialog } = useUi(); const runAction = useAction(); const { data: notes, reload } = useAsync(listNotes, []); const [newContent, setNewContent] = useState(''); const [editingId, setEditingId] = useState(null); const [editingContent, setEditingContent] = useState(''); async function addNote() { if (!newContent.trim()) return; await runAction(async () => { await createNote(newContent); setNewContent(''); await reload(); }); } function startEdit(note) { setEditingId(note.id); setEditingContent(note.content); } function cancelEdit() { setEditingId(null); setEditingContent(''); } async function saveEdit(id) { if (!editingContent.trim()) return; await runAction(async () => { await updateNote(id, editingContent); cancelEdit(); await reload(); }); } function confirmDelete(note) { openDialog({ tone: 'warning', title: t('common_confirm_delete_title'), message: t('common_confirm_delete_msg'), actions: [ { label: t('common_cancel'), variant: 'standard' }, { label: t('common_delete'), variant: 'danger', onClick: () => runAction(async () => { await deleteNote(note.id); await reload(); pushToast({ tone: 'success', message: t('common_saved') }); }), }, ], }); } if (!notes) return