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

181 lines
6.9 KiB
TypeScript

'use client'
import { useEffect, useState } from 'react'
import { useRouter } from 'next/navigation'
import { sdk } from '@farcaster/miniapp-sdk'
import Header from '@/components/Header'
import FAB from '@/components/FAB'
import { Poll, Vote } from '@/types'
import { getAllPolls } from '@/lib/db/polls'
import { getUserVote, calculateWinnings } from '@/lib/db/polls'
import { formatCUSD, formatTimeRemaining } from '@/lib/format'
export default function HistoryPage() {
const router = useRouter()
const [polls, setPolls] = useState<Poll[]>([])
const [userId, setUserId] = useState<string>('')
const [userVotes, setUserVotes] = useState<Map<string, Vote>>(new Map())
const [isLoading, setIsLoading] = useState(true)
useEffect(() => {
const loadData = async () => {
try {
// Get user context
const context = await sdk.context
const fid = context?.user?.fid?.toString() || 'anonymous'
setUserId(fid)
// Load all polls
const allPolls = await getAllPolls()
setPolls(allPolls)
// Load user votes for each poll
const votes = new Map<string, Vote>()
for (const poll of allPolls) {
const vote = await getUserVote(poll.id, fid)
if (vote) {
votes.set(poll.id, vote)
}
}
setUserVotes(votes)
} catch (error) {
console.error('Failed to load history:', error)
} finally {
setIsLoading(false)
}
}
loadData()
}, [])
if (isLoading) {
return (
<div className="min-h-screen flex items-center justify-center">
<div className="text-center">
<div className="text-4xl mb-4 animate-spin">⏳</div>
<p className="text-gray-600">Loading history...</p>
</div>
</div>
)
}
const totalWinnings = polls.reduce((sum, poll) => {
const vote = userVotes.get(poll.id)
if (!vote) return sum
return sum + calculateWinnings(poll, vote)
}, 0)
const totalVoted = Array.from(userVotes.values()).reduce((sum, vote) => sum + vote.amount, 0)
return (
<div className="min-h-screen pb-20">
<Header />
<main className="max-w-4xl mx-auto p-4 space-y-6">
<div className="bg-white rounded-2xl p-6 shadow-xl">
<h1 className="text-2xl font-bold text-gray-800 mb-4">📜 Poll History</h1>
{/* Stats */}
<div className="grid grid-cols-3 gap-4 mb-6">
<div className="bg-gradient-to-br from-celo-green to-celo-yellow rounded-xl p-4 text-white">
<p className="text-xs opacity-90">Polls Voted</p>
<p className="text-2xl font-bold">{userVotes.size}</p>
</div>
<div className="bg-gray-100 rounded-xl p-4">
<p className="text-xs text-gray-600">Total Voted</p>
<p className="text-lg font-bold text-gray-800">{formatCUSD(totalVoted)}</p>
</div>
<div className="bg-celo-yellow/20 rounded-xl p-4">
<p className="text-xs text-gray-600">Total Won</p>
<p className="text-lg font-bold text-celo-dark">{formatCUSD(totalWinnings)}</p>
</div>
</div>
</div>
{/* Poll List */}
{polls.length === 0 ? (
<div className="bg-white rounded-2xl p-8 text-center">
<div className="text-6xl mb-4">📭</div>
<h2 className="text-xl font-bold text-gray-800 mb-2">No polls yet</h2>
<p className="text-gray-600">Vote on today&apos;s poll to see it here!</p>
<button
onClick={() => router.push('/')}
className="mt-4 bg-celo-green text-white font-semibold py-2 px-6 rounded-lg hover:bg-celo-green/90 transition-all"
>
Go to Today&apos;s Poll
</button>
</div>
) : (
<div className="space-y-4">
{polls.map((poll) => {
const userVote = userVotes.get(poll.id)
const userOption = userVote ? poll.options.find(opt => opt.id === userVote.optionId) : null
const userWon = userVote && poll.status === 'closed' && userVote.optionId === poll.correctOptionId
const winnings = userVote ? calculateWinnings(poll, userVote) : 0
return (
<div
key={poll.id}
onClick={() => router.push(`/poll/${poll.id}`)}
className="bg-white rounded-xl p-5 shadow-md hover:shadow-xl transition-all cursor-pointer"
>
<div className="flex items-start justify-between mb-3">
<div className="flex items-center gap-3">
<span className="text-3xl">{poll.emoji}</span>
<div>
<h3 className="font-bold text-gray-800">{poll.question}</h3>
<p className="text-sm text-gray-600">{formatTimeRemaining(poll.endTime)}</p>
</div>
</div>
{poll.status === 'closed' ? (
<span className="bg-gray-200 text-gray-700 text-xs px-3 py-1 rounded-full font-semibold">
Closed
</span>
) : (
<span className="bg-celo-green text-white text-xs px-3 py-1 rounded-full font-semibold">
Active
</span>
)}
</div>
{userVote ? (
<div className="flex items-center justify-between">
<div>
<p className="text-sm text-gray-600">
You voted: <span className="font-semibold">{userOption?.text}</span> ({formatCUSD(userVote.amount)})
</p>
</div>
{poll.status === 'closed' && (
<div>
{userWon ? (
<div className="text-right">
<span className="bg-celo-yellow text-white text-xs px-2 py-1 rounded-full font-semibold">
✓ Won
</span>
<p className="text-celo-green font-bold text-sm mt-1">
+{formatCUSD(winnings)}
</p>
</div>
) : (
<span className="bg-gray-200 text-gray-600 text-xs px-2 py-1 rounded-full">
Lost
</span>
)}
</div>
)}
</div>
) : (
<p className="text-sm text-gray-500">You didn&apos;t vote on this poll</p>
)}
</div>
)
})}
</div>
)}
</main>
<FAB />
</div>
)
}