"use client"; import { useState } from "react"; import { Button, Card } from "@/components/ui/Primitives"; import { createNote } from "@/lib/notes-client"; import { NOTE_TEMPLATES } from "@/lib/note-templates"; import type { WorkspaceSummary } from "@/lib/types"; interface CreateNoteModalProps { workspaces: WorkspaceSummary[]; defaultWorkspaceId?: string; onCreated: () => void; onClose: () => void; } export function CreateNoteModal({ workspaces, defaultWorkspaceId, onCreated, onClose }: CreateNoteModalProps) { const [title, setTitle] = useState(""); const [body, setBody] = useState(""); const [workspaceId, setWorkspaceId] = useState(defaultWorkspaceId ?? workspaces[0]?.id ?? ""); const [tags, setTags] = useState(""); const [templateId, setTemplateId] = useState(""); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); function applyTemplate(id: string) { setTemplateId(id); const t = NOTE_TEMPLATES.find((x) => x.id === id); if (t) { setTitle(t.title); setBody(t.body); } } const canSubmit = title.trim().length > 0 && body.trim().length > 0 && workspaceId.length > 0; async function handleSubmit(e: React.FormEvent) { e.preventDefault(); if (!canSubmit || saving) return; setSaving(true); setError(null); try { await createNote({ id: crypto.randomUUID(), workspaceId, title: title.trim(), body: body.includes("<") && body.includes(">") ? body.trim() : `

${body.trim()}

`, tags: tags .split(",") .map((t) => t.trim()) .filter(Boolean), sourceType: "manual", }); onCreated(); } catch (err) { setError(err instanceof Error ? err.message : "Failed to create note"); } finally { setSaving(false); } } return (
{ if (e.target === e.currentTarget) onClose(); }} >
Create Note
{error &&
{error}
}