"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { Loader2, NotebookPen, Pencil, Pin, PinOff, Plus, Trash2, X } from "lucide-react"; import type { Note } from "@prisma/client"; import { Markdown } from "@/components/Markdown"; import { cn, formatDateTime, formatRelative } from "@/lib/utils"; /** Nur als "bearbeitet" zaehlen, wenn wirklich spaeter geaendert wurde. */ function wasEdited(note: Note): boolean { const created = new Date(note.createdAt).getTime(); const updated = new Date(note.updatedAt).getTime(); return updated - created > 1500; } export function NotesPanel({ projectId, notes }: { projectId: string; notes: Note[] }) { const router = useRouter(); const [composerOpen, setComposerOpen] = useState(false); const [title, setTitle] = useState(""); const [content, setContent] = useState(""); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [editingId, setEditingId] = useState(null); const [busyId, setBusyId] = useState(null); function resetComposer() { setTitle(""); setContent(""); setEditingId(null); setComposerOpen(false); setError(null); } async function save(event: React.FormEvent) { event.preventDefault(); if (!content.trim()) return; setSaving(true); setError(null); try { const response = await fetch( editingId ? `/api/notes/${editingId}` : `/api/projects/${projectId}/notes`, { method: editingId ? "PATCH" : "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title, content }), }, ); if (!response.ok) { const payload = await response.json().catch(() => ({})); throw new Error(payload.error ?? "Speichern fehlgeschlagen."); } resetComposer(); router.refresh(); } catch (caught) { setError(caught instanceof Error ? caught.message : String(caught)); } finally { setSaving(false); } } async function togglePin(note: Note) { setBusyId(note.id); await fetch(`/api/notes/${note.id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pinned: !note.pinned }), }).catch(() => undefined); setBusyId(null); router.refresh(); } async function remove(note: Note) { if (!window.confirm("Notiz wirklich loeschen?")) return; setBusyId(note.id); await fetch(`/api/notes/${note.id}`, { method: "DELETE" }).catch(() => undefined); setBusyId(null); router.refresh(); } function startEdit(note: Note) { setEditingId(note.id); setTitle(note.title ?? ""); setContent(note.content); setComposerOpen(true); } return (

Notizen {notes.length}

{!composerOpen ? ( ) : null}
{composerOpen ? (
setTitle(event.target.value)} maxLength={160} />