|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import styles from './Contributors.module.css'; |
| 3 | + |
| 4 | +const REPOS = [ |
| 5 | + 'sparkison/m3u-editor', |
| 6 | + 'sparkison/m3u-proxy', |
| 7 | + 'sparkison/m3u-editor-docs-v2' |
| 8 | +]; |
| 9 | + |
| 10 | +export default function Contributors() { |
| 11 | + const [contributors, setContributors] = useState([]); |
| 12 | + const [loading, setLoading] = useState(true); |
| 13 | + const [error, setError] = useState(null); |
| 14 | + |
| 15 | + useEffect(() => { |
| 16 | + fetchContributors(); |
| 17 | + }, []); |
| 18 | + |
| 19 | + const fetchContributors = async () => { |
| 20 | + try { |
| 21 | + const allContributors = new Map(); |
| 22 | + |
| 23 | + // Fetch contributors from all repos |
| 24 | + for (const repo of REPOS) { |
| 25 | + const response = await fetch( |
| 26 | + `https://api.github.com/repos/${repo}/contributors?per_page=100`, |
| 27 | + { |
| 28 | + headers: { |
| 29 | + 'Accept': 'application/vnd.github.v3+json' |
| 30 | + } |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + if (!response.ok) { |
| 35 | + throw new Error(`Failed to fetch contributors for ${repo}`); |
| 36 | + } |
| 37 | + |
| 38 | + const data = await response.json(); |
| 39 | + |
| 40 | + // Merge contributors, filtering out bots |
| 41 | + data.forEach(contributor => { |
| 42 | + // Skip bots (any login ending with [bot]) |
| 43 | + if (contributor.login.endsWith('[bot]')) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (allContributors.has(contributor.login)) { |
| 48 | + // Add contributions from this repo to existing contributor |
| 49 | + const existing = allContributors.get(contributor.login); |
| 50 | + existing.contributions += contributor.contributions; |
| 51 | + } else { |
| 52 | + // Add new contributor |
| 53 | + allContributors.set(contributor.login, { |
| 54 | + login: contributor.login, |
| 55 | + avatar_url: contributor.avatar_url, |
| 56 | + html_url: contributor.html_url, |
| 57 | + contributions: contributor.contributions |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + // Convert to array and sort by contributions |
| 64 | + const sortedContributors = Array.from(allContributors.values()) |
| 65 | + .sort((a, b) => b.contributions - a.contributions); |
| 66 | + |
| 67 | + setContributors(sortedContributors); |
| 68 | + setLoading(false); |
| 69 | + } catch (err) { |
| 70 | + console.error('Error fetching contributors:', err); |
| 71 | + setError(err.message); |
| 72 | + setLoading(false); |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + if (loading) { |
| 77 | + return ( |
| 78 | + <section className={styles.contributorsSection}> |
| 79 | + <div className="container"> |
| 80 | + <h2 className={styles.heading}>Contributors</h2> |
| 81 | + <p className={styles.loading}>Loading contributors...</p> |
| 82 | + </div> |
| 83 | + </section> |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + if (error) { |
| 88 | + return ( |
| 89 | + <section className={styles.contributorsSection}> |
| 90 | + <div className="container"> |
| 91 | + <h2 className={styles.heading}>Contributors</h2> |
| 92 | + <p className={styles.error}>Unable to load contributors. Please check back later.</p> |
| 93 | + </div> |
| 94 | + </section> |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + return ( |
| 99 | + <section className={styles.contributorsSection}> |
| 100 | + <div className="container"> |
| 101 | + <h2 className={styles.heading}>Contributors</h2> |
| 102 | + <p className={styles.subtitle}> |
| 103 | + Thank you to all the amazing people who have contributed to the M3U Editor project! 🙌 |
| 104 | + </p> |
| 105 | + <div className={styles.contributorsGrid}> |
| 106 | + {contributors.map((contributor) => ( |
| 107 | + <a |
| 108 | + key={contributor.login} |
| 109 | + href={contributor.html_url} |
| 110 | + target="_blank" |
| 111 | + rel="noopener noreferrer" |
| 112 | + className={styles.contributorCard} |
| 113 | + title={`${contributor.login} - ${contributor.contributions} contributions`} |
| 114 | + > |
| 115 | + <img |
| 116 | + src={contributor.avatar_url} |
| 117 | + alt={contributor.login} |
| 118 | + className={styles.avatar} |
| 119 | + loading="lazy" |
| 120 | + /> |
| 121 | + <div className={styles.contributorInfo}> |
| 122 | + <div className={styles.username}>{contributor.login}</div> |
| 123 | + <div className={styles.contributions}> |
| 124 | + {contributor.contributions} {contributor.contributions === 1 ? 'contribution' : 'contributions'} |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </a> |
| 128 | + ))} |
| 129 | + </div> |
| 130 | + <p className={styles.footer}> |
| 131 | + Contributors are fetched from the{' '} |
| 132 | + <a href="https://github.com/sparkison/m3u-editor" target="_blank" rel="noopener noreferrer"> |
| 133 | + m3u-editor |
| 134 | + </a> |
| 135 | + ,{' '} |
| 136 | + <a href="https://github.com/sparkison/m3u-proxy" target="_blank" rel="noopener noreferrer"> |
| 137 | + m3u-proxy |
| 138 | + </a> |
| 139 | + , and{' '} |
| 140 | + <a href="https://github.com/sparkison/m3u-editor-docs-v2" target="_blank" rel="noopener noreferrer"> |
| 141 | + m3u-editor-docs-v2 |
| 142 | + </a> |
| 143 | + {' '}repositories. |
| 144 | + </p> |
| 145 | + </div> |
| 146 | + </section> |
| 147 | + ); |
| 148 | +} |
0 commit comments