Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
701 lines (575 loc) · 24.9 KB
/
Copy pathapp.js
File metadata and controls
701 lines (575 loc) · 24.9 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
// app.js - Main application logic (Fixed for Electron)
console.log('🔍 Debugging module availability:');
console.log('- require available:', typeof require !== 'undefined');
console.log('- electron available:', typeof require !== 'undefined' && require.resolve ? 'yes' : 'no');
let GitHubAPI, RepoStorage, ipcRenderer;
try {
if (typeof require !== 'undefined') {
console.log('📁 Attempting to load modules...');
ipcRenderer = require('electron').ipcRenderer;
console.log('✅ ipcRenderer loaded');
GitHubAPI = require('./github-api');
console.log('✅ GitHubAPI loaded');
RepoStorage = require('./repo-storage');
console.log('✅ RepoStorage loaded');
}
} catch (error) {
console.error('❌ Module loading failed:', error.message);
console.error('📍 Error details:', error);
}
class RepoSpaceApp {
constructor() {
this.githubAPI = GitHubAPI ? new GitHubAPI() : null;
this.repoStorage = RepoStorage ? new RepoStorage() : null;
this.currentRepo = null;
this.searchTimeout = null;
this.elements = {};
this.init();
}
init() {
console.log('🚀 RepoSpace starting...');
this.cacheElements();
this.setupEventListeners();
this.initializeTheme();
if (this.repoStorage) {
this.repoStorage.ensureBaseDirectory();
}
console.log('✅ RepoSpace initialized');
}
cacheElements() {
this.elements = {
themeToggle: document.getElementById('themeToggle'),
themeIcon: document.getElementById('themeIcon'),
browseSavedBtn: document.getElementById('browseSavedBtn'),
loginBtn: document.getElementById('loginBtn'),
userInfo: document.getElementById('userInfo'),
searchInput: document.getElementById('searchInput'),
searchBtn: document.getElementById('searchBtn'),
repoGrid: document.getElementById('repoGrid'),
saveModal: document.getElementById('saveModal'),
repoName: document.getElementById('repoName'),
customCategory: document.getElementById('customCategory')
};
const missingElements = Object.entries(this.elements)
.filter(([key, element]) => !element)
.map(([key]) => key);
if (missingElements.length > 0) {
console.warn('Missing DOM elements:', missingElements);
}
}
setupEventListeners() {
if (this.elements.themeToggle) {
this.elements.themeToggle.addEventListener('click', () => this.toggleTheme());
}
if (this.elements.browseSavedBtn) {
this.elements.browseSavedBtn.addEventListener('click', () => this.showSavedRepos());
}
console.log('🔍 Looking for browseSavedBtn element...');
if (this.elements.browseSavedBtn) {
console.log('✅ Browse Saved button found and connected');
this.elements.browseSavedBtn.addEventListener('click', (e) => {
console.log('🖱️ Browse Saved button clicked!');
console.log('🔍 Event details:', e);
console.log('📦 RepoStorage available:', !!this.repoStorage);
this.showSavedRepos();
});
} else {
console.log('❌ Browse Saved button NOT found in DOM!');
console.log('📋 Available elements:', Object.keys(this.elements));
}
if (this.elements.loginBtn) {
this.elements.loginBtn.addEventListener('click', () => this.handleLogin());
}
if (this.elements.searchInput) {
this.elements.searchInput.addEventListener('input', (e) => this.handleSearchInput(e));
this.elements.searchInput.addEventListener('keypress', (e) => this.handleSearchKeypress(e));
}
if (this.elements.saveModal) {
this.elements.saveModal.addEventListener('click', (e) => this.handleModalClick(e));
}
if (this.elements.searchBtn) {
this.elements.searchBtn.addEventListener('click', () => {
const query = this.elements.searchInput.value.trim();
if (query) {
this.searchRepositories(query);
}
});
}
document.querySelectorAll('.category-btn').forEach(btn => {
btn.addEventListener('click', () => this.selectCategory(btn));
});
document.addEventListener('keydown', (e) => this.handleGlobalKeyboard(e));
}
initializeTheme() {
const savedTheme = localStorage.getItem('theme') || 'light';
const body = document.body;
if (savedTheme === 'dark') {
body.classList.add('dark-theme');
if (this.elements.themeIcon) {
this.elements.themeIcon.textContent = '☀️';
}
} else {
body.classList.remove('dark-theme');
if (this.elements.themeIcon) {
this.elements.themeIcon.textContent = '🌙';
}
}
}
toggleTheme() {
const body = document.body;
const isDark = body.classList.toggle('dark-theme');
if (this.elements.themeIcon) {
this.elements.themeIcon.textContent = isDark ? '☀️' : '🌙';
}
localStorage.setItem('theme', isDark ? 'dark' : 'light');
console.log(`🎨 Switched to ${isDark ? 'dark' : 'light'} theme`);
}
async handleLogin() {
if (!ipcRenderer) {
this.showError('OAuth not available - not running in Electron');
return;
}
try {
this.elements.loginBtn.textContent = 'Signing in...';
this.elements.loginBtn.disabled = true;
const accessToken = await ipcRenderer.invoke('start-oauth');
if (this.githubAPI) {
this.githubAPI = new GitHubAPI(accessToken);
}
this.elements.loginBtn.style.display = 'none';
this.elements.userInfo.style.display = 'block';
this.elements.userInfo.textContent = '✅ Signed in';
console.log('🔑 Successfully authenticated with GitHub');
} catch (error) {
console.error('❌ Login failed:', error);
this.showError('Login failed: ' + error.message);
this.elements.loginBtn.textContent = 'Sign in with GitHub';
this.elements.loginBtn.disabled = false;
}
}
showSavedRepos() {
console.log('📁 showSavedRepos() called');
if (!this.repoStorage) {
console.log('❌ Storage system not available');
this.showError('Storage system not available');
return;
}
const categories = this.repoStorage.getCategories();
console.log('📂 Found categories:', categories);
if (categories.length === 0) {
this.elements.repoGrid.innerHTML = `
<div style="text-align: center; padding: 40px; color: var(--text-color);">
<div style="font-size: 3rem; margin-bottom: 20px;">📁</div>
<h3>No saved repositories yet</h3>
<p>Search and save some repositories to see them here!</p>
</div>
`;
return;
}
let html = '';
categories.forEach(category => {
const repos = this.repoStorage.getReposByCategory(category);
html += `
<div style="margin-bottom: 30px;">
<h3 style="color: var(--primary-color); margin-bottom: 15px;">📂 ${category} (${repos.length})</h3>
<div class="repo-grid">
${repos.map(repo => this.createSavedRepoCard(repo)).join('')}
</div>
</div>
`;
});
this.elements.repoGrid.innerHTML = html;
console.log(`📁 Showing ${categories.length} categories with saved repos`);
}
handleSearchInput(e) {
clearTimeout(this.searchTimeout);
const query = e.target.value.trim();
if (query.length > 2) {
this.searchTimeout = setTimeout(() => {
this.searchRepositories(query);
}, 500); // Wait 500ms after user stops typing
} else if (query.length === 0) {
this.clearResults();
}
}
handleSearchKeypress(e) {
if (e.key === 'Enter') {
clearTimeout(this.searchTimeout);
const query = e.target.value.trim();
if (query) {
this.searchRepositories(query);
}
}
}
async searchRepositories(query) {
if (!this.githubAPI) {
this.showError('GitHub API not available');
return;
}
try {
console.log('🔍 Searching for:', query);
this.showLoading();
const results = await this.githubAPI.searchRepositories(query);
this.displaySearchResults(results.items || []);
} catch (error) {
console.error('❌ Search failed:', error);
this.showError(`Search failed: ${error.message}`);
}
}
displaySearchResults(repositories) {
if (!this.elements.repoGrid) return;
if (repositories.length === 0) {
this.elements.repoGrid.innerHTML = `
<div style="text-align: center; padding: 40px; color: var(--text-color);">
<h3>No repositories found</h3>
<p>Try different keywords or check your spelling</p>
</div>
`;
return;
}
this.elements.repoGrid.innerHTML = repositories.map(repo => this.createRepoCard(repo)).join('');
console.log(`📦 Found ${repositories.length} repositories`);
}
createRepoCard(repo) {
const description = (repo.description || 'No description available').replace(/'/g, ''').replace(/"/g, '"');
return `
<div class="repo-card" data-repo="${repo.full_name}">
<div class="repo-header">
<a href="#" class="repo-name" onclick="app.viewRepo('${repo.full_name}')">${repo.full_name}</a>
<div class="repo-stars">⭐ ${this.formatNumber(repo.stargazers_count)}</div>
</div>
<p class="repo-description">
${repo.description || 'No description available'}
</p>
<div class="repo-meta">
<div class="repo-language">
${repo.language ? `
<div class="language-dot" style="background-color: ${this.getLanguageColor(repo.language)};"></div>
${repo.language}
` : 'No language specified'}
</div>
<span>Updated ${this.formatDate(repo.updated_at)}</span>
</div>
<button class="btn btn-secondary save-btn" onclick="app.openSaveModal('${repo.full_name}', '${repo.clone_url}', '${description}', '${repo.language || ''}', ${repo.stargazers_count})">
💾 Save
</button>
<button class="btn btn-secondary save-btn" onclick="app.openRepoFolder('${repo.full_name}')" style="right: 70px;">
📁 Browse
</button>
</div>
`;
}
createSavedRepoCard(repo) {
return `
<div class="repo-card">
<div class="repo-header">
<div class="repo-name">${repo.fullName}</div>
<div class="repo-stars">⭐ ${this.formatNumber(repo.stars)}</div>
</div>
<p class="repo-description">${repo.description}</p>
<div class="repo-meta">
<div class="repo-language">
${repo.language ? `
<div class="language-dot" style="background-color: ${this.getLanguageColor(repo.language)};"></div>
${repo.language}
` : 'No language'}
</div>
<span>Saved ${this.formatDate(repo.savedAt)}</span>
</div>
<button class="btn btn-primary save-btn" onclick="app.openRepoFolder('${repo.fullName}')">
📁 Open Folder
</button>
</div>
`;
}
async viewRepo(fullName) {
try {
if (this.repoStorage) {
const exists = await this.repoStorage.checkRepoExists(fullName);
if (exists) {
await this.showRepoViewer(fullName);
return;
}
}
await this.showRepoPreview(fullName);
} catch (error) {
console.error('Error viewing repo:', error);
this.showError('Error viewing repository: ' + error.message);
}
}
async showRepoPreview(fullName) {
try {
const [owner, repo] = fullName.split('/');
this.elements.repoGrid.innerHTML = '<div style="text-align: center; padding: 40px;">📖 Loading preview...</div>';
const repoData = await this.githubAPI.getRepository(owner, repo);
const readmeContent = await this.githubAPI.getFileContent(owner, repo, 'README.md').catch(() => 'No README found');
const description = (repoData.description || '').replace(/'/g, ''').replace(/"/g, '"');
const html = `
<div style="padding: 20px;">
<div class="preview-banner" style="background: var(--hover-color); padding: 15px; border-radius: 8px; margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center;">
<div>
<strong>📖 Preview Mode</strong> - Save this repository for full offline access and file browsing
</div>
<button class="btn btn-primary" onclick="app.openSaveModal('${fullName}', '${repoData.clone_url}', '${description}', '${repoData.language || ''}', ${repoData.stargazers_count})">
💾 Save Repository
</button>
</div>
<h2 style="color: var(--primary-color);">${fullName}</h2>
<p style="opacity: 0.8; margin-bottom: 20px;">${repoData.description || 'No description'}</p>
<div style="display: flex; gap: 20px; margin-bottom: 20px;">
<span>⭐ ${this.formatNumber(repoData.stargazers_count)} stars</span>
<span>🍴 ${this.formatNumber(repoData.forks_count)} forks</span>
<span>📝 ${repoData.language || 'No language'}</span>
</div>
<div style="background: var(--card-bg); padding: 20px; border-radius: 8px;">
<h3>README.md</h3>
<pre style="white-space: pre-wrap; font-family: monospace; background: var(--bg-color); padding: 15px; border-radius: 6px; overflow-x: auto;">${readmeContent}</pre>
</div>
<div style="text-align: center; margin-top: 20px;">
<button class="btn btn-secondary" onclick="history.back()">← Back</button>
</div>
</div>
`;
this.elements.repoGrid.innerHTML = html;
} catch (error) {
console.error('Error loading preview:', error);
this.showError('Error loading preview: ' + error.message);
}
}
showSavedRepos() {
console.log('📁 showSavedRepos() called');
if (!this.repoStorage) {
this.showError('Storage system not available');
return;
}
const categories = this.repoStorage.getCategories();
console.log('📂 Found categories:', categories);
if (categories.length === 0) {
this.elements.repoGrid.innerHTML = `
<div style="text-align: center; padding: 40px; color: var(--text-color);">
<div style="font-size: 3rem; margin-bottom: 20px;">📁</div>
<h3>No saved repositories yet</h3>
<p>Search and save some repositories to see them here!</p>
</div>
`;
return;
}
let html = '';
categories.forEach(category => {
const repos = this.repoStorage.getReposByCategory(category);
html += `
<div style="margin-bottom: 30px;">
<h3 style="color: var(--primary-color); margin-bottom: 15px;">📂 ${category} (${repos.length})</h3>
<div class="repo-grid">
${repos.map(repo => this.createSavedRepoCard(repo)).join('')}
</div>
</div>
`;
});
this.elements.repoGrid.innerHTML = html;
console.log(`📁 Showing ${categories.length} categories with saved repos`);
}
openSaveModal(fullName, cloneUrl, description = '', language = '', stars = 0) {
this.currentRepo = {
fullName: fullName,
cloneUrl: cloneUrl,
description: description,
language: language,
stars: stars
};
if (this.elements.repoName) {
this.elements.repoName.textContent = fullName;
}
if (this.elements.saveModal) {
this.elements.saveModal.style.display = 'block';
}
this.loadExistingCategories();
console.log('💾 Opening save modal for:', fullName);
}
loadExistingCategories() {
if (!this.repoStorage) return;
const categories = this.repoStorage.getCategories();
const categoryGrid = document.querySelector('.category-grid');
if (!categoryGrid) return;
const existingCustom = categoryGrid.querySelectorAll('.category-btn.custom');
existingCustom.forEach(btn => btn.remove());
categories.forEach(category => {
const btn = document.createElement('div');
btn.className = 'category-btn custom';
btn.dataset.category = category;
btn.textContent = `📁 ${category}`;
btn.addEventListener('click', () => this.selectCategory(btn));
categoryGrid.appendChild(btn);
});
}
selectCategory(btn) {
document.querySelectorAll('.category-btn').forEach(b => b.classList.remove('selected'));
btn.classList.add('selected');
if (this.elements.customCategory) {
this.elements.customCategory.value = btn.dataset.category;
}
}
async saveRepository() {
const category = this.elements.customCategory ? this.elements.customCategory.value.trim() : '';
if (!category) {
this.showError('Please select or enter a category');
return;
}
if (!this.currentRepo) {
this.showError('No repository selected');
return;
}
if (!this.repoStorage) {
this.showError('Storage system not available');
return;
}
try {
console.log(`💾 Saving ${this.currentRepo.fullName} to category: ${category}`);
const saved = this.repoStorage.saveRepoMetadata(this.currentRepo, category);
if (!saved) {
this.showError(`Repository already exists in "${category}" category`);
return;
}
await this.downloadRepositoryFiles(this.currentRepo);
this.showSuccess(`✅ Repository saved to "${category}" category!`);
this.closeSaveModal();
} catch (error) {
console.error('❌ Failed to save repository:', error);
this.showError('Failed to save repository: ' + error.message);
}
}
async downloadRepositoryFiles(repoData) {
if (!this.githubAPI || !this.repoStorage) {
throw new Error('Required services not available');
}
try {
const [owner, repo] = repoData.fullName.split('/');
console.log(`📥 Downloading files from ${repoData.fullName}...`);
const files = await this.githubAPI.getAllRepositoryFiles(owner, repo);
const savedPath = await this.repoStorage.saveRepositoryFiles(repoData.fullName, files, category);
console.log(`✅ Successfully downloaded ${files.length} files to: ${savedPath}`);
} catch (error) {
console.error('❌ Failed to download repository files:', error);
throw error;
}
}
async openRepoFolder(fullName) {
try {
const result = await ipcRenderer.invoke('open-repo-folder', fullName);
if (result.success) {
console.log('📁 Opened repo folder');
} else {
this.showError('Failed to open folder: ' + result.error);
}
} catch (error) {
this.showError('Failed to open folder: ' + error.message);
}
}
closeSaveModal() {
if (this.elements.saveModal) {
this.elements.saveModal.style.display = 'none';
}
document.querySelectorAll('.category-btn').forEach(btn => btn.classList.remove('selected'));
if (this.elements.customCategory) {
this.elements.customCategory.value = '';
}
this.currentRepo = null;
}
handleModalClick(e) {
if (e.target.id === 'saveModal') {
this.closeSaveModal();
}
}
handleGlobalKeyboard(e) {
if (e.key === 'Escape' && this.elements.saveModal && this.elements.saveModal.style.display === 'block') {
this.closeSaveModal();
}
if ((e.ctrlKey || e.metaKey) && e.key === 'k') {
e.preventDefault();
if (this.elements.searchInput) {
this.elements.searchInput.focus();
}
}
}
showLoading() {
if (this.elements.repoGrid) {
this.elements.repoGrid.innerHTML = `
<div style="text-align: center; padding: 40px;">
<div style="font-size: 2rem; margin-bottom: 10px;">🔍</div>
<div>Searching repositories...</div>
</div>
`;
}
}
clearResults() {
if (this.elements.repoGrid) {
this.elements.repoGrid.innerHTML = `
<div style="text-align: center; padding: 40px; color: var(--text-color); opacity: 0.6;">
<div style="font-size: 3rem; margin-bottom: 20px;">🔍</div>
<h3>Start by searching for repositories</h3>
<p>Try searching for topics like "react", "python", "machine-learning", or "blockchain"</p>
</div>
`;
}
}
showError(message) {
alert('❌ ' + message);
console.error(message);
}
showSuccess(message) {
alert(message);
console.log(message);
}
formatNumber(num) {
if (num >= 1000000) return (num / 1000000).toFixed(1) + 'M';
if (num >= 1000) return (num / 1000).toFixed(1) + 'k';
return num.toString();
}
formatDate(dateString) {
const date = new Date(dateString);
const now = new Date();
const diffTime = Math.abs(now - date);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays === 1) return '1 day ago';
if (diffDays < 7) return `${diffDays} days ago`;
if (diffDays < 30) return `${Math.ceil(diffDays / 7)} weeks ago`;
return `${Math.ceil(diffDays / 30)} months ago`;
}
getLanguageColor(language) {
const colors = {
'JavaScript': '#f1e05a',
'Python': '#3572A5',
'Java': '#b07219',
'TypeScript': '#2b7489',
'C++': '#f34b7d',
'C': '#555555',
'Go': '#00ADD8',
'Rust': '#dea584',
'HTML': '#e34c26',
'CSS': '#563d7c',
'PHP': '#4F5D95',
'Ruby': '#701516',
'Swift': '#ffac45',
'Kotlin': '#F18E33'
};
return colors[language] || '#666666';
}
}
// Global functions for HTML onclick events
function saveRepository() {
if (window.app) {
window.app.saveRepository();
}
}
function closeSaveModal() {
if (window.app) {
window.app.closeSaveModal();
}
}
document.addEventListener('DOMContentLoaded', () => {
window.app = new RepoSpaceApp();
});
// Export for Node.js if needed
if (typeof module !== 'undefined' && module.exports) {
module.exports = RepoSpaceApp;
}