103 lines
4.1 KiB
TypeScript
103 lines
4.1 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
export default function AnalyticsPage() {
|
|
const [stats, setStats] = useState<any>(null)
|
|
|
|
useEffect(() => {
|
|
loadAnalytics()
|
|
}, [])
|
|
|
|
const loadAnalytics = async () => {
|
|
try {
|
|
const res = await fetch('/api/analytics/user')
|
|
const data = await res.json()
|
|
setStats(data)
|
|
} catch (error) {
|
|
console.error('Failed to load analytics:', error)
|
|
}
|
|
}
|
|
|
|
if (!stats) {
|
|
return <div className="min-h-screen bg-gray-900 flex items-center justify-center">
|
|
<div className="text-white text-xl">Loading analytics...</div>
|
|
</div>
|
|
}
|
|
|
|
const winRate = stats.totalVotes > 0 ? (stats.totalWins / stats.totalVotes * 100).toFixed(1) : 0
|
|
const roi = stats.totalSpent > 0 ? ((stats.totalEarned - stats.totalSpent) / stats.totalSpent * 100).toFixed(1) : 0
|
|
|
|
return (
|
|
<div className="min-h-screen bg-gradient-to-br from-gray-900 to-gray-800 p-4">
|
|
<div className="max-w-6xl mx-auto pt-20">
|
|
<h1 className="text-3xl font-bold text-white mb-8">馃搳 Your Analytics</h1>
|
|
|
|
{/* Key Metrics */}
|
|
<div className="grid grid-cols-2 md:grid-cols-4 gap-4 mb-8">
|
|
<div className="bg-gray-800 rounded-xl p-6 text-center">
|
|
<div className="text-3xl font-bold text-celo-green">{stats.totalVotes}</div>
|
|
<div className="text-sm text-gray-400 mt-1">Total Votes</div>
|
|
</div>
|
|
<div className="bg-gray-800 rounded-xl p-6 text-center">
|
|
<div className="text-3xl font-bold text-celo-yellow">{winRate}%</div>
|
|
<div className="text-sm text-gray-400 mt-1">Win Rate</div>
|
|
</div>
|
|
<div className="bg-gray-800 rounded-xl p-6 text-center">
|
|
<div className="text-3xl font-bold text-white">{stats.totalEarned}</div>
|
|
<div className="text-sm text-gray-400 mt-1">cUSD Earned</div>
|
|
</div>
|
|
<div className="bg-gray-800 rounded-xl p-6 text-center">
|
|
<div className={`text-3xl font-bold ${parseFloat(roi) >= 0 ? 'text-green-500' : 'text-red-500'}`}>
|
|
{roi}%
|
|
</div>
|
|
<div className="text-sm text-gray-400 mt-1">ROI</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Category Performance */}
|
|
<div className="bg-gray-800 rounded-xl p-6 mb-8">
|
|
<h2 className="text-xl font-bold text-white mb-4">Performance by Category</h2>
|
|
<div className="space-y-4">
|
|
{stats.byCategory?.map((cat: any) => (
|
|
<div key={cat.category}>
|
|
<div className="flex justify-between text-sm mb-2">
|
|
<span className="text-white">{cat.category}</span>
|
|
<span className="text-gray-400">{cat.wins}/{cat.votes} wins</span>
|
|
</div>
|
|
<div className="w-full bg-gray-700 rounded-full h-2">
|
|
<div
|
|
className="bg-celo-green h-2 rounded-full"
|
|
style={{ width: `${(cat.wins / cat.votes * 100)}%` }}
|
|
/>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Recent Activity */}
|
|
<div className="bg-gray-800 rounded-xl p-6">
|
|
<h2 className="text-xl font-bold text-white mb-4">Recent Activity</h2>
|
|
<div className="space-y-3">
|
|
{stats.recentVotes?.map((vote: any) => (
|
|
<div key={vote.id} className="flex items-center justify-between py-3 border-b border-gray-700">
|
|
<div>
|
|
<div className="text-white font-medium">{vote.pollQuestion}</div>
|
|
<div className="text-sm text-gray-400">{vote.option}</div>
|
|
</div>
|
|
<div className="text-right">
|
|
<div className={`font-bold ${vote.won ? 'text-green-500' : 'text-red-500'}`}>
|
|
{vote.won ? '+' : '-'}{vote.amount} cUSD
|
|
</div>
|
|
<div className="text-xs text-gray-400">{new Date(vote.timestamp).toLocaleDateString()}</div>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|