import { PostDto, VisitType } from "@nice/common"; import { motion } from "framer-motion"; import dayjs from "dayjs"; import { Avatar } from "antd"; import { useVisitor } from "@nice/client"; import { useContext, useState } from "react"; import { PostDetailContext } from "./context/PostDetailContext"; import { LikeFilled, LikeOutlined } from "@ant-design/icons"; export default function PostCommentCard({ post, index, isReceiverComment, }: { post: PostDto; index: number; isReceiverComment: boolean; }) { const { user } = useContext(PostDetailContext); const { like, unLike } = useVisitor(); const [liked, setLiked] = useState(post?.liked || false); const [likeCount, setLikeCount] = useState(post?.likes || 0); async function likeThisPost() { if (!liked) { try { setLikeCount((prev) => prev + 1); setLiked(true); like.mutateAsync({ data: { visitorId: user?.id || null, postId: post.id, type: VisitType.LIKE, }, }); } catch (error) { console.error("Failed to like post:", error); setLikeCount((prev) => prev - 1); setLiked(false); } } else { setLikeCount((prev) => prev - 1); setLiked(false); unLike.mutateAsync({ where: { visitorId: user?.id || null, postId: post.id, type: VisitType.LIKE, }, }); } } return (
{!post.author?.avatar && (post.author?.showname || "匿名用户")}
{post.author?.showname || "匿名用户"} {dayjs(post?.createdAt).format("YYYY-MM-DD HH:mm")} {isReceiverComment && ( 官方回答 )}
{/* 添加有帮助按钮 */}
{liked ? : } {likeCount} 有帮助
); }