"use client"; import dynamic from "next/dynamic"; import { useRouter } from "next/navigation"; import { Pin, PinOff, Save, Trash2 } from "lucide-react"; import { useState, useTransition } from "react"; import { EntityComments } from "@/components/comments/entity-comments"; import { RichTextContent } from "@/components/rich-text"; import { DetailBackLink } from "@/components/detail-back-link"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { ShareButton } from "@/components/share-button"; import type { NoteDto } from "../server/queries"; import { createNote, deleteNote, setNotePinned, updateNote } from "../server/actions"; const RichTextEditor = dynamic( () => import("@/components/rich-text/rich-text-editor").then((mod) => mod.RichTextEditor), { ssr: false, loading: () => (
Loading editor…
), }, ); export function NoteEditor({ note, currentUserId }: { note?: NoteDto; currentUserId?: string }) { const router = useRouter(); const [currentNote, setCurrentNote] = useState(note); const [title, setTitle] = useState(note?.title ?? ""); const [body, setBody] = useState(note?.body ?? ""); const [remindAt, setRemindAt] = useState(toLocalDateTimeValue(note?.remindAt ?? null)); const [isPending, startTransition] = useTransition(); const pinned = currentNote?.pinned ?? false; function saveNote() { startTransition(async () => { if (currentNote) { const updated = await updateNote({ id: currentNote.id, title, body, remindAt: remindAt ? new Date(remindAt) : null, }); setCurrentNote(updated); return; } const created = await createNote({ title, body, remindAt: remindAt ? new Date(remindAt) : null, }); router.push(`/notes/${created.id}`); }); } function togglePinned() { if (!currentNote) return; startTransition(async () => { setCurrentNote(await setNotePinned({ id: currentNote.id, pinned: !pinned })); }); } function removeNote() { if (!currentNote) return; startTransition(async () => { await deleteNote({ id: currentNote.id }); router.push("/notes"); }); } return (
{pinned && }

{currentNote ? currentNote.title : "New note"}

{currentNote ? ( ) : null} {currentNote ? : null} {currentNote ? ( ) : null}
setTitle(event.target.value)} />
setRemindAt(event.target.value)} />
{currentNote && currentUserId ? ( ) : null}
); } function toLocalDateTimeValue(value: string | null) { if (!value) return ""; const date = new Date(value); const local = new Date(date.getTime() - date.getTimezoneOffset() * 60_000); return local.toISOString().slice(0, 16); }