-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
352 lines (292 loc) · 10.2 KB
/
app.js
File metadata and controls
352 lines (292 loc) · 10.2 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
342
343
344
345
346
347
348
349
350
351
352
document.addEventListener('DOMContentLoaded', function() {
const helpIcon = document.getElementById('helpIcon');
const searchHelp = document.getElementById('searchHelp');
helpIcon.addEventListener('click', function(e) {
e.stopPropagation();
searchHelp.classList.toggle('visible');
});
document.addEventListener('click', function(e) {
if (!searchHelp.contains(e.target) && e.target !== helpIcon) {
searchHelp.classList.remove('visible');
}
});
});
// Constantes de configuration
const CONFIG = {
DEBOUNCE_DELAY: 300,
MIN_SEARCH_INTERVAL: 500,
ITEMS_PER_PAGE: 10,
CACHE_DURATION: 5 * 60 * 1000, // 5 minutes
MIN_SEARCH_LENGTH: 2,
MAX_PAGES: 100 // GitHub limite à 1000 résultats (100 pages de 10)
};
// Gestionnaire de cache amélioré
const searchCache = {
data: new Map(),
set(key, value) {
// Limiter la taille du cache
if (this.data.size > 100) {
const oldestKey = this.data.keys().next().value;
this.data.delete(oldestKey);
}
this.data.set(key, {
value,
timestamp: Date.now()
});
},
get(key) {
const item = this.data.get(key);
if (!item) return null;
if (Date.now() - item.timestamp > CONFIG.CACHE_DURATION) {
this.data.delete(key);
return null;
}
return item.value;
},
clear() {
this.data.clear();
}
};
// État global de l'application
const state = {
currentPage: 1,
isLoading: false,
lastQuery: '',
lastSearchTime: 0,
searchTimeout: null,
totalPages: 0,
pendingRequest: null
};
// Fonction de recherche optimisée
async function searchProjects(page = 1) {
const searchInput = document.getElementById('searchInput').value.trim();
const projectsContainer = document.getElementById('projects');
// Validations de base
if (!isValidSearch(searchInput)) {
projectsContainer.innerHTML = '';
return;
}
// Éviter les recherches en double
if (shouldSkipSearch(searchInput, page)) return;
// Vérifier le cache
const cacheKey = `${searchInput}-${page}`;
const cachedResults = searchCache.get(cacheKey);
if (cachedResults) {
displayResults(cachedResults, page);
return;
}
// Afficher l'état de chargement
updateLoadingState(page);
try {
// Annuler la requête précédente si elle existe
if (state.pendingRequest) {
state.pendingRequest.abort();
}
// Créer un nouveau contrôleur d'abandon
const controller = new AbortController();
state.pendingRequest = controller;
const response = await fetchWithTimeout(
buildSearchUrl(searchInput, page),
{
signal: controller.signal,
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'Open-Source-Hub'
}
}
);
const data = await response.json();
// Mettre en cache les résultats
searchCache.set(cacheKey, data);
// Afficher les résultats
displayResults(data, page);
} catch (error) {
handleSearchError(error, page);
} finally {
state.isLoading = false;
state.pendingRequest = null;
}
}
// Fonctions utilitaires
function isValidSearch(query) {
return query.length >= CONFIG.MIN_SEARCH_LENGTH;
}
function shouldSkipSearch(query, page) {
return state.isLoading ||
(page === 1 && query === state.lastQuery) ||
page > CONFIG.MAX_PAGES;
}
function buildSearchUrl(query, page) {
return `https://api.github.com/search/repositories?` +
`q=${encodeURIComponent(query)}+is:public` +
`&sort=stars&order=desc&page=${page}&per_page=${CONFIG.ITEMS_PER_PAGE}`;
}
async function fetchWithTimeout(url, options, timeout = 5000) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, {
...options,
signal: controller.signal
});
clearTimeout(timeoutId);
return response;
} catch (error) {
clearTimeout(timeoutId);
throw error;
}
}
// Gestionnaire de recherche amélioré
function handleSearch() {
clearTimeout(state.searchTimeout);
const now = Date.now();
const timeElapsed = now - state.lastSearchTime;
const debounceDelay = Math.max(
CONFIG.DEBOUNCE_DELAY,
CONFIG.MIN_SEARCH_INTERVAL - timeElapsed
);
state.searchTimeout = setTimeout(() => {
state.currentPage = 1;
state.lastSearchTime = Date.now();
searchProjects(state.currentPage);
}, debounceDelay);
}
// Event listeners optimisés
function setupEventListeners() {
const searchInput = document.getElementById('searchInput');
searchInput.addEventListener('input', handleSearch);
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter' && !state.isLoading) {
clearTimeout(state.searchTimeout);
state.currentPage = 1;
state.lastSearchTime = Date.now();
searchProjects(state.currentPage);
}
});
}
// Initialisation
setupEventListeners();
function displayResults(data, page) {
const projectsContainer = document.getElementById('projects');
if (page === 1) {
projectsContainer.innerHTML = '';
}
if (data.items.length === 0) {
projectsContainer.innerHTML = '<div class="no-results">No projects found</div>';
return;
}
// Calculer le nombre total de pages
state.totalPages = Math.ceil(data.total_count / CONFIG.ITEMS_PER_PAGE);
// Afficher les résultats
let resultsHTML = `
<div class="results-info">
Showing ${((page - 1) * CONFIG.ITEMS_PER_PAGE) + 1} - ${Math.min(page * CONFIG.ITEMS_PER_PAGE, data.total_count)}
of ${data.total_count.toLocaleString()} repositories
</div>
<div class="projects-list">
`;
data.items.forEach(project => {
resultsHTML += `
<div class="project-card">
<a href="${project.html_url}" target="_blank">
<h2>${project.name}</h2>
<span class="url">${project.html_url}</span>
</a>
<p>${truncateDescription(project.description)}</p>
<div class="project-stats">
<span>⭐ ${project.stargazers_count.toLocaleString()} stars</span>
<span>👁 ${project.watchers_count.toLocaleString()} watchers</span>
<span>🔄 ${project.forks_count.toLocaleString()} forks</span>
<span>📝 ${project.language || 'Not specified'}</span>
</div>
</div>
`;
});
resultsHTML += '</div>';
// Ajouter la pagination
resultsHTML += createPaginationControls(page, state.totalPages);
projectsContainer.innerHTML = resultsHTML;
// Ajouter les event listeners pour la pagination
document.querySelectorAll('.pagination-button').forEach(button => {
button.addEventListener('click', () => {
const newPage = parseInt(button.dataset.page);
if (newPage !== state.currentPage) {
searchProjects(newPage);
window.scrollTo({ top: 0, behavior: 'smooth' });
}
});
});
}
function createPaginationControls(currentPage, totalPages) {
if (totalPages <= 1) return '';
let paginationHTML = '<div class="pagination">';
// Bouton précédent
paginationHTML += `
<button class="pagination-button"
data-page="${currentPage - 1}"
${currentPage === 1 ? 'disabled' : ''}>
Previous
</button>
`;
let startPage = Math.max(1, currentPage - 2);
let endPage = Math.min(totalPages, startPage + 4);
if (endPage - startPage < 4) {
startPage = Math.max(1, endPage - 4);
}
// Première page
if (startPage > 1) {
paginationHTML += `
<button class="pagination-button" data-page="1">1</button>
${startPage > 2 ? '<span class="pagination-ellipsis">...</span>' : ''}
`;
}
// Pages numérotées
for (let i = startPage; i <= endPage; i++) {
paginationHTML += `
<button class="pagination-button ${i === currentPage ? 'active' : ''}"
data-page="${i}">
${i}
</button>
`;
}
// Dernière page
if (endPage < totalPages) {
paginationHTML += `
${endPage < totalPages - 1 ? '<span class="pagination-ellipsis">...</span>' : ''}
<button class="pagination-button" data-page="${totalPages}">${totalPages}</button>
`;
}
// Bouton suivant
paginationHTML += `
<button class="pagination-button"
data-page="${currentPage + 1}"
${currentPage === totalPages ? 'disabled' : ''}>
Next
</button>
`;
paginationHTML += '</div>';
return paginationHTML;
}
function truncateDescription(description, maxLength = 150) {
if (!description) return 'No description available';
if (description.length <= maxLength) return description;
return description.substring(0, maxLength).trim() + '...';
}
function updateLoadingState(page) {
const projectsContainer = document.getElementById('projects');
if (page === 1) {
projectsContainer.innerHTML = `
<div class="loading">
${searchCache.data.size > 0 ?
`<small>(${searchCache.data.size} results cached)</small>` :
''}
Searching...
</div>
`;
}
}
document.getElementById('toggleDarkMode').addEventListener('click', function() {
document.body.classList.toggle('dark-mode');
const isDarkMode = document.body.classList.contains('dark-mode');
this.textContent = isDarkMode ? '🌞' : '🌙'; // Change l'icône en fonction du mode
});