Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | import React, { useState, useEffect } from 'react'; import StatsOverview from '../components/stats/StatsOverview'; import PnLChart from '../components/stats/PnLChart'; import CLVSummaryCard from '../components/stats/CLVSummaryCard'; import { BetStats } from '../types/game.types'; import api from '../services/api'; import { formatCurrency, formatPercentage, getSportDisplayName } from '../utils/format'; interface SportBreakdown { sport: string; bets: number; won: number; lost: number; winRate: number; pnl: number; } interface BetTypeBreakdown { betType: string; bets: number; won: number; lost: number; winRate: number; pnl: number; } export default function Stats() { const [stats, setStats] = useState<BetStats | null>(null); const [pnlData, setPnlData] = useState<any[]>([]); const [sportBreakdown, setSportBreakdown] = useState<SportBreakdown[]>([]); const [betTypeBreakdown, setBetTypeBreakdown] = useState<BetTypeBreakdown[]>([]); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); // Date range filter const [dateRange, setDateRange] = useState<'7d' | '30d' | '90d' | 'all'>('30d'); // Fetch stats const fetchStats = async () => { setLoading(true); setError(null); try { const params: any = {}; // Calculate date range if (dateRange !== 'all') { const days = parseInt(dateRange); const startDate = new Date(); startDate.setDate(startDate.getDate() - days); params.startDate = startDate.toISOString(); } const response = await api.get('/bets/stats', { params }); const apiStats = response.data.data; // Access nested data from {status, data} response setStats(apiStats); // Map sport breakdown from API response const sportStats = Object.entries(apiStats.bySport || {}).map(([sport, data]: [string, any]) => { const settledBets = data.won + (data.count - data.won); const lost = data.count - data.won; const winRate = settledBets > 0 ? (data.won / settledBets) * 100 : 0; return { sport, bets: data.count, won: data.won, lost, winRate: Math.round(winRate * 10) / 10, pnl: Math.round(data.netProfit * 100) / 100 }; }); setSportBreakdown(sportStats); // Map bet type breakdown from API response const betTypeStats = Object.entries(apiStats.byBetType || {}).map(([betType, data]: [string, any]) => { const settledBets = data.won + (data.count - data.won); const lost = data.count - data.won; const winRate = settledBets > 0 ? (data.won / settledBets) * 100 : 0; return { betType, bets: data.count, won: data.won, lost, winRate: Math.round(winRate * 10) / 10, pnl: Math.round(data.netProfit * 100) / 100 }; }); setBetTypeBreakdown(betTypeStats); // For P&L chart, we'd need daily bet history // For now, generate from available data (simplified) // TODO: Add daily P&L tracking in backend setPnlData([ { date: new Date().toISOString().split('T')[0], pnl: apiStats.netProfit, cumulativePnl: apiStats.netProfit } ]); setError(null); } catch (err: any) { console.error('Error fetching stats:', err); setError(err.response?.data?.message || 'Failed to load statistics'); } finally { setLoading(false); } }; useEffect(() => { fetchStats(); }, [dateRange]); if (loading) { return ( <div className="min-h-screen bg-gray-50 py-8 px-4 sm:px-6 lg:px-8"> <div className="max-w-7xl mx-auto"> <div className="h-8 w-48 bg-gray-200 rounded animate-pulse mb-8"></div> <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8"> {[1, 2, 3, 4].map((i) => ( <div key={i} className="bg-white rounded-lg shadow-md p-6 h-32 animate-pulse"> <div className="h-4 bg-gray-200 rounded w-1/2 mb-4"></div> <div className="h-8 bg-gray-200 rounded w-3/4"></div> </div> ))} </div> </div> </div> ); } if (error || !stats) { return ( <div className="min-h-screen bg-gray-50 py-8 px-4 sm:px-6 lg:px-8"> <div className="max-w-7xl mx-auto"> <div className="bg-red-50 border border-red-200 rounded-lg p-6 text-center"> <p className="text-red-800 mb-4">{error || 'Failed to load statistics'}</p> <button onClick={fetchStats} className="px-6 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors" > Retry </button> </div> </div> </div> ); } return ( <div className="min-h-screen bg-gray-50 dark:bg-gray-900 py-8 px-4 sm:px-6 lg:px-8 transition-colors"> <div className="max-w-7xl mx-auto"> {/* Header */} <div className="mb-8 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-4"> <div> <h1 className="text-3xl font-display font-bold text-gray-900 dark:text-white text-shadow-pixel tracking-wide">STATISTICS</h1> <p className="mt-2 text-gray-600 dark:text-gray-400"> Analyze your betting performance </p> </div> {/* Date Range Filter */} <div className="flex gap-2"> {(['7d', '30d', '90d', 'all'] as const).map((range) => ( <button key={range} onClick={() => setDateRange(range)} className={` px-4 py-2 rounded-lg font-medium text-sm transition-all ${ dateRange === range ? 'bg-brand-600 text-white shadow-md' : 'bg-white dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 border border-gray-200 dark:border-gray-600' } `} > {range === 'all' ? 'All Time' : range.toUpperCase()} </button> ))} </div> </div> {/* Overview Cards */} <div className="mb-8"> <StatsOverview stats={stats} /> </div> {/* P&L Chart */} <div className="mb-8"> <PnLChart data={pnlData} /> </div> {/* CLV Summary Card */} <div className="mb-8"> <CLVSummaryCard /> </div> {/* Breakdown Tables */} <div className="grid grid-cols-1 lg:grid-cols-2 gap-6"> {/* By Sport */} <div className="card p-6"> <h3 className="font-display font-bold text-base uppercase tracking-widest text-gray-900 dark:text-white mb-4">By Sport</h3> <div className="overflow-x-auto"> <table className="w-full text-sm"> <thead> <tr className="text-left text-gray-600 dark:text-gray-400 border-b border-gray-200 dark:border-gray-700"> <th className="pb-2 font-medium">Sport</th> <th className="pb-2 font-medium text-center">Bets</th> <th className="pb-2 font-medium text-center">Win Rate</th> <th className="pb-2 font-medium text-right">P&L</th> </tr> </thead> <tbody className="divide-y divide-gray-100 dark:divide-gray-700"> {sportBreakdown.map((row) => ( <tr key={row.sport}> <td className="py-3 font-medium text-gray-900 dark:text-white"> {getSportDisplayName(row.sport)} </td> <td className="py-3 text-center text-gray-700 dark:text-gray-300"> {row.bets} </td> <td className="py-3 text-center text-gray-700 dark:text-gray-300"> {formatPercentage(row.winRate)} </td> <td className={`py-3 text-right font-semibold ${row.pnl >= 0 ? 'text-green-600' : 'text-red-600'}`}> {formatCurrency(row.pnl)} </td> </tr> ))} </tbody> </table> </div> </div> {/* By Bet Type */} <div className="card p-6"> <h3 className="font-display font-bold text-base uppercase tracking-widest text-gray-900 dark:text-white mb-4">By Bet Type</h3> <div className="overflow-x-auto"> <table className="w-full text-sm"> <thead> <tr className="text-left text-gray-600 dark:text-gray-400 border-b border-gray-200 dark:border-gray-700"> <th className="pb-2 font-medium">Type</th> <th className="pb-2 font-medium text-center">Bets</th> <th className="pb-2 font-medium text-center">Win Rate</th> <th className="pb-2 font-medium text-right">P&L</th> </tr> </thead> <tbody className="divide-y divide-gray-100 dark:divide-gray-700"> {betTypeBreakdown.map((row) => ( <tr key={row.betType}> <td className="py-3 font-medium text-gray-900 dark:text-white capitalize"> {row.betType} </td> <td className="py-3 text-center text-gray-700 dark:text-gray-300"> {row.bets} </td> <td className="py-3 text-center text-gray-700 dark:text-gray-300"> {formatPercentage(row.winRate)} </td> <td className={`py-3 text-right font-semibold ${row.pnl >= 0 ? 'text-green-600' : 'text-red-600'}`}> {formatCurrency(row.pnl)} </td> </tr> ))} </tbody> </table> </div> </div> </div> </div> </div> ); } |