"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { Pencil, Trash2, Pin, Star } from "lucide-react"; import { apiMe, apiDeletePost, apiTogglePin, apiToggleRecommend, type User } from "@/lib/api"; interface PostActionsProps { postId: string; authorId: number; pinned: number; recommended: boolean; } export default function PostActions({ postId, authorId, pinned, recommended }: PostActionsProps) { const router = useRouter(); const [user, setUser] = useState(null); const [deleting, setDeleting] = useState(false); const [isPinned, setIsPinned] = useState(pinned > 0); const [isRecommended, setIsRecommended] = useState(recommended); const [acting, setActing] = useState(""); useEffect(() => { apiMe() .then((res) => setUser(res.user || null)) .catch(() => setUser(null)); }, []); // 仅作者本人或管理员可编辑/删除;置顶/推荐仅管理员 const canManage = user && (user.id === authorId || user.role === "admin"); const isAdmin = user?.role === "admin"; if (!canManage) return null; const handleDelete = async () => { if (!confirm("确定要删除这篇帖子吗?此操作不可恢复。")) return; setDeleting(true); try { const res = await apiDeletePost(postId); if (res.message) { router.push("/"); } else { alert(res.error || "删除失败"); } } finally { setDeleting(false); } }; const handlePin = async () => { setActing("pin"); try { const res = await apiTogglePin(postId); setIsPinned(res.pinned > 0); router.refresh(); } finally { setActing(""); } }; const handleRecommend = async () => { setActing("recommend"); try { const res = await apiToggleRecommend(postId); setIsRecommended(res.recommended); router.refresh(); } finally { setActing(""); } }; return (
{isAdmin && ( <> )} 编辑
); }