2025-12-08 16:31:30 +03:00

120 lines
3.9 KiB
TypeScript

'use client'
import { useState, useEffect } from 'react'
import { useParams } from 'next/navigation'
import Link from 'next/link'
import { ArrowLeft, SortAsc } from 'lucide-react'
import CommentThread from '@/components/social/CommentThread'
import CommentInput from '@/components/social/CommentInput'
interface Comment {
id: string
pollId: string
authorFid: string
authorUsername: string
authorPfp?: string
text: string
timestamp: number
likes: number
liked: boolean
replies?: Comment[]
level: number
}
export default function CommentsPage() {
const params = useParams()
const pollId = params.id as string
const [comments, setComments] = useState<Comment[]>([])
const [displayCount, setDisplayCount] = useState(20)
const [sort, setSort] = useState<'newest' | 'oldest' | 'top'>('newest')
const [loading, setLoading] = useState(true)
const loadComments = () => {
fetch(`/api/poll/${pollId}/comments`)
.then(r => r.json())
.then(data => {
let sorted = data.comments || []
if (sort === 'oldest') {
sorted = sorted.sort((a: Comment, b: Comment) => a.timestamp - b.timestamp)
} else if (sort === 'top') {
sorted = sorted.sort((a: Comment, b: Comment) => b.likes - a.likes)
} else {
sorted = sorted.sort((a: Comment, b: Comment) => b.timestamp - a.timestamp)
}
setComments(sorted)
setLoading(false)
})
}
useEffect(() => {
loadComments()
}, [pollId, sort])
const displayedComments = comments.slice(0, displayCount)
const hasMore = displayCount < comments.length
return (
<div className="min-h-screen bg-gradient-to-b from-celo-dark to-black text-white p-4">
<div className="max-w-2xl mx-auto space-y-6">
<div className="flex items-center gap-4">
<Link href={`/poll/${pollId}`} className="text-gray-400 hover:text-white">
<ArrowLeft size={24} />
</Link>
<h1 className="text-2xl font-bold">💬 Comments</h1>
</div>
<div className="flex items-center justify-between">
<span className="text-gray-400">{comments.length} comments</span>
<div className="flex gap-2">
<button
onClick={() => setSort('newest')}
className={`px-3 py-1 rounded-lg text-sm ${sort === 'newest' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
Newest
</button>
<button
onClick={() => setSort('oldest')}
className={`px-3 py-1 rounded-lg text-sm ${sort === 'oldest' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
Oldest
</button>
<button
onClick={() => setSort('top')}
className={`px-3 py-1 rounded-lg text-sm ${sort === 'top' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
Top
</button>
</div>
</div>
<CommentInput pollId={pollId} onSubmit={loadComments} />
{loading ? (
<div className="text-center py-12">Loading...</div>
) : comments.length === 0 ? (
<div className="text-center py-12 text-gray-400">
<p className="text-4xl mb-4">💬</p>
<p>No comments yet</p>
<p className="text-sm mt-2">Be the first to share your thoughts!</p>
</div>
) : (
<>
<CommentThread comments={displayedComments} pollId={pollId} onUpdate={loadComments} />
{hasMore && (
<button
onClick={() => setDisplayCount(prev => prev + 20)}
className="w-full bg-white/10 hover:bg-white/20 text-white py-3 rounded-xl transition"
>
Load more ({comments.length - displayCount} remaining)
</button>
)}
</>
)}
</div>
</div>
)
}