Skip to content

Commit 67d7e23

Browse files
committed
Debug
1 parent 7e59dd3 commit 67d7e23

1 file changed

Lines changed: 71 additions & 78 deletions

File tree

script.js

Lines changed: 71 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
console.log('Script loaded');
2+
13
// State
24
const state = {
35
data: { leetcode: [], kattis: [], vicutils: [] },
@@ -14,6 +16,7 @@ function getRepoPath() {
1416

1517
// Fetch repository data
1618
async function fetchRepoData() {
19+
console.log('Fetching repo data...');
1720
const platforms = ['leetcode', 'kattis', 'vicutils'];
1821

1922
for (const platform of platforms) {
@@ -25,7 +28,6 @@ async function fetchRepoData() {
2528
const problems = [];
2629

2730
if (platform === 'vicutils') {
28-
// VicUtils: scan for HTML files only
2931
for (const item of items) {
3032
if (item.type === 'file' && item.name.endsWith('.html')) {
3133
problems.push({
@@ -37,7 +39,6 @@ async function fetchRepoData() {
3739
}
3840
}
3941
} else {
40-
// LeetCode/Kattis: scan directories
4142
const dirs = items.filter(i => i.type === 'dir');
4243

4344
for (const dir of dirs) {
@@ -70,14 +71,57 @@ async function fetchRepoData() {
7071
}
7172

7273
state.data[platform] = problems.sort((a, b) => a.name.localeCompare(b.name));
74+
console.log(`Loaded ${problems.length} items for ${platform}`);
7375
} catch (err) {
7476
console.error(`Error loading ${platform}:`, err);
7577
}
7678
}
7779
}
7880

81+
// Parse hash to get view state
82+
function parseHash() {
83+
const hash = window.location.hash.slice(1);
84+
console.log('Parsing hash:', hash);
85+
86+
if (!hash) {
87+
return { view: 'platforms', platform: null, problem: null };
88+
}
89+
90+
if (hash.startsWith('view/')) {
91+
const remainder = hash.slice(5); // Remove 'view/'
92+
const slashIndex = remainder.indexOf('/');
93+
94+
if (slashIndex === -1) {
95+
return { view: 'platforms', platform: null, problem: null };
96+
}
97+
98+
const platform = remainder.slice(0, slashIndex);
99+
const problemName = decodeURIComponent(remainder.slice(slashIndex + 1));
100+
101+
console.log('Parsed view - platform:', platform, 'problem:', problemName);
102+
103+
const problem = state.data[platform]?.find(p => p.name === problemName);
104+
105+
if (problem) {
106+
console.log('Found problem:', problem);
107+
return { view: 'problem', platform, problem };
108+
} else {
109+
console.warn('Problem not found. Available:', state.data[platform]?.map(p => p.name));
110+
return { view: 'list', platform, problem: null };
111+
}
112+
}
113+
114+
if (['leetcode', 'kattis', 'vicutils'].includes(hash)) {
115+
return { view: 'list', platform: hash, problem: null };
116+
}
117+
118+
return { view: 'platforms', platform: null, problem: null };
119+
}
120+
79121
// Render views
80122
function render() {
123+
console.log('Rendering view:', state.currentView);
124+
81125
const views = ['loading-screen', 'platform-selector', 'problem-list', 'problem-view'];
82126
views.forEach(v => document.getElementById(v).style.display = 'none');
83127

@@ -140,6 +184,8 @@ async function renderProblemView() {
140184
const platform = state.currentPlatform;
141185
const problem = state.currentProblem;
142186

187+
console.log('Rendering problem:', problem.name, 'from platform:', platform);
188+
143189
// Fetch Python code if available
144190
let pythonCode = '';
145191
let problemUrl = '';
@@ -171,8 +217,8 @@ async function renderProblemView() {
171217
let html = `
172218
<div class="problem-header">
173219
<div class="problem-nav">
174-
<a href="javascript:history.back()" class="nav-link">← Back</a>
175-
<a href="${window.location.pathname}" class="nav-link">🏠 Home</a>
220+
<a href="#${platform}" class="nav-link">← Back</a>
221+
<a href="#" class="nav-link">🏠 Home</a>
176222
${problemUrl ? `<a href="${problemUrl}" target="_blank" class="nav-link">🔗 Problem</a>` : ''}
177223
<a href="${repoUrl}" target="_blank" class="nav-link">📂 GitHub</a>
178224
</div>
@@ -256,13 +302,13 @@ async function renderProblemView() {
256302

257303
// Navigation
258304
function navigateToPlatform(platform) {
259-
state.currentView = 'list';
260-
state.currentPlatform = platform;
305+
console.log('Navigating to platform:', platform);
261306
window.location.hash = platform;
262-
render();
263307
}
264308

265309
function navigateToProblem(platform, problem) {
310+
console.log('Navigating to problem:', problem.name);
311+
266312
// Check if has HTML page
267313
if (problem.files.html.length > 0) {
268314
const htmlFile = problem.files.html[0].name;
@@ -272,20 +318,13 @@ function navigateToProblem(platform, problem) {
272318
return;
273319
}
274320

275-
state.currentView = 'problem';
276-
state.currentPlatform = platform;
277-
state.currentProblem = problem;
278321
const encodedName = encodeURIComponent(problem.name);
279322
window.location.hash = `view/${platform}/${encodedName}`;
280-
render();
281323
}
282324

283325
function navigateToHome() {
284-
state.currentView = 'platforms';
285-
state.currentPlatform = null;
286-
state.currentProblem = null;
326+
console.log('Navigating to home');
287327
window.location.hash = '';
288-
render();
289328
}
290329

291330
// Utilities
@@ -311,84 +350,38 @@ document.addEventListener('click', e => {
311350
document.getElementById('back-to-platforms')?.addEventListener('click', navigateToHome);
312351

313352
window.addEventListener('hashchange', () => {
314-
const hash = window.location.hash.slice(1);
315-
console.log('Hash changed to:', hash);
316-
317-
if (!hash) {
318-
navigateToHome();
319-
} else if (hash.startsWith('view/')) {
320-
// Remove 'view/' prefix first
321-
const remainder = hash.substring(5);
322-
// Split only on the first slash to separate platform from problem name
323-
const firstSlashIndex = remainder.indexOf('/');
324-
if (firstSlashIndex === -1) {
325-
navigateToHome();
326-
} else {
327-
const platform = remainder.substring(0, firstSlashIndex);
328-
const problemName = decodeURIComponent(remainder.substring(firstSlashIndex + 1));
329-
console.log('hashchange - Looking for problem:', problemName, 'in platform:', platform);
330-
const problem = state.data[platform]?.find(p => p.name === problemName);
331-
if (problem) {
332-
state.currentView = 'problem';
333-
state.currentPlatform = platform;
334-
state.currentProblem = problem;
335-
render();
336-
} else {
337-
// Problem not found, go to platform list
338-
state.currentView = 'list';
339-
state.currentPlatform = platform;
340-
render();
341-
}
342-
}
343-
} else if (['leetcode', 'kattis', 'vicutils'].includes(hash)) {
344-
navigateToPlatform(hash);
345-
}
353+
console.log('Hash changed');
354+
const parsed = parseHash();
355+
state.currentView = parsed.view;
356+
state.currentPlatform = parsed.platform;
357+
state.currentProblem = parsed.problem;
358+
render();
346359
});
347360

348361
// Initialize
349362
(async () => {
350-
// Check if we're on index.html or root path
363+
console.log('Initializing app...');
364+
365+
// Check if we're on index page
351366
const isIndexPage = window.location.pathname.endsWith('index.html') ||
352367
window.location.pathname.endsWith('/') ||
353368
window.location.pathname.split('/').pop() === '';
354369

370+
console.log('Is index page:', isIndexPage);
371+
355372
if (!isIndexPage) {
356-
// We're on a direct problem HTML page - don't initialize the SPA
373+
console.log('Not index page, skipping initialization');
357374
return;
358375
}
359376

360377
await fetchRepoData();
361378

362-
const hash = window.location.hash.slice(1);
379+
const parsed = parseHash();
380+
state.currentView = parsed.view;
381+
state.currentPlatform = parsed.platform;
382+
state.currentProblem = parsed.problem;
363383

364-
if (hash.startsWith('view/')) {
365-
// Remove 'view/' prefix first
366-
const remainder = hash.substring(5);
367-
// Split only on the first slash to separate platform from problem name
368-
const firstSlashIndex = remainder.indexOf('/');
369-
if (firstSlashIndex === -1) {
370-
state.currentView = 'platforms';
371-
} else {
372-
const platform = remainder.substring(0, firstSlashIndex);
373-
const problemName = decodeURIComponent(remainder.substring(firstSlashIndex + 1));
374-
console.log('Looking for problem:', problemName, 'in platform:', platform);
375-
console.log('Available problems:', state.data[platform]?.map(p => p.name));
376-
const problem = state.data[platform]?.find(p => p.name === problemName);
377-
if (problem) {
378-
state.currentView = 'problem';
379-
state.currentPlatform = platform;
380-
state.currentProblem = problem;
381-
} else {
382-
console.warn('Problem not found:', problemName);
383-
state.currentView = 'platforms';
384-
}
385-
}
386-
} else if (['leetcode', 'kattis', 'vicutils'].includes(hash)) {
387-
state.currentView = 'list';
388-
state.currentPlatform = hash;
389-
} else {
390-
state.currentView = 'platforms';
391-
}
384+
console.log('Initial state:', state);
392385

393386
render();
394387
})();

0 commit comments

Comments
 (0)