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

163 lines
6.3 KiB
TypeScript

'use client'
import { useState, useEffect } from 'react'
import { useSearchParams } from 'next/navigation'
import { Search, TrendingUp, Clock, Users, DollarSign, Filter } from 'lucide-react'
import Link from 'next/link'
import { Poll } from '@/types'
export default function DiscoverPage() {
const searchParams = useSearchParams()
const categoryParam = searchParams.get('category')
const [polls, setPolls] = useState<Poll[]>([])
const [search, setSearch] = useState('')
const [filter, setFilter] = useState<'trending' | 'recent' | 'popular'>('trending')
const [status, setStatus] = useState<'all' | 'active' | 'closed'>('all')
const [category, setCategory] = useState(categoryParam || 'all')
const [sort, setSort] = useState<'newest' | 'popular' | 'ending' | 'pot'>('newest')
const [loading, setLoading] = useState(true)
const [showFilters, setShowFilters] = useState(false)
useEffect(() => {
const params = new URLSearchParams()
if (search) params.set('search', search)
if (status !== 'all') params.set('status', status)
if (category !== 'all') params.set('category', category)
params.set('sort', sort)
fetch(`/api/discover?${params.toString()}`)
.then(r => r.json())
.then(data => {
setPolls(data.polls || [])
setLoading(false)
})
}, [search, status, category, sort])
return (
<div className="min-h-screen bg-gradient-to-b from-celo-dark to-black text-white p-4">
<div className="max-w-6xl mx-auto space-y-6">
<h1 className="text-3xl font-bold">🔍 Discover Polls</h1>
<div className="relative">
<Search className="absolute left-3 top-3 text-gray-400" size={20} />
<input
type="text"
placeholder="Search polls..."
value={search}
onChange={(e) => setSearch(e.target.value)}
className="w-full pl-10 pr-4 py-3 bg-white/10 rounded-xl border border-white/20 focus:border-celo-green outline-none"
/>
</div>
<div className="flex flex-wrap gap-2">
<button
onClick={() => setShowFilters(!showFilters)}
className="flex items-center gap-2 px-4 py-2 bg-white/10 rounded-lg hover:bg-white/20"
>
<Filter size={16} />
Filters
</button>
<button
onClick={() => setFilter('trending')}
className={`flex items-center gap-2 px-4 py-2 rounded-lg ${filter === 'trending' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
<TrendingUp size={16} />
Trending
</button>
<button
onClick={() => setFilter('recent')}
className={`flex items-center gap-2 px-4 py-2 rounded-lg ${filter === 'recent' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
<Clock size={16} />
Recent
</button>
<button
onClick={() => setFilter('popular')}
className={`flex items-center gap-2 px-4 py-2 rounded-lg ${filter === 'popular' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
<Users size={16} />
Popular
</button>
</div>
{showFilters && (
<div className="bg-white/10 rounded-xl p-4 space-y-4">
<div>
<label className="block text-sm text-gray-400 mb-2">Status</label>
<div className="flex gap-2">
<button
onClick={() => setStatus('all')}
className={`px-3 py-1 rounded-lg text-sm ${status === 'all' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
All
</button>
<button
onClick={() => setStatus('active')}
className={`px-3 py-1 rounded-lg text-sm ${status === 'active' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
Active
</button>
<button
onClick={() => setStatus('closed')}
className={`px-3 py-1 rounded-lg text-sm ${status === 'closed' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
Closed
</button>
</div>
</div>
<div>
<label className="block text-sm text-gray-400 mb-2">Sort By</label>
<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('popular')}
className={`px-3 py-1 rounded-lg text-sm ${sort === 'popular' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
Most Votes
</button>
<button
onClick={() => setSort('pot')}
className={`px-3 py-1 rounded-lg text-sm ${sort === 'pot' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
>
Biggest Pot
</button>
</div>
</div>
</div>
)}
{loading ? (
<div className="text-center py-12">Loading...</div>
) : (
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
{polls.map(poll => (
<Link key={poll.id} href={`/poll/${poll.id}`}>
<div className="bg-white/10 rounded-xl p-4 hover:bg-white/20 transition">
<div className="flex items-start justify-between">
<div className="flex-1">
<h3 className="text-lg font-semibold">{poll.emoji} {poll.question}</h3>
<p className="text-sm text-gray-400 mt-1">{poll.description}</p>
<div className="flex gap-4 mt-3 text-sm text-gray-400">
<span>👥 {poll.totalVotes} votes</span>
<span>💰 {poll.totalAmount.toFixed(2)} cUSD</span>
</div>
</div>
</div>
</div>
</Link>
))}
</div>
)}
</div>
</div>
)
}