后端 Go+Gin:认证/CSRF/限流、板块、帖子、评论、点赞、通知、用户资料、置顶推荐;前端 Next.js 16:发帖/编辑/删除、搜索、分页、点赞、通知中心、设置;基础设施 docker-compose 与配置模板;添加 .gitignore 与专有许可证(保留所有权利)
118 lines
3.6 KiB
TypeScript
118 lines
3.6 KiB
TypeScript
"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<User | null>(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 (
|
|
<div className="flex items-center gap-2 flex-wrap justify-end">
|
|
{isAdmin && (
|
|
<>
|
|
<button
|
|
onClick={handlePin}
|
|
disabled={acting !== ""}
|
|
className="flex items-center gap-1 text-sm px-3 py-1.5 rounded-lg hover:opacity-80 disabled:opacity-50"
|
|
style={{
|
|
background: isPinned ? "var(--primary)" : "var(--bg-secondary)",
|
|
color: isPinned ? "white" : "var(--text)",
|
|
}}
|
|
>
|
|
<Pin size={14} /> {isPinned ? "取消置顶" : "置顶"}
|
|
</button>
|
|
<button
|
|
onClick={handleRecommend}
|
|
disabled={acting !== ""}
|
|
className="flex items-center gap-1 text-sm px-3 py-1.5 rounded-lg hover:opacity-80 disabled:opacity-50"
|
|
style={{
|
|
background: isRecommended ? "#e11d48" : "var(--bg-secondary)",
|
|
color: isRecommended ? "white" : "var(--text)",
|
|
}}
|
|
>
|
|
<Star size={14} /> {isRecommended ? "取消推荐" : "推荐"}
|
|
</button>
|
|
</>
|
|
)}
|
|
<Link
|
|
href={`/post/${postId}/edit`}
|
|
className="flex items-center gap-1 text-sm px-3 py-1.5 rounded-lg hover:opacity-80"
|
|
style={{ background: "var(--bg-secondary)", color: "var(--text)" }}
|
|
>
|
|
<Pencil size={14} /> 编辑
|
|
</Link>
|
|
<button
|
|
onClick={handleDelete}
|
|
disabled={deleting}
|
|
className="flex items-center gap-1 text-sm px-3 py-1.5 rounded-lg text-red-600 hover:bg-red-50 disabled:opacity-50"
|
|
style={{ background: "var(--bg-secondary)" }}
|
|
>
|
|
<Trash2 size={14} /> {deleting ? "删除中..." : "删除"}
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|