|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useEffect, useState } from 'react'; |
| 4 | +import Navbar from "@/components/Navbar"; |
| 5 | +import { useAuth } from "@/hooks/useAuth"; |
| 6 | +import TopThree from '@/components/leaderboard/TopThree'; |
| 7 | +import LeaderboardTable from '@/components/leaderboard/LeaderboardTable'; |
| 8 | +import { Team } from '@/components/leaderboard/data'; |
| 9 | + |
| 10 | +export default function LeaderboardPage() { |
| 11 | + const [teams, setTeams] = useState<Team[]>([]); |
| 12 | + const [loading, setLoading] = useState(true); |
| 13 | + const [error, setError] = useState<string | null>(null); |
| 14 | + |
| 15 | + const { isAuthenticated, isLoading: authLoading, login } = useAuth(); |
| 16 | + const API_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8000"; |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + if (authLoading) return; |
| 20 | + |
| 21 | + if (!isAuthenticated) { |
| 22 | + setLoading(false); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const fetchLeaderboard = async () => { |
| 27 | + try { |
| 28 | + const res = await fetch(`${API_URL}/leaderboard`); |
| 29 | + if (!res.ok) { |
| 30 | + throw new Error('Failed to fetch leaderboard data'); |
| 31 | + } |
| 32 | + const data = await res.json(); |
| 33 | + |
| 34 | + if (Array.isArray(data)) { |
| 35 | + setTeams(data); |
| 36 | + } else if (Array.isArray(data.data)) { |
| 37 | + setTeams(data.data); |
| 38 | + } else { |
| 39 | + setTeams([]); |
| 40 | + console.error("Unexpected data format", data); |
| 41 | + } |
| 42 | + } catch (err) { |
| 43 | + console.error("Error fetching leaderboard:", err); |
| 44 | + setError("Failed to load leaderboard data."); |
| 45 | + } finally { |
| 46 | + setLoading(false); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + fetchLeaderboard(); |
| 51 | + }, [API_URL, isAuthenticated, authLoading]); |
| 52 | + |
| 53 | + if (authLoading) { |
| 54 | + return <div className="min-h-screen bg-[#0b0b0b] flex items-center justify-center text-white font-mono">[ AUTH_CHECK... ]</div>; |
| 55 | + } |
| 56 | + |
| 57 | + if (!isAuthenticated) { |
| 58 | + return ( |
| 59 | + <> |
| 60 | + <Navbar /> |
| 61 | + <main className="min-h-screen w-full bg-[#0b0b0b] text-white pt-24 pb-12 px-4 md:px-8 relative overflow-hidden flex flex-col items-center justify-center"> |
| 62 | + <div className="absolute inset-0 bg-[url('https://grainy-gradients.vercel.app/noise.svg')] opacity-20 pointer-events-none"></div> |
| 63 | + <div className="z-10 text-center border border-red-500/30 p-8 rounded-lg bg-red-900/10 backdrop-blur"> |
| 64 | + <h1 className="text-2xl font-bold font-mono text-red-500 mb-4">[ ACCESS_DENIED ]</h1> |
| 65 | + <p className="text-gray-400 font-mono mb-6">Restricted Area // Authorization Required</p> |
| 66 | + <button |
| 67 | + onClick={login} |
| 68 | + className="bg-[#CCFF00] text-black px-6 py-2 rounded font-bold tracking-widest hover:bg-[#b8e600] transition" |
| 69 | + > |
| 70 | + LOGIN_TO_ACCESS |
| 71 | + </button> |
| 72 | + </div> |
| 73 | + </main> |
| 74 | + </> |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + const topTeams = teams.slice(0, 3); |
| 79 | + const restTeams = teams.slice(3); |
| 80 | + |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <Navbar /> |
| 84 | + <main className="min-h-screen w-full bg-[#0b0b0b] text-white pt-24 pb-12 px-4 md:px-8 relative overflow-hidden"> |
| 85 | + {/* Background Noise/Grid Overlay */} |
| 86 | + <div className="absolute inset-0 bg-[url('https://grainy-gradients.vercel.app/noise.svg')] opacity-20 pointer-events-none"></div> |
| 87 | + |
| 88 | + <div className="max-w-7xl mx-auto relative z-10 flex flex-col items-center"> |
| 89 | + |
| 90 | + {/* Header Section */} |
| 91 | + <div className="text-center mb-12 w-full"> |
| 92 | + <h1 className="text-4xl md:text-6xl font-black tracking-tighter uppercase mb-4 font-mono"> |
| 93 | + LEADERBOARD<span className="text-gray-600 mx-2">//</span> |
| 94 | + <span className="text-lime-400 text-shadow-neon">GLOBAL_NET_STATS</span> |
| 95 | + </h1> |
| 96 | + |
| 97 | + <div className="flex justify-between items-center max-w-4xl mx-auto text-xs md:text-sm font-mono text-gray-400 border-t border-b border-gray-800 py-2 mt-4"> |
| 98 | + <span>[ STATUS=<span className="text-lime-500">LIVE_EVALUATION</span> ]</span> |
| 99 | + <div className="flex items-center"> |
| 100 | + <span>TOTAL_CLUSTERS= {teams.length}</span> |
| 101 | + <div className={`w-2 h-2 ${loading ? 'bg-yellow-500' : 'bg-lime-500'} rounded-full ml-2 animate-pulse`}></div> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + |
| 106 | + {loading ? ( |
| 107 | + <div className="flex flex-col items-center justify-center h-64 font-mono text-lime-400"> |
| 108 | + <div className="w-16 h-16 border-4 border-lime-400 border-t-transparent rounded-full animate-spin mb-4"></div> |
| 109 | + <div>[ LOADING_DATA_STREAM... ]</div> |
| 110 | + </div> |
| 111 | + ) : error ? ( |
| 112 | + <div className="text-red-500 font-mono text-center border border-red-900/50 bg-red-900/10 p-4 rounded"> |
| 113 | + [ ERROR: {error} ] |
| 114 | + </div> |
| 115 | + ) : ( |
| 116 | + <> |
| 117 | + {/* Top 3 Section */} |
| 118 | + {topTeams.length > 0 && <TopThree teams={topTeams} />} |
| 119 | + |
| 120 | + {/* Data Grid Section */} |
| 121 | + <LeaderboardTable teams={restTeams} /> |
| 122 | + </> |
| 123 | + )} |
| 124 | + |
| 125 | + </div> |
| 126 | + </main> |
| 127 | + </> |
| 128 | + ); |
| 129 | +} |
0 commit comments