|
| 1 | +import type React from "react" |
| 2 | +import { useEffect, useState } from "react" |
| 3 | +import "../index.css" |
| 4 | +import "./leaderboards.css" |
| 5 | + |
| 6 | +export interface GlobalLeaderboardEntry { |
| 7 | + user_id: string |
| 8 | + username: string |
| 9 | + primary_wallet: string |
| 10 | + total_score: number |
| 11 | +} |
| 12 | + |
| 13 | +export interface GuildLeaderboardEntry { |
| 14 | + guild_id: string |
| 15 | + guild_name: string |
| 16 | + icon_url: string | null |
| 17 | + total_score: number |
| 18 | + member_count: number |
| 19 | +} |
| 20 | + |
| 21 | +const fetchLeaderboard = async <T,>(endpoint: string, limit = 10): Promise<T[]> => { |
| 22 | + const res = await fetch(`/api/leaderboards/${endpoint}?limit=${limit}`) |
| 23 | + if (!res.ok) throw new Error("Failed to fetch leaderboard") |
| 24 | + const json = await res.json() |
| 25 | + if (!json.ok) throw new Error(json.error || "Unknown error") |
| 26 | + return json.data as T[] |
| 27 | +} |
| 28 | + |
| 29 | +const LeaderboardTable: React.FC<{ |
| 30 | + title: string |
| 31 | + entries: GlobalLeaderboardEntry[] | GuildLeaderboardEntry[] |
| 32 | + type: "global" | "guilds" |
| 33 | +}> = ({ title, entries, type }) => ( |
| 34 | + <div className="leaderboard-card"> |
| 35 | + <h2 className="leaderboard-title">{title}</h2> |
| 36 | + <table className="leaderboard-table"> |
| 37 | + <thead> |
| 38 | + <tr> |
| 39 | + <th>Rank</th> |
| 40 | + {type === "global" ? <th>Player</th> : <th>Guild</th>} |
| 41 | + <th>Score</th> |
| 42 | + {type === "guilds" && <th>Members</th>} |
| 43 | + </tr> |
| 44 | + </thead> |
| 45 | + <tbody> |
| 46 | + {entries.length === 0 ? ( |
| 47 | + <tr> |
| 48 | + <td colSpan={type === "guilds" ? 4 : 3}>No data</td> |
| 49 | + </tr> |
| 50 | + ) : ( |
| 51 | + entries.map((entry, idx) => ( |
| 52 | + <tr |
| 53 | + key={ |
| 54 | + type === "global" |
| 55 | + ? (entry as GlobalLeaderboardEntry).user_id |
| 56 | + : (entry as GuildLeaderboardEntry).guild_id |
| 57 | + } |
| 58 | + > |
| 59 | + <td>{idx + 1}</td> |
| 60 | + {type === "global" ? ( |
| 61 | + <td>{(entry as GlobalLeaderboardEntry).username}</td> |
| 62 | + ) : ( |
| 63 | + <td> |
| 64 | + {(entry as GuildLeaderboardEntry).icon_url && ( |
| 65 | + <img |
| 66 | + src={(entry as GuildLeaderboardEntry).icon_url!} |
| 67 | + alt="" |
| 68 | + style={{ |
| 69 | + width: 24, |
| 70 | + height: 24, |
| 71 | + marginRight: 8, |
| 72 | + verticalAlign: "middle", |
| 73 | + borderRadius: "50%", |
| 74 | + }} |
| 75 | + /> |
| 76 | + )} |
| 77 | + {(entry as GuildLeaderboardEntry).guild_name} |
| 78 | + </td> |
| 79 | + )} |
| 80 | + <td> |
| 81 | + {type === "global" |
| 82 | + ? (entry as GlobalLeaderboardEntry).total_score |
| 83 | + : (entry as GuildLeaderboardEntry).total_score} |
| 84 | + </td> |
| 85 | + {type === "guilds" && <td>{(entry as GuildLeaderboardEntry).member_count}</td>} |
| 86 | + </tr> |
| 87 | + )) |
| 88 | + )} |
| 89 | + </tbody> |
| 90 | + </table> |
| 91 | + </div> |
| 92 | +) |
| 93 | + |
| 94 | +const Leaderboards: React.FC = () => { |
| 95 | + const [global, setGlobal] = useState<GlobalLeaderboardEntry[]>([]) |
| 96 | + const [guilds, setGuilds] = useState<GuildLeaderboardEntry[]>([]) |
| 97 | + const [loading, setLoading] = useState(true) |
| 98 | + const [error, setError] = useState<string | null>(null) |
| 99 | + |
| 100 | + useEffect(() => { |
| 101 | + setLoading(true) |
| 102 | + setError(null) |
| 103 | + Promise.all([ |
| 104 | + fetchLeaderboard<GlobalLeaderboardEntry>("global", 10), |
| 105 | + fetchLeaderboard<GuildLeaderboardEntry>("guilds", 10), |
| 106 | + ]) |
| 107 | + .then(([globalData, guildData]) => { |
| 108 | + setGlobal(globalData) |
| 109 | + setGuilds(guildData) |
| 110 | + }) |
| 111 | + .catch((e) => setError(e.message)) |
| 112 | + .finally(() => setLoading(false)) |
| 113 | + }, []) |
| 114 | + |
| 115 | + if (loading) return <div className="leaderboards-loading">Loading leaderboards...</div> |
| 116 | + if (error) return <div className="leaderboards-error">{error}</div> |
| 117 | + |
| 118 | + return ( |
| 119 | + <div className="leaderboards-container"> |
| 120 | + <LeaderboardTable title="Top Players" entries={global} type="global" /> |
| 121 | + <LeaderboardTable title="Top Guilds" entries={guilds} type="guilds" /> |
| 122 | + </div> |
| 123 | + ) |
| 124 | +} |
| 125 | + |
| 126 | +export default Leaderboards |
0 commit comments