-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
341 lines (294 loc) · 12.4 KB
/
app.js
File metadata and controls
341 lines (294 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* Repository Data Manager
* Handles loading and managing repository data from a single JSON file
*/
class RepositoryDataManager {
constructor() {
this.repositoryData = [];
this.orgName = 'LizardByte'; // Organization name
this.distBranch = 'dist';
this.rawBase = 'https://raw.githubusercontent.com';
}
/**
* Fetch the last successful workflow run time from GitHub API
*/
async fetchLastWorkflowRun() {
try {
console.log('Fetching last successful workflow run...');
// Fetch workflow runs for the sync-release-assets.yml workflow
const response = await fetch(`https://api.github.com/repos/${this.orgName}/packages/actions/workflows/sync-release-assets.yml/runs?event=schedule&status=success&branch=master&per_page=1`);
if (!response.ok) {
console.warn(`Failed to fetch workflow runs: ${response.status}`);
return null;
}
const data = await response.json();
if (data.workflow_runs && data.workflow_runs.length > 0) {
const lastRun = data.workflow_runs[0];
console.log(`Last successful workflow run: ${lastRun.updated_at}`);
return lastRun.updated_at;
}
return null;
} catch (error) {
console.error('Error fetching workflow run:', error);
return null;
}
}
/**
* Load repository data from packages.json in the dist branch
*/
async loadRepositoryData() {
try {
console.log('Loading repository data from packages.json...');
// Fetch the packages.json file from the dist branch
const response = await fetch(`${this.rawBase}/${this.orgName}/packages/${this.distBranch}/packages.json`);
if (!response.ok) {
throw new Error(`Failed to fetch packages.json: ${response.status}`);
}
const data = await response.json();
// Validate the data structure
if (!data.repositories || !Array.isArray(data.repositories)) {
throw new Error('Invalid packages.json format: missing repositories array');
}
this.repositoryData = data.repositories;
console.log(`Loaded data for ${this.repositoryData.length} repositories from packages.json`);
// Fetch the last successful workflow run time
const lastUpdated = await this.fetchLastWorkflowRun();
return {
repositories: this.repositoryData,
lastUpdated: lastUpdated || new Date().toISOString(),
totalRepositories: this.repositoryData.length,
totalReleases: this.repositoryData.reduce((sum, repo) => sum + (repo.releases ? repo.releases.length : 0), 0),
totalAssets: this.repositoryData.reduce((sum, repo) =>
sum + (repo.releases ? repo.releases.reduce((releaseSum, release) => releaseSum + (release.assetCount || 0), 0) : 0), 0)
};
} catch (error) {
console.error('Error loading repository data:', error);
// Fallback to empty data
this.repositoryData = [];
return {
repositories: [],
error: error.message,
lastUpdated: null,
totalRepositories: 0,
totalReleases: 0,
totalAssets: 0
};
}
}
/**
* Get all repository data
*/
getRepositories() {
return this.repositoryData;
}
/**
* Filter repositories based on search term and archived status
*/
filterRepositories(searchTerm, showArchived = false) {
let filteredRepos = this.repositoryData;
// Filter by archived status first
if (!showArchived) {
filteredRepos = filteredRepos.filter(repo => !repo.archived);
}
// Then filter by search term if provided
if (!searchTerm) {
return filteredRepos;
}
return filteredRepos.filter(repo => {
const repoMatch = repo.name.toLowerCase().includes(searchTerm.toLowerCase());
const releaseMatch = repo.releases?.some(release =>
release.tag.toLowerCase().includes(searchTerm.toLowerCase()));
return repoMatch || releaseMatch;
});
}
}
/**
* UI Manager
* Handles all DOM manipulation and rendering
*/
class UIManager {
constructor() {
this.repositoryGrid = document.getElementById('repositoryGrid');
this.searchInput = document.getElementById('searchInput');
this.repoCountElement = document.getElementById('repoCount');
this.releaseCountElement = document.getElementById('releaseCount');
this.assetCountElement = document.getElementById('assetCount');
this.updateTimeElement = document.getElementById('updateTime');
this.orgName = 'LizardByte';
this.lastUpdated = null; // Store lastUpdated from packages.json
}
/**
* Render repositories in the grid
*/
renderRepositories(repos) {
if (repos.length === 0) {
this.repositoryGrid.innerHTML = '<div class="col-12 text-center fst-italic py-5">No repositories found.</div>';
return;
}
this.repositoryGrid.innerHTML = repos.map(repo => {
// Show only the latest 5 releases
const displayReleases = repo.releases ? repo.releases.slice(0, 5) : [];
const hasMoreReleases = repo.releases && repo.releases.length > 5;
const remainingCount = hasMoreReleases ? repo.releases.length - 5 : 0;
// Extract ternary operation for better readability
const releaseText = remainingCount > 1 ? 's' : '';
return `
<div class="col-lg-4 col-md-6 mb-4" data-repo="${repo.name.toLowerCase()}" ${repo.archived ? 'data-archived="true"' : ''}>
<div class="card h-100 shadow border-0 rounded-0">
<div class="card-body p-4 rounded-0">
<h5 class="card-title text-info mb-3">
${repo.name}
${repo.archived ? '<span class="badge bg-warning text-dark ms-2">Archived</span>' : ''}
</h5>
<ul class="list-group list-group-flush">
${displayReleases.length > 0 ? displayReleases.map(release => `
<li class="list-group-item d-flex justify-content-between align-items-center px-0">
<a href="https://github.com/${this.orgName}/packages/tree/dist/${repo.name}/${release.tag}"
class="text-decoration-none fw-medium" target="_blank" rel="noopener">
${release.tag}
</a>
<span class="badge bg-secondary rounded-pill">${release.assetCount}</span>
</li>
`).join('') : '<li class="list-group-item">No releases found</li>'}
${hasMoreReleases ? `
<li class="list-group-item px-0 text-center">
<a href="https://github.com/${this.orgName}/packages/tree/dist/${repo.name}"
class="btn btn-outline-primary btn-sm" target="_blank" rel="noopener">
Show ${remainingCount} more release${releaseText}
</a>
</li>
` : ''}
</ul>
</div>
</div>
</div>
`;
}).join('');
}
/**
* Update statistics display
*/
updateStats(repos) {
const repoCount = repos.length;
const releaseCount = repos.reduce((sum, repo) => sum + (repo.releases ? repo.releases.length : 0), 0);
const assetCount = repos.reduce((sum, repo) =>
sum + (repo.releases ? repo.releases.reduce((releaseSum, release) => releaseSum + (release.assetCount || 0), 0) : 0), 0);
this.repoCountElement.textContent = repoCount;
this.releaseCountElement.textContent = releaseCount;
this.assetCountElement.textContent = assetCount;
if (this.lastUpdated) { // Only set update time if lastUpdated is set
this.updateTimeElement.textContent = this.formatUpdateTime(this.lastUpdated);
}
}
/**
* Set the last updated time from packages.json
*/
setUpdateTime(lastUpdated) {
this.lastUpdated = lastUpdated;
this.updateTimeElement.textContent = this.formatUpdateTime(lastUpdated);
}
/**
* Format the update time for display
*/
formatUpdateTime(isoString) {
if (!isoString) return '-';
const date = new Date(isoString);
if (Number.isNaN(date.getTime())) return isoString;
return date.toLocaleString();
}
/**
* Show loading state
*/
showLoading() {
this.repositoryGrid.innerHTML = '<div class="col-12 text-center fst-italic py-5">Loading repository data...</div>';
this.repoCountElement.textContent = '-';
this.releaseCountElement.textContent = '-';
this.assetCountElement.textContent = '-';
this.updateTimeElement.textContent = '-';
this.lastUpdated = null;
}
}
/**
* Filter Manager
* Handles search and archived repository filtering functionality
*/
class FilterManager {
constructor(dataManager, uiManager) {
this.dataManager = dataManager;
this.uiManager = uiManager;
this.searchInput = document.getElementById('searchInput');
this.archivedToggle = document.getElementById('showArchivedToggle');
this.initializeFilters();
}
/**
* Initialize filter functionality
*/
initializeFilters() {
// Search input handler
this.searchInput.addEventListener('input', (e) => {
this.applyFilters();
});
// Archived toggle handler
this.archivedToggle.addEventListener('change', (e) => {
this.applyFilters();
});
}
/**
* Apply all filters and update UI
*/
applyFilters() {
const searchTerm = this.searchInput.value;
const showArchived = this.archivedToggle.checked;
const filteredRepos = this.dataManager.filterRepositories(searchTerm, showArchived);
this.uiManager.renderRepositories(filteredRepos);
this.uiManager.updateStats(filteredRepos);
}
/**
* Reset all filters
*/
resetFilters() {
this.searchInput.value = '';
this.archivedToggle.checked = false;
this.applyFilters();
}
}
/**
* Main Application
* Coordinates all components and manages application state
*/
class LizardByteAssetsApp {
constructor() {
this.dataManager = new RepositoryDataManager();
this.uiManager = new UIManager();
this.filterManager = null;
this.lastUpdated = null;
}
/**
* Initialize the application
*/
async init() {
try {
// Show loading state
this.uiManager.showLoading();
// Load repository data from packages.json
const data = await this.dataManager.loadRepositoryData();
this.lastUpdated = data.lastUpdated;
this.uiManager.setUpdateTime(this.lastUpdated);
// Initialize filter functionality first
this.filterManager = new FilterManager(this.dataManager, this.uiManager);
// Apply initial filters (this will render repositories and update stats)
this.filterManager.applyFilters();
const repositories = this.dataManager.getRepositories();
console.log(`Loaded ${repositories.length} repositories`);
} catch (error) {
console.error('Failed to initialize application:', error);
this.uiManager.repositoryGrid.innerHTML =
'<div class="col-12 text-center fst-italic py-5">Failed to load repository data. Please try again later.</div>';
}
}
}
// Initialize the application when the DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
const app = new LizardByteAssetsApp();
app.init();
});