"use client"; import { useRef, useState } from "react"; import { useRouter } from "next/navigation"; import type { FeedbackCategory } from "@/db/repository"; type SubmitState = { loading: boolean; message: string; error: boolean }; export function FeedbackForm() { const router = useRouter(); const pendingKey = useRef(null); const [category, setCategory] = useState("connection_issue"); const [message, setMessage] = useState(""); const [state, setState] = useState({ loading: false, message: "", error: false, }); async function submit(event: React.FormEvent) { event.preventDefault(); setState({ loading: true, message: "", error: false }); pendingKey.current ??= crypto.randomUUID(); try { const response = await fetch("/api/feedback", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ category, message, idempotencyKey: pendingKey.current, }), }); const body = (await response.json()) as { message?: string }; if (!response.ok) throw new Error(body.message || "提交失败"); pendingKey.current = null; setMessage(""); setState({ loading: false, message: "已收到。处理结果会显示在本页,不需要重复提交。", error: false, }); router.refresh(); } catch (error) { setState({ loading: false, message: error instanceof Error ? error.message : "提交失败", error: true, }); } } return (