"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import { apiCreatePost, apiMe, fetchBoards, type Board } from "@/lib/api"; export default function ComposePage() { const router = useRouter(); const [boards, setBoards] = useState([]); const [boardId, setBoardId] = useState(0); const [title, setTitle] = useState(""); const [content, setContent] = useState(""); const [tags, setTags] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); useEffect(() => { // 通过 /api/me 检查登录态(HttpOnly cookie 自动携带) apiMe() .then((res) => { if (!res.user) { router.push("/login?redirect=/compose"); return; } fetchBoards().then((data) => { setBoards(data.boards); if (data.boards.length > 0) setBoardId(data.boards[0].id); }); }) .catch(() => router.push("/login?redirect=/compose")); }, [router]); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(""); try { const res = await apiCreatePost({ board_id: boardId, title, content, tags, post_type: "normal", }); if (res.post) { router.push(`/post/${res.post.id}`); } else { setError(res.error || "发帖失败"); } } finally { setLoading(false); } }; return (

发布新帖

{error && (
{error}
)}
setTitle(e.target.value)} required maxLength={256} placeholder="帖子标题" className="w-full px-3 py-2 rounded-lg border" style={{ borderColor: "var(--border)", background: "var(--bg)", color: "var(--text)" }} />
setTags(e.target.value)} placeholder="例如:技术,Go" className="w-full px-3 py-2 rounded-lg border" style={{ borderColor: "var(--border)", background: "var(--bg)", color: "var(--text)" }} />