135 lines
4.7 KiB
TypeScript
135 lines
4.7 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect } from 'react'
|
|
import { useSearchParams } from 'next/navigation'
|
|
import Link from 'next/link'
|
|
import { Search } from 'lucide-react'
|
|
import { Poll } from '@/types'
|
|
|
|
interface User {
|
|
fid: string
|
|
username: string
|
|
pfpUrl?: string
|
|
totalVotes: number
|
|
}
|
|
|
|
export default function SearchPage() {
|
|
const searchParams = useSearchParams()
|
|
const initialQuery = searchParams.get('q') || ''
|
|
|
|
const [query, setQuery] = useState(initialQuery)
|
|
const [tab, setTab] = useState<'polls' | 'users'>('polls')
|
|
const [polls, setPolls] = useState<Poll[]>([])
|
|
const [users, setUsers] = useState<User[]>([])
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
useEffect(() => {
|
|
if (query.length < 2) return
|
|
|
|
setLoading(true)
|
|
fetch(`/api/search?q=${encodeURIComponent(query)}&type=${tab}`)
|
|
.then(r => r.json())
|
|
.then(data => {
|
|
if (tab === 'polls') {
|
|
setPolls(data.polls || [])
|
|
} else {
|
|
setUsers(data.users || [])
|
|
}
|
|
setLoading(false)
|
|
})
|
|
}, [query, tab])
|
|
|
|
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">
|
|
<h1 className="text-3xl font-bold">🔍 Search</h1>
|
|
|
|
<div className="relative">
|
|
<Search className="absolute left-3 top-3 text-gray-400" size={20} />
|
|
<input
|
|
type="text"
|
|
value={query}
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
placeholder="Search polls or users..."
|
|
className="w-full pl-10 pr-4 py-3 bg-white/10 rounded-xl border border-white/20 focus:border-celo-green outline-none text-white placeholder-gray-400"
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
|
|
<div className="flex gap-2">
|
|
<button
|
|
onClick={() => setTab('polls')}
|
|
className={`px-4 py-2 rounded-lg ${tab === 'polls' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
|
|
>
|
|
Polls
|
|
</button>
|
|
<button
|
|
onClick={() => setTab('users')}
|
|
className={`px-4 py-2 rounded-lg ${tab === 'users' ? 'bg-celo-green text-black' : 'bg-white/10'}`}
|
|
>
|
|
Users
|
|
</button>
|
|
</div>
|
|
|
|
{query.length < 2 ? (
|
|
<div className="text-center py-12 text-gray-400">
|
|
<Search size={48} className="mx-auto mb-4 opacity-50" />
|
|
<p>Type at least 2 characters to search</p>
|
|
</div>
|
|
) : loading ? (
|
|
<div className="text-center py-12">Loading...</div>
|
|
) : tab === 'polls' ? (
|
|
polls.length === 0 ? (
|
|
<div className="text-center py-12 text-gray-400">
|
|
<p>No polls found for "{query}"</p>
|
|
</div>
|
|
) : (
|
|
<div className="space-y-3">
|
|
{polls.map(poll => (
|
|
<Link key={poll.id} href={`/poll/${poll.id}`}>
|
|
<div className="bg-white/10 rounded-xl p-4 hover:bg-white/15 transition">
|
|
<h3 className="font-semibold">{poll.emoji} {poll.question}</h3>
|
|
{poll.description && (
|
|
<p className="text-sm text-gray-400 mt-1">{poll.description}</p>
|
|
)}
|
|
<div className="flex gap-4 mt-2 text-sm text-gray-400">
|
|
<span>👥 {poll.totalVotes} votes</span>
|
|
<span>💰 {poll.totalAmount.toFixed(2)} cUSD</span>
|
|
</div>
|
|
</div>
|
|
</Link>
|
|
))}
|
|
</div>
|
|
)
|
|
) : (
|
|
users.length === 0 ? (
|
|
<div className="text-center py-12 text-gray-400">
|
|
<p>No users found for "{query}"</p>
|
|
</div>
|
|
) : (
|
|
<div className="space-y-3">
|
|
{users.map(user => (
|
|
<Link key={user.fid} href={`/profile/${user.fid}`}>
|
|
<div className="bg-white/10 rounded-xl p-4 hover:bg-white/15 transition flex items-center gap-3">
|
|
{user.pfpUrl ? (
|
|
<img src={user.pfpUrl} alt={user.username} className="w-12 h-12 rounded-full" />
|
|
) : (
|
|
<div className="w-12 h-12 rounded-full bg-celo-green/20 flex items-center justify-center">
|
|
{user.username[0]?.toUpperCase()}
|
|
</div>
|
|
)}
|
|
<div>
|
|
<p className="font-semibold">{user.username}</p>
|
|
<p className="text-sm text-gray-400">{user.totalVotes} votes</p>
|
|
</div>
|
|
</div>
|
|
</Link>
|
|
))}
|
|
</div>
|
|
)
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|