celo-poll/components/social/CommentThread.tsx
2025-12-08 16:31:30 +03:00

133 lines
4.2 KiB
TypeScript

'use client'
import { useState } from 'react'
import { Heart, Reply, MoreVertical } from 'lucide-react'
import CommentInput from './CommentInput'
import Link from 'next/link'
interface Comment {
id: string
pollId: string
authorFid: string
authorUsername: string
authorPfp?: string
text: string
timestamp: number
likes: number
liked: boolean
replies?: Comment[]
level: number
}
interface CommentThreadProps {
comments: Comment[]
pollId: string
onUpdate: () => void
}
function CommentItem({ comment, pollId, onUpdate }: { comment: Comment; pollId: string; onUpdate: () => void }) {
const [showReply, setShowReply] = useState(false)
const [liking, setLiking] = useState(false)
const handleLike = async () => {
if (liking) return
setLiking(true)
await fetch(`/api/comments/${comment.id}/like`, { method: 'POST' })
setLiking(false)
onUpdate()
}
const timeAgo = (timestamp: number) => {
const seconds = Math.floor((Date.now() - timestamp) / 1000)
if (seconds < 60) return `${seconds}s ago`
if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`
if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`
return `${Math.floor(seconds / 86400)}d ago`
}
return (
<div className={`${comment.level > 0 ? 'ml-8 border-l-2 border-white/10 pl-4' : ''}`}>
<div className="bg-white/5 rounded-lg p-4">
<div className="flex items-start gap-3">
<Link href={`/profile/${comment.authorFid}`}>
{comment.authorPfp ? (
<img src={comment.authorPfp} alt={comment.authorUsername} className="w-10 h-10 rounded-full" />
) : (
<div className="w-10 h-10 rounded-full bg-celo-green/20 flex items-center justify-center">
{comment.authorUsername[0]?.toUpperCase()}
</div>
)}
</Link>
<div className="flex-1">
<div className="flex items-center gap-2">
<Link href={`/profile/${comment.authorFid}`} className="font-semibold hover:underline">
{comment.authorUsername}
</Link>
<span className="text-sm text-gray-400">{timeAgo(comment.timestamp)}</span>
</div>
<p
className="mt-1 text-gray-200"
dangerouslySetInnerHTML={{
__html: comment.text.replace(/@(\w+)/g, '<a href="/profile/$1" class="text-celo-green hover:underline">@$1</a>')
}}
/>
<div className="flex items-center gap-4 mt-3">
<button
onClick={handleLike}
disabled={liking}
className={`flex items-center gap-1 text-sm ${comment.liked ? 'text-red-400' : 'text-gray-400'} hover:text-red-400 transition`}
>
<Heart size={16} fill={comment.liked ? 'currentColor' : 'none'} />
{comment.likes > 0 && comment.likes}
</button>
{comment.level < 3 && (
<button
onClick={() => setShowReply(!showReply)}
className="flex items-center gap-1 text-sm text-gray-400 hover:text-white transition"
>
<Reply size={16} />
Reply
</button>
)}
</div>
</div>
</div>
</div>
{showReply && (
<div className="mt-3 ml-8">
<CommentInput
pollId={pollId}
parentId={comment.id}
onSubmit={() => {
setShowReply(false)
onUpdate()
}}
/>
</div>
)}
{comment.replies && comment.replies.length > 0 && (
<div className="mt-3 space-y-3">
{comment.replies.map(reply => (
<CommentItem key={reply.id} comment={reply} pollId={pollId} onUpdate={onUpdate} />
))}
</div>
)}
</div>
)
}
export default function CommentThread({ comments, pollId, onUpdate }: CommentThreadProps) {
return (
<div className="space-y-4">
{comments.map(comment => (
<CommentItem key={comment.id} comment={comment} pollId={pollId} onUpdate={onUpdate} />
))}
</div>
)
}