Skip to content

Commit 4282050

Browse files
author
Vic-Nas
committed
Tried fixing
1 parent a85400d commit 4282050

1 file changed

Lines changed: 35 additions & 43 deletions

File tree

script.js

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const state = {
66
folderCache: {},
77
currentView: 'loading',
88
currentPath: [],
9-
currentItem: null,
10-
isLoading: true
9+
currentItem: null
1110
};
1211

1312
// Utility
@@ -16,25 +15,18 @@ function getRepoPath() {
1615
return parts[1] && parts[2] ? `${parts[1]}/${parts[2]}` : 'Vic-Nas/PythonSolutions';
1716
}
1817

19-
// Fetch folder contents with better caching
18+
// Fetch folder contents
2019
async function fetchFolderContents(path) {
21-
const cacheKey = path;
22-
const cached = state.folderCache[cacheKey];
23-
24-
// Cache for 2 minutes
25-
if (cached && cached.data && Date.now() - cached.timestamp < 120000) {
26-
return cached.data;
20+
if (state.folderCache[path]) {
21+
return state.folderCache[path];
2722
}
2823

2924
try {
3025
const res = await fetch(`https://api.github.com/repos/${getRepoPath()}/contents/${path}`);
3126
if (!res.ok) return null;
3227

3328
const items = await res.json();
34-
state.folderCache[cacheKey] = {
35-
data: items,
36-
timestamp: Date.now()
37-
};
29+
state.folderCache[path] = items;
3830
return items;
3931
} catch (err) {
4032
console.error(`Error fetching ${path}:`, err);
@@ -56,19 +48,16 @@ function hasFiles(items) {
5648
async function loadPlatforms() {
5749
console.log('Loading platforms...');
5850
const rootItems = await fetchFolderContents('');
59-
if (!rootItems) {
60-
console.error('Failed to load root items');
61-
return;
62-
}
51+
if (!rootItems) return;
6352

6453
const platformFolders = rootItems.filter(item =>
6554
item.type === 'dir' &&
6655
!['utils', '.git'].includes(item.name) &&
6756
!item.name.startsWith('.')
6857
);
6958

70-
// Build all platforms first
71-
const platforms = [];
59+
// Clear platforms before loading
60+
state.platforms = [];
7261

7362
for (const folder of platformFolders) {
7463
const platformData = {
@@ -78,25 +67,31 @@ async function loadPlatforms() {
7867
count: 0
7968
};
8069

81-
// Check for platform.png
82-
const contents = await fetchFolderContents(folder.name);
83-
if (contents) {
84-
const platformImg = contents.find(f => f.name === 'platform.png');
85-
if (platformImg) {
86-
// Use the download_url from GitHub API
87-
platformData.image = platformImg.download_url || `${folder.name}/platform.png`;
88-
}
89-
90-
// Count items recursively
91-
platformData.count = await countItems(folder.name);
92-
}
70+
// Add platform immediately so it shows up
71+
state.platforms.push(platformData);
9372

94-
platforms.push(platformData);
73+
// Then load details asynchronously
74+
(async () => {
75+
// Check for platform.png
76+
const contents = await fetchFolderContents(folder.name);
77+
if (contents) {
78+
const platformImg = contents.find(f => f.name === 'platform.png');
79+
if (platformImg) {
80+
platformData.image = platformImg.download_url || `${folder.name}/platform.png`;
81+
}
82+
83+
// Count items recursively
84+
platformData.count = await countItems(folder.name);
85+
86+
// Re-render to show updated count
87+
if (state.currentView === 'platforms') {
88+
renderPlatforms();
89+
}
90+
}
91+
})();
9592
}
9693

97-
platforms.sort((a, b) => a.name.localeCompare(b.name));
98-
state.platforms = platforms;
99-
94+
state.platforms.sort((a, b) => a.name.localeCompare(b.name));
10095
console.log('Loaded platforms:', state.platforms);
10196
}
10297

@@ -368,9 +363,7 @@ async function renderProblem() {
368363
}
369364

370365
view.innerHTML = html;
371-
if (typeof hljs !== 'undefined') {
372-
hljs.highlightAll();
373-
}
366+
hljs.highlightAll();
374367
}
375368

376369
// Navigation
@@ -467,17 +460,16 @@ window.addEventListener('hashchange', async () => {
467460
return;
468461
}
469462

470-
// Make goBack globally available for onclick handler
463+
// Make goBack globally available
471464
window.goBack = goBack;
472465

473-
// Load platforms
474-
await loadPlatforms();
466+
// Start loading platforms but don't wait
467+
loadPlatforms();
475468

476-
// Parse initial hash and render
469+
// Immediately parse hash and show the requested view
477470
const parsed = parseHash();
478471
state.currentView = parsed.view;
479472
state.currentPath = parsed.path;
480-
state.isLoading = false;
481473

482474
console.log('Initial state:', state);
483475

0 commit comments

Comments
 (0)