import { useState, useEffect } from 'react'; import { Get } from '../../../scripts/pocketbase/Get'; import { Authentication } from '../../../scripts/pocketbase/Authentication'; interface LeaderboardStats { totalUsers: number; totalPoints: number; topScore: number; yourPoints: number; yourRank: number | null; } export default function LeaderboardStats() { const [stats, setStats] = useState({ totalUsers: 0, totalPoints: 0, topScore: 0, yourPoints: 0, yourRank: null }); const [loading, setLoading] = useState(true); const [isAuthenticated, setIsAuthenticated] = useState(false); const [currentUserId, setCurrentUserId] = useState(null); const get = Get.getInstance(); const auth = Authentication.getInstance(); // Set the current user ID once on component mount useEffect(() => { try { // Use the Authentication class directly const isLoggedIn = auth.isAuthenticated(); setIsAuthenticated(isLoggedIn); if (isLoggedIn) { const user = auth.getCurrentUser(); if (user && user.id) { setCurrentUserId(user.id); } else { setLoading(false); } } else { setLoading(false); } } catch (err) { console.error('Error checking authentication:', err); setLoading(false); } }, []); useEffect(() => { const fetchStats = async () => { try { setLoading(true); // Get all users without sorting - we'll sort on client side const response = await get.getList('limitedUser', 1, 500, '', '', { fields: ['id', 'name', 'points'] }); // Filter out users with no points for the leaderboard stats const leaderboardUsers = response.items .filter((user: any) => user.points !== undefined && user.points !== null && user.points > 0 ) // Sort by points descending .sort((a: any, b: any) => b.points - a.points); const totalUsers = leaderboardUsers.length; const totalPoints = leaderboardUsers.reduce((sum: number, user: any) => sum + (user.points || 0), 0); const topScore = leaderboardUsers.length > 0 ? leaderboardUsers[0].points : 0; // Find current user's points and rank - BUT don't filter by points > 0 for the current user let yourPoints = 0; let yourRank = null; if (isAuthenticated && currentUserId) { // Look for the current user in ALL users, not just those with points > 0 const currentUser = response.items.find((user: any) => user.id === currentUserId); if (currentUser) { yourPoints = currentUser.points || 0; // Only calculate rank if user has points if (yourPoints > 0) { // Find user position in the sorted array for (let i = 0; i < leaderboardUsers.length; i++) { if (leaderboardUsers[i].id === currentUserId) { yourRank = i + 1; break; } } } } } setStats({ totalUsers, totalPoints, topScore, yourPoints, yourRank }); } catch (err) { console.error('Error fetching leaderboard stats:', err); // Set fallback stats setStats({ totalUsers: 0, totalPoints: 0, topScore: 0, yourPoints: 0, yourRank: null }); } finally { setLoading(false); } }; fetchStats(); }, [isAuthenticated, currentUserId]); if (loading) { return (
{[1, 2, 3, 4].map((i) => (
))}
); } return (
Total Members
{stats.totalUsers}
In the leaderboard
Total Points
{stats.totalPoints}
Earned by all members
Top Score
{stats.topScore}
Highest individual points
Your Score
{isAuthenticated ? stats.yourPoints : '-'}
{isAuthenticated ? (stats.yourRank ? `Ranked #${stats.yourRank}` : 'Not ranked yet') : 'Log in to see your rank' }
); }