'use client'; import { useState, type FormEvent } from 'react'; import { useRouter } from 'next/navigation'; import type { ProjectNote } from '@/lib/types'; export function NoteEditor({ projectId, note, }: { projectId: number; note: ProjectNote; }) { const router = useRouter(); const [title, setTitle] = useState(note.title); const [bodyMd, setBodyMd] = useState(note.bodyMd); const [tags, setTags] = useState(note.tags ?? ''); const [isPinned, setIsPinned] = useState(note.isPinned === 1); const [error, setError] = useState(null); const [saved, setSaved] = useState(false); const [loading, setLoading] = useState(false); async function onSubmit(event: FormEvent) { event.preventDefault(); setLoading(true); setError(null); setSaved(false); const res = await fetch(`/api/projects/${projectId}/notes/${note.id}`, { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title, bodyMd, tags: tags || null, isPinned: isPinned ? 1 : 0, }), }); setLoading(false); if (res.ok) { setSaved(true); router.refresh(); } else { const b = (await res.json().catch(() => null)) as { error?: { message?: string }; } | null; setError(b?.error?.message ?? '更新に失敗しました'); } } return (

編集

setTitle(e.target.value)} className="w-full rounded border px-3 py-2" required maxLength={200} />