"use client" import { Loader2 } from "lucide-react" import dynamic from "next/dynamic" import Link from "next/link" import { useRouter } from "next/navigation" import { useCallback, useEffect, useState } from "react" import { toast } from "sonner" import { createArticle, getArticle, updateArticle } from "@/actions/article" import type { ArticleGroup } from "@/models/article-group" const Editor = dynamic(() => import("@/components/editor/richTextEditor"), { ssr: false, loading: () => (
编辑器加载中...
), }) interface ArticleEditorProps { articleId: string groups: ArticleGroup[] } export default function ArticleEditor({ articleId, groups, }: ArticleEditorProps) { const router = useRouter() const isNew = articleId === "new" const [loading, setLoading] = useState(!isNew) const [title, setTitle] = useState("") const [initialContent, setInitialContent] = useState("") const [groupId, setGroupId] = useState( groups.length > 0 ? String(groups[0].id) : "", ) useEffect(() => { if (!isNew) { const id = Number(articleId) if (Number.isNaN(id)) { toast.error("无效的文章 ID") router.push("/articles") return } getArticle(id).then(resp => { if (resp.success && resp.data) { setTitle(resp.data.title || "") setInitialContent(resp.data.content || "") if (resp.data.group_id) { setGroupId(String(resp.data.group_id)) } } else { toast.error( resp.success ? "文章数据为空" : resp.message || "文章不存在", ) router.push("/articles") } setLoading(false) }) } }, [articleId, isNew, router]) const handleSave = useCallback( async (data: { title: string; content: string }) => { if (!groupId) { toast.error("请选择文章分组") return } if (isNew) { const resp = await createArticle({ title: data.title, content: data.content, group_id: Number(groupId), status: 1, }) if (resp.success) { toast.success("文章创建成功") router.push("/articles") } else { toast.error(resp.message || "创建失败") } } else { const id = Number(articleId) const resp = await updateArticle({ id, title: data.title, content: data.content, group_id: Number(groupId), status: 1, }) if (resp.success) { toast.success("文章保存成功") router.push("/articles") } else { toast.error(resp.message || "保存失败") } } }, [isNew, articleId, groupId, router], ) if (loading) { return (
) } if (groups.length === 0) { return (

还没有文章分组,请先创建分组

前往创建分组
) } return (
) }