"use client"; import { NotebookPen, Plus } from "lucide-react"; import { useEffect, useState } from "react"; import { NotesEditor } from "@/components/notes/notes-editor"; import { Button } from "@/components/ui/button"; import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle, } from "@/components/ui/empty"; import { createNote, deleteNote, listNotes, type Note, updateNote, } from "@/lib/notes"; import { notify } from "@/lib/toast"; import { cn } from "@/lib/utils"; const newDraft = (): Note => ({ id: "", title: "", content: "", createdAt: "", updatedAt: "", }); export function NotesView() { const [notes, setNotes] = useState([]); // No auto-selection: with nothing chosen the right pane shows the Empty state. const [selected, setSelected] = useState(null); const [draftKey, setDraftKey] = useState(0); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); useEffect(() => { let active = true; listNotes() .then((data) => { if (active) setNotes(data); }) .catch((err) => { if (active) { notify.error( "Couldn't load notes", err instanceof Error ? err.message : "Please try again.", ); } }) .finally(() => { if (active) setLoading(false); }); return () => { active = false; }; }, []); const startNew = () => { setSelected(newDraft()); setDraftKey((k) => k + 1); }; const save = async (data: { title: string; content: string }) => { setSaving(true); try { const saved = selected?.id ? await updateNote(selected.id, data) : await createNote(data); const list = await listNotes(); setNotes(list); setSelected(list.find((n) => n.id === saved.id) ?? saved); notify.success("Note saved"); } catch (err) { notify.error( "Couldn't save note", err instanceof Error ? err.message : "Please try again.", ); } finally { setSaving(false); } }; const remove = async (id: string) => { try { await deleteNote(id); const list = await listNotes(); setNotes(list); setSelected(null); notify.success("Note deleted"); } catch (err) { notify.error( "Couldn't delete note", err instanceof Error ? err.message : "Please try again.", ); } }; return (
{/* Left: note list */} {/* Right: editor or empty state */}
{selected ? (
remove(selected.id) : undefined} onSave={save} saving={saving} />
) : ( No note selected Select a note from the list, or create a new one to start writing. )}
); }