"use client"; import { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { Loader2 } from "lucide-react"; import Modal from "@/components/Modal"; import Avatar from "@/components/Avatar"; import MarkdownBodyClient from "@/components/MarkdownBodyClient"; import { apiAdminCommentHistory, type CommentEditHistoryItem, } from "@/lib/api"; const PAGE_SIZE = 10; /** * 版主可见:评论历次编辑前的旧正文快照(倒序分页)。 * 宽度对齐详情页评论主栏(约 800px+),长文可滚动;底部「加载更多」。 */ export default function CommentEditHistoryModal({ open, commentId, onClose, }: { open: boolean; commentId: number | null; onClose: () => void; }) { const [loading, setLoading] = useState(false); const [loadingMore, setLoadingMore] = useState(false); const [error, setError] = useState(""); const [items, setItems] = useState([]); const [page, setPage] = useState(1); const [total, setTotal] = useState(0); const hasMore = items.length < total; const loadPage = useCallback( async (nextPage: number, append: boolean) => { if (commentId == null) return; if (append) setLoadingMore(true); else { setLoading(true); setError(""); } try { const res = await apiAdminCommentHistory(commentId, nextPage, PAGE_SIZE); const batch = res.items || []; setError(""); setTotal(res.total ?? 0); setPage(res.page ?? nextPage); setItems((prev) => (append ? [...prev, ...batch] : batch)); } catch (e) { const msg = e instanceof Error ? e.message : "加载失败"; if (append) { // 追加失败不清空已有列表 setError(msg); } else { setError(msg); setItems([]); setTotal(0); } } finally { setLoading(false); setLoadingMore(false); } }, [commentId] ); useEffect(() => { if (!open || commentId == null) return; setItems([]); setPage(1); setTotal(0); setError(""); void loadPage(1, false); }, [open, commentId, loadPage]); return ( 0 ? `共 ${total} 条修订记录` : undefined} onClose={onClose} // 对齐详情页中间评论栏可用宽度(1440 三栏布局下约 800px) maxWidthClass="max-w-[min(840px,calc(100vw-2rem))]" panelClassName="p-5 sm:p-6 max-h-[85vh] flex flex-col min-h-0 overflow-hidden" bodyClassName="overflow-y-auto min-h-0 flex-1 -mx-1 px-1" footer={ } > {loading ? (
加载中…
) : error && items.length === 0 ? (

{error}

) : items.length === 0 ? (

暂无编辑记录

) : (
    {items.map((it) => { const name = it.editor.nickname || it.editor.username || "用户"; return (
  • {name}

    {new Date(it.created_at).toLocaleString("zh-CN")} · 编辑前正文

  • ); })}
{error && items.length > 0 && (

{error}

)} {hasMore && (
)}
)}
); }