'use client'; import { useState, type FormEvent } from 'react'; import { useRouter } from 'next/navigation'; const CATEGORIES = [ { value: '', label: 'なし' }, { value: 'notice', label: 'notice' }, { value: 'spec', label: 'spec' }, { value: 'minutes', label: 'minutes' }, { value: 'question', label: 'question' }, { value: 'decision', label: 'decision' }, { value: 'trouble', label: 'trouble' }, { value: 'memo', label: 'memo' }, ] as const; export function ThreadForm({ projectId }: { projectId: number }) { const router = useRouter(); const [title, setTitle] = useState(''); const [bodyMd, setBodyMd] = useState(''); const [category, setCategory] = useState(''); const [error, setError] = useState(null); const [loading, setLoading] = useState(false); async function onSubmit(event: FormEvent) { event.preventDefault(); setLoading(true); setError(null); const res = await fetch(`/api/projects/${projectId}/board/threads`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title, bodyMd, category: category || undefined, }), }); setLoading(false); if (res.ok) { const data = (await res.json()) as { thread: { id: number } }; router.push(`/projects/${projectId}/board/${data.thread.id}`); 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="flex-1 rounded border px-3 py-2" required maxLength={200} />