File size: 27,946 Bytes
d36f611
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
// Global variables
let clientsData = [];
let serversData = [];
let allData = [];
let filteredData = [];
let currentView = 'grid';
let currentPage = 1;
let itemsPerPage = 20;
let totalPages = 1;

// DOM Elements
const navItems = document.querySelectorAll('.nav-item');
const pages = document.querySelectorAll('.page');
const searchInput = document.getElementById('search-input');
const typeFilter = document.getElementById('type-filter');
const categoryFilter = document.getElementById('category-filter');
const languageFilter = document.getElementById('language-filter');
const searchResults = document.getElementById('search-results');
const resultsCount = document.getElementById('results-count');
const viewButtons = document.querySelectorAll('.view-btn');
const modal = document.getElementById('project-modal');
const modalTitle = document.getElementById('modal-title');
const modalBody = document.getElementById('modal-body');
const modalClose = document.querySelector('.modal-close');
const paginationContainer = document.getElementById('pagination-container');
const prevBtn = document.getElementById('prev-btn');
const nextBtn = document.getElementById('next-btn');
const pageNumbers = document.getElementById('page-numbers');
const paginationInfo = document.getElementById('pagination-info');

// Initialize app
document.addEventListener('DOMContentLoaded', async () => {
    await loadData();
    setupEventListeners();
    updateDashboard();
    initializeSearch();
});

// Load data from JSON files with optimization
async function loadData() {
    try {
        showLoading();
        
        const [clientsResponse, serversResponse] = await Promise.all([
            fetch('mcpso_clients_cleaned.json'),
            fetch('mcpso_servers_cleaned.json')
        ]);

        if (!clientsResponse.ok || !serversResponse.ok) {
            throw new Error('Failed to fetch data');
        }

        clientsData = await clientsResponse.json();
        serversData = await serversResponse.json();
        
        // Combine all data and deduplicate by GitHub repository
        const rawData = [
            ...clientsData.map(item => ({ ...item, type: 'client' })),
            ...serversData.map(item => ({ ...item, type: 'server' }))
        ];
        
        // Process and deduplicate data
        allData = processAndDeduplicateData(rawData);
        filteredData = [...allData];
        
        console.log(`Loaded ${clientsData.length} clients and ${serversData.length} servers (${allData.length} total projects)`);
        
        hideLoading();
    } catch (error) {
        console.error('Error loading data:', error);
        showError('Failed to load data. Please refresh the page.');
        hideLoading();
    }
}

// Setup event listeners
function setupEventListeners() {
    // Navigation
    navItems.forEach(item => {
        item.addEventListener('click', () => switchPage(item.dataset.page));
    });

    // Search and filters
    searchInput.addEventListener('input', debounce(performSearch, 300));
    typeFilter.addEventListener('change', performSearch);
    categoryFilter.addEventListener('change', performSearch);
    languageFilter.addEventListener('change', performSearch);

    // View toggle
    viewButtons.forEach(btn => {
        btn.addEventListener('click', () => toggleView(btn.dataset.view));
    });

    // Modal
    modalClose.addEventListener('click', closeModal);
    modal.addEventListener('click', (e) => {
        if (e.target === modal) closeModal();
    });

    document.addEventListener('keydown', (e) => {
        if (e.key === 'Escape') closeModal();
    });

    // Pagination
    prevBtn.addEventListener('click', () => {
        if (currentPage > 1) {
            currentPage--;
            renderSearchResults();
            renderPagination();
        }
    });

    nextBtn.addEventListener('click', () => {
        if (currentPage < totalPages) {
            currentPage++;
            renderSearchResults();
            renderPagination();
        }
    });
}

// Switch between pages
function switchPage(pageId) {
    // Update navigation
    navItems.forEach(item => {
        item.classList.toggle('active', item.dataset.page === pageId);
    });

    // Update pages
    pages.forEach(page => {
        page.classList.toggle('active', page.id === pageId);
    });

    // Initialize page-specific content
    if (pageId === 'search') {
        // Reset pagination when switching to search page
        currentPage = 1;
        performSearch();
        populateFilters();
    }
}

// Process and deduplicate data based on GitHub repository
function processAndDeduplicateData(rawData) {
    const githubMap = new Map();
    const processedData = [];
    
    for (const item of rawData) {
        // Only process items with valid GitHub data
        if (item.github && item.github.full_name && item.github.stargazers_count >= 0) {
            const githubKey = item.github.full_name.toLowerCase();
            
            // Extract real project name from GitHub URL
            const realName = extractProjectNameFromGithub(item.github.full_name);
            
            // Check if we already have this repository
            if (githubMap.has(githubKey)) {
                const existing = githubMap.get(githubKey);
                // Keep the one with more complete information or higher stars
                if (item.github.stargazers_count > existing.github.stargazers_count ||
                    (item.github.stargazers_count === existing.github.stargazers_count && 
                     item.description && item.description.length > (existing.description || '').length)) {
                    githubMap.set(githubKey, { ...item, displayName: realName });
                }
            } else {
                githubMap.set(githubKey, { ...item, displayName: realName });
            }
        } else {
            // For items without GitHub data, keep them as is but filter out obviously invalid names
            if (isValidProjectName(item.name)) {
                processedData.push({ ...item, displayName: item.name });
            }
        }
    }
    
    // Add deduplicated GitHub projects
    processedData.push(...Array.from(githubMap.values()));
    
    return processedData;
}

// Extract project name from GitHub full_name
function extractProjectNameFromGithub(fullName) {
    if (!fullName) return 'Unknown';
    const parts = fullName.split('/');
    return parts[parts.length - 1] || 'Unknown';
}

// Check if project name is valid (filter out test names)
function isValidProjectName(name) {
    if (!name || typeof name !== 'string') return false;
    
    const invalidPatterns = [
        /^[0-9]+$/,        // Pure numbers: 1, 11, 12, 11111
        /^test$/i,         // Just "test"
        /^[a-z]$/i,        // Single letters
        /^.{1,2}$/,        // Too short (1-2 characters)
    ];
    
    return !invalidPatterns.some(pattern => pattern.test(name.trim()));
}

// Update dashboard statistics and charts
function updateDashboard() {
    updateStatistics();
    renderCharts();
    renderTopProjects();
}

// Update statistics
function updateStatistics() {
    const clientsCount = clientsData.length;
    const serversCount = serversData.length;
    
    const categories = new Set([
        ...clientsData.map(item => item.category).filter(Boolean),
        ...serversData.map(item => item.category).filter(Boolean)
    ]);
    
    const totalStars = allData
        .filter(item => item.github && item.github.stargazers_count)
        .reduce((sum, item) => sum + item.github.stargazers_count, 0);

    animateNumber('clients-count', clientsCount);
    animateNumber('servers-count', serversCount);
    animateNumber('categories-count', categories.size);
    animateNumber('stars-count', formatNumber(totalStars));
}

// Animate numbers
function animateNumber(elementId, target) {
    const element = document.getElementById(elementId);
    const start = 0;
    const duration = 1000;
    const startTime = performance.now();
    
    function update(currentTime) {
        const elapsed = currentTime - startTime;
        const progress = Math.min(elapsed / duration, 1);
        
        const current = Math.floor(progress * (
            typeof target === 'string' 
                ? parseInt(target.replace(/[^\d]/g, '')) 
                : target
        ));
        
        element.textContent = typeof target === 'string' 
            ? formatNumber(current) 
            : current;
        
        if (progress < 1) {
            requestAnimationFrame(update);
        } else {
            element.textContent = target;
        }
    }
    
    requestAnimationFrame(update);
}

// Format numbers
function formatNumber(num) {
    if (num >= 1000000) {
        return (num / 1000000).toFixed(1) + 'M';
    } else if (num >= 1000) {
        return (num / 1000).toFixed(1) + 'K';
    }
    return num.toString();
}

// Render charts
function renderCharts() {
    renderCategoryChart();
    renderLanguageChart();
}

// Category distribution chart
function renderCategoryChart() {
    const ctx = document.getElementById('categoryChart').getContext('2d');
    
    const categoryCount = {};
    allData.forEach(item => {
        if (item.category) {
            const category = formatCategoryName(item.category);
            categoryCount[category] = (categoryCount[category] || 0) + 1;
        }
    });

    const sortedCategories = Object.entries(categoryCount)
        .sort((a, b) => b[1] - a[1])
        .slice(0, 8);

    new Chart(ctx, {
        type: 'doughnut',
        data: {
            labels: sortedCategories.map(([category]) => category),
            datasets: [{
                data: sortedCategories.map(([, count]) => count),
                backgroundColor: [
                    '#3b82f6', '#10b981', '#8b5cf6', '#f59e0b', 
                    '#ef4444', '#06b6d4', '#84cc16', '#f97316'
                ],
                borderWidth: 0,
                borderRadius: 4
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    position: 'bottom',
                    labels: {
                        padding: 20,
                        usePointStyle: true,
                        font: { family: 'Inter' }
                    }
                }
            }
        }
    });
}

// Language distribution chart
function renderLanguageChart() {
    const ctx = document.getElementById('languageChart').getContext('2d');
    
    const languageCount = {};
    allData.forEach(item => {
        if (item.github && item.github.language) {
            const lang = item.github.language;
            languageCount[lang] = (languageCount[lang] || 0) + 1;
        }
    });

    const sortedLanguages = Object.entries(languageCount)
        .sort((a, b) => b[1] - a[1])
        .slice(0, 10);

    new Chart(ctx, {
        type: 'bar',
        data: {
            labels: sortedLanguages.map(([lang]) => lang),
            datasets: [{
                label: 'Projects',
                data: sortedLanguages.map(([, count]) => count),
                backgroundColor: '#3b82f6',
                borderRadius: 6,
                borderSkipped: false
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: { display: false }
            },
            scales: {
                y: {
                    beginAtZero: true,
                    grid: { color: '#f3f4f6' },
                    ticks: { font: { family: 'Inter' } }
                },
                x: {
                    grid: { display: false },
                    ticks: { font: { family: 'Inter' } }
                }
            }
        }
    });
}

// Render top projects
function renderTopProjects() {
    const container = document.getElementById('top-projects');
    
    const topProjects = allData
        .filter(item => item.github && item.github.stargazers_count > 0)
        .sort((a, b) => b.github.stargazers_count - a.github.stargazers_count)
        .slice(0, 10);

    if (topProjects.length === 0) {
        container.innerHTML = `
            <div class="empty-state">
                <i class="fas fa-star"></i>
                <p>No projects with GitHub data found</p>
            </div>
        `;
        return;
    }

    container.innerHTML = topProjects.map((project, index) => `
        <div class="project-item" onclick="showProjectModal('${project.id}')">
            <div class="project-rank">#${index + 1}</div>
            <div class="project-github">
                <a href="${project.url}" target="_blank" onclick="event.stopPropagation()">
                    <i class="fab fa-github"></i>
                    ${project.github.full_name}
                </a>
            </div>
            <div class="project-stars">
                <i class="fas fa-star"></i>
                ${formatNumber(project.github.stargazers_count)}
            </div>
        </div>
    `).join('');
}

// Initialize search page
function initializeSearch() {
    populateFilters();
    performSearch();
}

// Populate filter dropdowns
function populateFilters() {
    // Categories
    const categories = [...new Set(allData.map(item => item.category).filter(Boolean))].sort();
    populateSelect(categoryFilter, categories, formatCategoryName);

    // Languages
    const languages = [...new Set(allData
        .filter(item => item.github && item.github.language)
        .map(item => item.github.language)
    )].sort();
    populateSelect(languageFilter, languages);
}

// Populate select element
function populateSelect(selectElement, options, formatter = null) {
    // Clear existing options (except first one)
    while (selectElement.children.length > 1) {
        selectElement.removeChild(selectElement.lastChild);
    }

    options.forEach(option => {
        const optionEl = document.createElement('option');
        optionEl.value = option;
        optionEl.textContent = formatter ? formatter(option) : option;
        selectElement.appendChild(optionEl);
    });
}

// Perform search and filtering
function performSearch() {
    const query = searchInput.value.toLowerCase().trim();
    const type = typeFilter.value;
    const category = categoryFilter.value;
    const language = languageFilter.value;

    filteredData = allData.filter(item => {
        // Text search (use displayName for better search experience)
        const searchName = item.displayName || item.name;
        const matchesQuery = !query || 
            searchName.toLowerCase().includes(query) ||
            (item.description || '').toLowerCase().includes(query) ||
            item.author_name.toLowerCase().includes(query) ||
            (item.tags || '').toLowerCase().includes(query);

        // Type filter
        const matchesType = !type || item.type === type;

        // Category filter
        const matchesCategory = !category || item.category === category;

        // Language filter
        const matchesLanguage = !language || 
            (item.github && item.github.language === language);

        return matchesQuery && matchesType && matchesCategory && matchesLanguage;
    });

    // Reset to first page when search changes
    currentPage = 1;
    totalPages = Math.ceil(filteredData.length / itemsPerPage);
    
    updateResultsCount();
    renderSearchResults();
    renderPagination();
}

// Update results count
function updateResultsCount() {
    const count = filteredData.length;
    resultsCount.textContent = `${count} result${count !== 1 ? 's' : ''}`;
}

// Render search results
function renderSearchResults() {
    const container = searchResults;
    
    if (filteredData.length === 0) {
        container.innerHTML = `
            <div class="empty-state">
                <i class="fas fa-search"></i>
                <h3>No results found</h3>
                <p>Try adjusting your search criteria</p>
            </div>
        `;
        paginationContainer.style.display = 'none';
        return;
    }

    // Calculate pagination
    const startIndex = (currentPage - 1) * itemsPerPage;
    const endIndex = startIndex + itemsPerPage;
    const currentItems = filteredData.slice(startIndex, endIndex);

    container.className = `results-grid ${currentView === 'list' ? 'list-view' : ''}`;
    
    container.innerHTML = currentItems.map(item => createProjectCard(item)).join('');
    paginationContainer.style.display = 'block';
}

// Create project card HTML
function createProjectCard(item) {
    const tags = item.tags ? item.tags.split(',').map(tag => tag.trim()).slice(0, 4) : [];
    const github = item.github;

    return `
        <div class="project-card" onclick="showProjectModal('${item.id}')">
            <div class="project-header">
                <div class="project-info">
                    <div class="project-title">${escapeHtml(item.displayName || item.name)}</div>
                    <div class="project-author">by ${escapeHtml(item.author_name)}</div>
                </div>
                <span class="project-type ${item.type}">${item.type}</span>
            </div>
            
            <div class="project-meta">
                ${github ? `
                    <div class="project-stats">
                        <span class="stat-item">
                            <i class="fas fa-star"></i>
                            ${formatNumber(github.stargazers_count || 0)}
                        </span>
                        <span class="stat-item">
                            <i class="fas fa-code-branch"></i>
                            ${formatNumber(github.forks_count || 0)}
                        </span>
                        ${github.language ? `
                            <span class="stat-item">
                                <i class="fas fa-code"></i>
                                ${escapeHtml(github.language)}
                            </span>
                        ` : ''}
                    </div>
                ` : ''}
                
                ${item.category ? `
                    <div class="category-badge">${formatCategoryName(item.category)}</div>
                ` : ''}
            </div>
            
            ${tags.length > 0 ? `
                <div class="project-tags">
                    ${tags.map(tag => `<span class="tag">${escapeHtml(tag)}</span>`).join('')}
                    ${tags.length > 4 ? `<span class="tag more">+${item.tags.split(',').length - 4}</span>` : ''}
                </div>
            ` : ''}
        </div>
    `;
}

// Toggle view mode
function toggleView(view) {
    currentView = view;
    viewButtons.forEach(btn => {
        btn.classList.toggle('active', btn.dataset.view === view);
    });
    renderSearchResults();
}

// Render pagination
function renderPagination() {
    if (totalPages <= 1) {
        paginationContainer.style.display = 'none';
        return;
    }

    paginationContainer.style.display = 'block';

    // Update prev/next buttons
    prevBtn.disabled = currentPage === 1;
    nextBtn.disabled = currentPage === totalPages;

    // Generate page numbers
    const maxVisiblePages = 7;
    let startPage, endPage;

    if (totalPages <= maxVisiblePages) {
        startPage = 1;
        endPage = totalPages;
    } else {
        const halfVisible = Math.floor(maxVisiblePages / 2);
        if (currentPage <= halfVisible) {
            startPage = 1;
            endPage = maxVisiblePages;
        } else if (currentPage + halfVisible >= totalPages) {
            startPage = totalPages - maxVisiblePages + 1;
            endPage = totalPages;
        } else {
            startPage = currentPage - halfVisible;
            endPage = currentPage + halfVisible;
        }
    }

    let pagesHTML = '';

    // First page and ellipsis
    if (startPage > 1) {
        pagesHTML += `<button class="page-number" onclick="goToPage(1)">1</button>`;
        if (startPage > 2) {
            pagesHTML += `<span class="page-number ellipsis">...</span>`;
        }
    }

    // Page numbers
    for (let i = startPage; i <= endPage; i++) {
        pagesHTML += `<button class="page-number ${i === currentPage ? 'active' : ''}" onclick="goToPage(${i})">${i}</button>`;
    }

    // Last page and ellipsis
    if (endPage < totalPages) {
        if (endPage < totalPages - 1) {
            pagesHTML += `<span class="page-number ellipsis">...</span>`;
        }
        pagesHTML += `<button class="page-number" onclick="goToPage(${totalPages})">${totalPages}</button>`;
    }

    pageNumbers.innerHTML = pagesHTML;

    // Update pagination info
    const startItem = (currentPage - 1) * itemsPerPage + 1;
    const endItem = Math.min(currentPage * itemsPerPage, filteredData.length);
    paginationInfo.textContent = `Showing ${startItem}-${endItem} of ${filteredData.length}`;
}

// Go to specific page
function goToPage(page) {
    if (page >= 1 && page <= totalPages && page !== currentPage) {
        currentPage = page;
        renderSearchResults();
        renderPagination();
        
        // Scroll to top of results
        searchResults.scrollIntoView({ behavior: 'smooth', block: 'start' });
    }
}

// Show project modal
function showProjectModal(itemId) {
    const item = allData.find(project => project.id == itemId);
    if (!item) return;

    modalTitle.textContent = item.displayName || item.name;
    
    const github = item.github;
    const tags = item.tags ? item.tags.split(',').map(tag => tag.trim()) : [];
    
    modalBody.innerHTML = `
        <div class="modal-grid">
            <div class="modal-info">
                <div class="info-row">
                    <span class="info-label">Author</span>
                    <span class="info-value">${escapeHtml(item.author_name)}</span>
                </div>
                <div class="info-row">
                    <span class="info-label">Type</span>
                    <span class="info-value">
                        <span class="project-type ${item.type}">${item.type}</span>
                    </span>
                </div>
                ${item.category ? `
                    <div class="info-row">
                        <span class="info-label">Category</span>
                        <span class="info-value">
                            <span class="category-badge">${formatCategoryName(item.category)}</span>
                        </span>
                    </div>
                ` : ''}
                ${item.url ? `
                    <div class="info-row">
                        <span class="info-label">Website</span>
                        <span class="info-value">
                            <a href="${escapeHtml(item.url)}" target="_blank" class="external-link">
                                <i class="fas fa-external-link-alt"></i>
                                Visit Project
                            </a>
                        </span>
                    </div>
                ` : ''}
            </div>

            ${github ? `
                <div class="modal-github">
                    <h4><i class="fab fa-github"></i> GitHub Repository</h4>
                    <div class="github-stats">
                        <div class="github-stat">
                            <div class="github-stat-value">${formatNumber(github.stargazers_count || 0)}</div>
                            <div class="github-stat-label"><i class="fas fa-star"></i> Stars</div>
                        </div>
                        <div class="github-stat">
                            <div class="github-stat-value">${formatNumber(github.forks_count || 0)}</div>
                            <div class="github-stat-label"><i class="fas fa-code-branch"></i> Forks</div>
                        </div>
                        <div class="github-stat">
                            <div class="github-stat-value">${formatNumber(github.open_issues_count || 0)}</div>
                            <div class="github-stat-label"><i class="fas fa-exclamation-circle"></i> Issues</div>
                        </div>
                        <div class="github-stat">
                            <div class="github-stat-value">${formatNumber(github.contributors_count || 0)}</div>
                            <div class="github-stat-label"><i class="fas fa-users"></i> Contributors</div>
                        </div>
                        ${github.language ? `
                            <div class="github-stat">
                                <div class="github-stat-value">${escapeHtml(github.language)}</div>
                                <div class="github-stat-label"><i class="fas fa-code"></i> Language</div>
                            </div>
                        ` : ''}
                        ${github.license ? `
                            <div class="github-stat">
                                <div class="github-stat-value">${escapeHtml(github.license)}</div>
                                <div class="github-stat-label"><i class="fas fa-balance-scale"></i> License</div>
                            </div>
                        ` : ''}
                    </div>
                    ${github.full_name ? `
                        <a href="https://github.com/${escapeHtml(github.full_name)}" target="_blank" class="github-link">
                            <i class="fab fa-github"></i>
                            View on GitHub
                        </a>
                    ` : ''}
                </div>
            ` : ''}
        </div>
        
        ${item.description ? `
            <div class="modal-section">
                <h4>Description</h4>
                <p class="description-text">${escapeHtml(item.description)}</p>
            </div>
        ` : ''}
        
        ${tags.length > 0 ? `
            <div class="modal-section">
                <h4>Tags</h4>
                <div class="project-tags">
                    ${tags.map(tag => `<span class="tag">${escapeHtml(tag)}</span>`).join('')}
                </div>
            </div>
        ` : ''}
    `;
    
    modal.classList.add('active');
}

// Close modal
function closeModal() {
    modal.classList.remove('active');
}

// Utility functions
function escapeHtml(text) {
    const div = document.createElement('div');
    div.textContent = text;
    return div.innerHTML;
}

function formatCategoryName(category) {
    if (!category) return '';
    
    const categoryMap = {
        'developer-tools': 'Developer Tools',
        'communication': 'Communication',
        'ai-chatbot': 'AI Chatbot',
        'research-and-data': 'Research & Data',
        'security': 'Security',
        'speech-processing': 'Speech Processing',
        'location-services': 'Location Services',
        'browser-automation': 'Browser Automation',
        'databases': 'Databases',
        'knowledge-and-memory': 'Knowledge & Memory',
        'cloud-platforms': 'Cloud Platforms',
        'finance': 'Finance'
    };
    
    return categoryMap[category] || 
           category.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
}

function debounce(func, wait) {
    let timeout;
    return function executedFunction(...args) {
        const later = () => {
            clearTimeout(timeout);
            func(...args);
        };
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
    };
}

function showLoading() {
    // You can implement loading states here
    console.log('Loading...');
}

function hideLoading() {
    // You can implement hiding loading states here
    console.log('Loading complete');
}

function showError(message) {
    console.error(message);
    // You can implement error notification here
}