122 lines
4.4 KiB
TypeScript
122 lines
4.4 KiB
TypeScript
'use client'
|
|
|
|
import { Poll } from '@/types'
|
|
import { formatTimeRemaining, getPercentage, formatCUSD } from '@/lib/format'
|
|
import { useEffect, useState } from 'react'
|
|
|
|
interface PollCardProps {
|
|
poll: Poll
|
|
userVoteOptionId?: string
|
|
}
|
|
|
|
export default function PollCard({ poll, userVoteOptionId }: PollCardProps) {
|
|
const [timeRemaining, setTimeRemaining] = useState(formatTimeRemaining(poll.endTime))
|
|
|
|
useEffect(() => {
|
|
const interval = setInterval(() => {
|
|
setTimeRemaining(formatTimeRemaining(poll.endTime))
|
|
}, 1000)
|
|
|
|
return () => clearInterval(interval)
|
|
}, [poll.endTime])
|
|
|
|
const totalVotes = poll.options.reduce((sum, opt) => sum + opt.votes, 0)
|
|
|
|
return (
|
|
<div className="bg-white rounded-2xl shadow-xl overflow-hidden">
|
|
{/* Header */}
|
|
<div className="bg-gradient-to-r from-celo-green to-celo-yellow p-6">
|
|
<div className="flex items-center gap-3">
|
|
<span className="text-5xl">{poll.emoji}</span>
|
|
<div className="flex-1">
|
|
<h2 className="text-2xl font-bold text-white">{poll.question}</h2>
|
|
{poll.description && (
|
|
<p className="text-white/90 text-sm mt-1">{poll.description}</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center justify-between mt-4 text-white">
|
|
<div className="bg-white/20 backdrop-blur-sm rounded-lg px-4 py-2">
|
|
<p className="text-xs opacity-90">Time Left</p>
|
|
<p className="font-bold">{timeRemaining}</p>
|
|
</div>
|
|
<div className="bg-white/20 backdrop-blur-sm rounded-lg px-4 py-2">
|
|
<p className="text-xs opacity-90">Total Pot</p>
|
|
<p className="font-bold">{formatCUSD(poll.totalPot)}</p>
|
|
</div>
|
|
<div className="bg-white/20 backdrop-blur-sm rounded-lg px-4 py-2">
|
|
<p className="text-xs opacity-90">Total Votes</p>
|
|
<p className="font-bold">{totalVotes}</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Options */}
|
|
<div className="p-6 space-y-3">
|
|
{poll.options.map((option) => {
|
|
const percentage = getPercentage(option.votes, totalVotes)
|
|
const isUserVote = option.id === userVoteOptionId
|
|
const isCorrect = poll.status === 'closed' && option.id === poll.correctOptionId
|
|
|
|
return (
|
|
<div
|
|
key={option.id}
|
|
className={`relative rounded-xl border-2 overflow-hidden transition-all ${
|
|
isUserVote
|
|
? 'border-celo-green bg-celo-green/10'
|
|
: isCorrect
|
|
? 'border-celo-yellow bg-celo-yellow/10'
|
|
: 'border-gray-200 bg-gray-50'
|
|
}`}
|
|
>
|
|
{/* Progress bar */}
|
|
<div
|
|
className={`absolute inset-0 transition-all ${
|
|
isCorrect ? 'bg-celo-yellow/20' : 'bg-celo-green/10'
|
|
}`}
|
|
style={{ width: `${percentage}%` }}
|
|
/>
|
|
|
|
{/* Content */}
|
|
<div className="relative p-4 flex items-center justify-between">
|
|
<div className="flex items-center gap-3">
|
|
<span className="text-2xl">{option.emoji}</span>
|
|
<div>
|
|
<p className="font-semibold text-gray-800">{option.text}</p>
|
|
<p className="text-xs text-gray-600">
|
|
{option.votes} votes · {formatCUSD(option.totalAmount)}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
{isUserVote && (
|
|
<span className="bg-celo-green text-white text-xs px-2 py-1 rounded-full font-semibold">
|
|
Your Vote
|
|
</span>
|
|
)}
|
|
{isCorrect && (
|
|
<span className="bg-celo-yellow text-white text-xs px-2 py-1 rounded-full font-semibold">
|
|
✓ Winner
|
|
</span>
|
|
)}
|
|
<span className="text-xl font-bold text-gray-700">{percentage}%</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
})}
|
|
</div>
|
|
|
|
{/* Status */}
|
|
{poll.status === 'closed' && (
|
|
<div className="bg-gray-100 p-4 text-center">
|
|
<p className="text-sm text-gray-600">
|
|
Poll closed · {poll.winnersCount} winners · 95% pot distributed
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|