"use client"; import { FormEvent, useState } from "react"; import { useRouter } from "next/navigation"; import { MarkdownPreview } from "@/components/MarkdownPreview"; import { normalizeImageUrl } from "@/lib/normalize"; const defaultIntro = `## 今日进展 - 今日目标: - 产品 / 交付: - 客户 / 收入: - 增长 / 运营: - 学习 / 复盘:`; export function CreatePostForm({ availableTags = [], publishLimit = 10, todayCount = 0 }: { availableTags?: string[]; publishLimit?: number; todayCount?: number; }) { const [title, setTitle] = useState(""); const [cover, setCover] = useState(""); const [tags, setTags] = useState(""); const [markdown, setMarkdown] = useState(defaultIntro); const [loading, setLoading] = useState(false); const [preview, setPreview] = useState(false); const router = useRouter(); const addTag = (tag: string) => { const current = new Set( tags .split(",") .map((item) => item.trim()) .filter(Boolean) ); current.add(tag); setTags(Array.from(current).join(", ")); }; async function handleSubmit(e: FormEvent) { e.preventDefault(); setLoading(true); try { const normalizedCover = normalizeImageUrl(cover); const res = await fetch("/api/posts", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title, markdown, cover: normalizedCover, tags: Array.from( new Set( tags .split(",") .map((item) => item.trim()) .filter(Boolean) ) ) }) }); if (!res.ok) { const data = await res.json().catch(() => ({})); alert(data.error ? JSON.stringify(data.error) : "发布失败"); return; } const data = await res.json(); router.push(`/p/${data.slug}`); router.refresh(); } finally { setLoading(false); } } return (
); }