@@ -19,9 +19,28 @@ document.addEventListener('DOMContentLoaded', function() {
1919 let totalPages = 1 ;
2020 let guildId = '' ;
2121
22- // Extract guild ID from URL
23- const pathParts = window . location . pathname . split ( '/' ) ;
24- guildId = pathParts [ pathParts . length - 1 ] ;
22+ // Extract guild ID from URL or session storage (for GitHub Pages)
23+ const getGuildId = ( ) => {
24+ // Check if running on GitHub Pages
25+ const isGitHubPages = window . location . hostname . includes ( 'github.io' ) ||
26+ window . location . hostname . includes ( 'testify.lol' ) ;
27+
28+ if ( isGitHubPages ) {
29+ // Get guild ID from session storage (set by the routing page)
30+ return sessionStorage . getItem ( 'guildId' ) ;
31+ } else {
32+ // Standard path extraction for development environment
33+ const pathParts = window . location . pathname . split ( '/' ) ;
34+ return pathParts [ pathParts . length - 1 ] ;
35+ }
36+ } ;
37+
38+ guildId = getGuildId ( ) ;
39+
40+ if ( ! guildId ) {
41+ showError ( "No guild ID found. Please go back and try again." ) ;
42+ return ;
43+ }
2544
2645 // Initialize
2746 fetchLeaderboard ( currentPage ) ;
@@ -52,13 +71,36 @@ document.addEventListener('DOMContentLoaded', function() {
5271 } ) ;
5372
5473 globalBtn . addEventListener ( 'click' , ( ) => {
55- window . location . href = '/leaderboard/global' ;
74+ // Handle GitHub Pages routing
75+ if ( window . location . hostname . includes ( 'github.io' ) ||
76+ window . location . hostname . includes ( 'testify.lol' ) ) {
77+ window . location . href = '/leaderboard/global' ;
78+ } else {
79+ window . location . href = '/leaderboard/global' ;
80+ }
5681 } ) ;
5782
83+ // API endpoint handling for GitHub Pages
84+ function getApiEndpoint ( endpoint ) {
85+ // Check if running on GitHub Pages
86+ const isGitHubPages = window . location . hostname . includes ( 'github.io' ) ||
87+ window . location . hostname . includes ( 'testify.lol' ) ;
88+
89+ if ( isGitHubPages ) {
90+ // For GitHub Pages, use the mock data endpoint
91+ return `https://api.testify.lol${ endpoint } ` ;
92+ } else {
93+ // For local development
94+ return endpoint ;
95+ }
96+ }
97+
5898 // Functions
5999 async function fetchLeaderboard ( page ) {
60100 try {
61- const response = await fetch ( `/api/leaderboard/${ guildId } ?page=${ page } ` ) ;
101+ const apiUrl = getApiEndpoint ( `/api/leaderboard/${ guildId } ?page=${ page } ` ) ;
102+
103+ const response = await fetch ( apiUrl ) ;
62104
63105 if ( ! response . ok ) {
64106 throw new Error ( `Server returned ${ response . status } : ${ response . statusText } ` ) ;
@@ -76,11 +118,15 @@ document.addEventListener('DOMContentLoaded', function() {
76118 // Update guild info if available
77119 if ( data . guildId ) {
78120 try {
79- const discordApiResponse = await fetch ( `/api/guild/${ data . guildId } ` ) ;
121+ const discordApiResponse = await fetch ( getApiEndpoint ( `/api/guild/${ data . guildId } ` ) ) ;
80122 if ( discordApiResponse . ok ) {
81123 const guildData = await discordApiResponse . json ( ) ;
82124 guildNameEl . textContent = guildData . name || 'Guild Leaderboard' ;
83125 guildInfoEl . textContent = `${ guildData . memberCount || 'Unknown' } members` ;
126+ } else {
127+ // Fallback if API call fails
128+ guildNameEl . textContent = 'Guild Leaderboard' ;
129+ guildInfoEl . textContent = `Guild ID: ${ data . guildId } ` ;
84130 }
85131 } catch ( e ) {
86132 console . warn ( 'Could not fetch guild details:' , e ) ;
@@ -111,15 +157,15 @@ document.addEventListener('DOMContentLoaded', function() {
111157 // Try to fetch user info from Discord API
112158 let userInfo ;
113159 try {
114- const userResponse = await fetch ( `/api/user/${ user . User } ` ) ;
160+ const userResponse = await fetch ( getApiEndpoint ( `/api/user/${ user . User } ` ) ) ;
115161 if ( userResponse . ok ) {
116162 userInfo = await userResponse . json ( ) ;
117163 }
118164 } catch ( e ) {
119165 console . warn ( `Could not fetch user info for ${ user . User } :` , e ) ;
120166 }
121167
122- const userTag = userInfo ?. tag || 'Unknown User' ;
168+ const userTag = userInfo ?. tag || user . User || 'Unknown User' ;
123169 const avatarUrl = userInfo ?. avatarURL || `https://cdn.discordapp.com/embed/avatars/${ Math . floor ( Math . random ( ) * 5 ) } .png` ;
124170
125171 row . innerHTML = `
0 commit comments