/** * @file Leaderboard page component showing top players across categories. * @copyright nhcarrigan * @license Naomi's Public License * @author Naomi Carrigan */ /* eslint-disable max-lines-per-function -- Complex component with many render paths */ /* eslint-disable complexity -- Many conditional render paths for categories and entries */ import { useEffect, useState, type JSX } from "react"; import type { LeaderboardCategory, LeaderboardEntry } from "@elysium/types"; interface CategoryConfig { id: LeaderboardCategory; label: string; icon: string; formatValue: (value: number)=> string; } const goldSuffixes = [ "", "K", "M", "B", "T", "Qa", "Qt", "S", "Sp", "O", "N", "D", ]; /** * Formats a gold value with a short suffix. * @param value - The gold amount to format. * @returns The formatted string. */ const formatGold = (value: number): string => { if (value === 0) { return "0"; } const tier = Math.floor(Math.log10(Math.abs(value)) / 3); const clamped = Math.min(tier, goldSuffixes.length - 1); const scaled = value / Math.pow(1000, clamped); return `${String(Number.parseFloat(scaled.toFixed(2)))}${goldSuffixes[clamped] ?? ""}`; }; const categories: Array = [ { formatValue: (v): string => { return formatGold(v); }, icon: "πŸͺ™", id: "totalGold", label: "Lifetime Gold", }, { formatValue: (v): string => { return v.toLocaleString(); }, icon: "πŸ’€", id: "bossesDefeated", label: "Bosses Defeated", }, { formatValue: (v): string => { return v.toLocaleString(); }, icon: "πŸ“œ", id: "questsCompleted", label: "Quests Completed", }, { formatValue: (v): string => { return v.toLocaleString(); }, icon: "πŸ†", id: "achievementsUnlocked", label: "Achievements", }, { formatValue: (v): string => { return v.toLocaleString(); }, icon: "⭐", id: "prestigeCount", label: "Prestige", }, { formatValue: (v): string => { return v.toLocaleString(); }, icon: "🌌", id: "transcendenceCount", label: "Transcendence", }, { formatValue: (v): string => { return v.toLocaleString(); }, icon: "✨", id: "apotheosisCount", label: "Apotheosis", }, ]; const rankBadges: Record = { 1: "πŸ₯‡", 2: "πŸ₯ˆ", 3: "πŸ₯‰" }; /** * Renders the leaderboard page with category tabs and player rankings. * @returns The JSX element. */ const LeaderboardPage = (): JSX.Element => { const [ category, setCategory ] = useState("totalGold"); const [ entries, setEntries ] = useState>([]); const [ loading, setLoading ] = useState(true); const [ error, setError ] = useState(null); useEffect(() => { setLoading(true); setError(null); fetch(`/api/leaderboards?category=${category}&limit=100`). then(async(response) => { if (!response.ok) { throw new Error("Failed to load leaderboard"); } // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- API response cast const data = (await response.json()) as { entries: Array; }; setEntries(data.entries); }). catch((error_: unknown) => { setError( error_ instanceof Error ? error_.message : "Failed to load leaderboard", ); }). finally(() => { setLoading(false); }); }, [ category ]); const currentConfig = categories.find((cat) => { return cat.id === category; }) ?? categories[0]; return (

{"πŸ† Leaderboards"}

{"The mightiest adventurers in Elysium"}

{"πŸ”„ Rankings update when you prestige."}

{categories.map((cat) => { function handleCategoryClick(): void { setCategory(cat.id); } return ( ); })}
{loading ?
{"Loading…"}
: null} {error === null ? null :
{"⚠️ "} {error}
} {!loading && error === null && entries.length === 0 &&
{"No entries yet β€” be the first on the board!"}
} {!loading && error === null && entries.length > 0 &&
{"Rank"} {"Player"} {currentConfig?.icon} {currentConfig?.label}
{entries.map((entry) => { const avatarUrl = entry.avatar === null ? `https://cdn.discordapp.com/embed/avatars/${String(Number.parseInt(entry.discordId, 10) % 5)}.png` : `https://cdn.discordapp.com/avatars/${entry.discordId}/${entry.avatar}.png?size=32`; const displayName = entry.characterName === "" ? entry.username : entry.characterName; return ( {rankBadges[entry.rank] ?? `#${String(entry.rank)}`} {displayName} {displayName} {entry.activeTitle === "" ? null : {entry.activeTitle} } {currentConfig?.formatValue(entry.value)} ); })}
}
{"βš”οΈ Play Elysium"}

{"Players can opt out via their profile settings."}

); }; export { LeaderboardPage };