"use client"; import { useState, useEffect } from "react"; import { useSession } from "next-auth/react"; import { ThumbsUp, MessageSquare, ChevronDown, ChevronUp, Star } from "lucide-react"; import MemberBadge from "@/components/common/MemberBadge"; import { UserLevel } from "@prisma/client"; interface User { id: number; name: string; avatarUrl?: string; level: UserLevel; points: number; } interface Reply { id: number; content: string; likeCount: number; createdAt: string; liked: boolean; user: User; } interface CommentData { id: number; content: string; rating?: number; likeCount: number; isPinned: boolean; createdAt: string; updatedAt: string; replyCount: number; liked: boolean; user: User; replies?: Reply[]; } interface CommentCardProps { comment: CommentData; targetType: "tool" | "skill"; targetSlug: string; onReply: (parentId: number, replyToName: string) => void; onLike: (commentId: number) => void; } function getTimeAgo(date: Date): string { const now = new Date(); const diff = now.getTime() - date.getTime(); const seconds = Math.floor(diff / 1000); const minutes = Math.floor(seconds / 60); const hours = Math.floor(minutes / 60); const days = Math.floor(hours / 24); const months = Math.floor(days / 30); if (seconds < 60) return "刚刚"; if (minutes < 60) return `${minutes}分钟前`; if (hours < 24) return `${hours}小时前`; if (days < 30) return `${days}天前`; if (months < 12) return `${months}个月前`; return date.toLocaleDateString("zh-CN"); } export default function CommentCard({ comment, targetType, targetSlug, onReply, onLike }: CommentCardProps) { const { data: session } = useSession(); const [showReplies, setShowReplies] = useState(false); const [likeAnimating, setLikeAnimating] = useState(false); const handleLike = () => { setLikeAnimating(true); onLike(comment.id); setTimeout(() => setLikeAnimating(false), 300); }; const initial = comment.user.name.charAt(0).toUpperCase(); return (
{comment.isPinned && (
置顶
)}
{comment.user.avatarUrl ? ( ) : (
{initial}
)}
{comment.user.name} {comment.rating && (
{[1, 2, 3, 4, 5].map((s) => ( ))}
)} {getTimeAgo(new Date(comment.createdAt))}

{comment.content}

{comment.replies && comment.replies.length > 0 && (
{showReplies && comment.replies.map((reply) => (
{reply.user.avatarUrl ? ( ) : (
{reply.user.name.charAt(0).toUpperCase()}
)}
{reply.user.name} {getTimeAgo(new Date(reply.createdAt))}

{reply.content}

))}
)}
); }