"use client"; import { FormEvent, useState } from "react"; type Source = { id: number; name: string }; export function PublishForm({ sources }: { sources: Source[] }) { const [error, setError] = useState(""); const [submitting, setSubmitting] = useState(false); async function submit(event: FormEvent) { event.preventDefault(); setSubmitting(true); setError(""); try { const response = await fetch("/api/posts", { method: "POST", body: new FormData(event.currentTarget), headers: { Accept: "application/json" } }); const result = await response.json(); if (!response.ok) throw new Error(result.error || "發佈失敗"); window.location.assign(`/posts/${result.id}`); } catch (reason) { setError(reason instanceof Error ? reason.message : "發佈失敗"); setSubmitting(false); } } return