|
| 1 | +import { useEffect, useState } from 'react' |
| 2 | + |
| 3 | +const ENDPOINT = 'https://gh-pinned-repos.egoist.dev/?username=codeyogico' |
| 4 | + |
| 5 | +function clean(r) { |
| 6 | + const repo = (r.repo || '').trim() |
| 7 | + const link = (r.link || '').trim() || `https://github.com/${r.owner}/${repo}` |
| 8 | + return { |
| 9 | + owner: r.owner, |
| 10 | + repo, |
| 11 | + link: link.replace(/\s+/g, ''), |
| 12 | + description: (r.description || '').trim(), |
| 13 | + language: (r.language || '').trim(), |
| 14 | + stars: Number(r.stars) || 0, |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +export default function PinnedRepos() { |
| 19 | + const [repos, setRepos] = useState(null) |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + let cancelled = false |
| 23 | + fetch(ENDPOINT) |
| 24 | + .then((r) => (r.ok ? r.json() : null)) |
| 25 | + .then((data) => { |
| 26 | + if (cancelled || !Array.isArray(data)) return |
| 27 | + setRepos(data.map(clean).filter((r) => r.repo)) |
| 28 | + }) |
| 29 | + .catch(() => {}) |
| 30 | + return () => { |
| 31 | + cancelled = true |
| 32 | + } |
| 33 | + }, []) |
| 34 | + |
| 35 | + if (!repos || repos.length === 0) return null |
| 36 | + |
| 37 | + return ( |
| 38 | + <section className="pinned"> |
| 39 | + <h2 className="pinned-title">github</h2> |
| 40 | + <ul className="pinned-list"> |
| 41 | + {repos.map((r) => ( |
| 42 | + <li key={`${r.owner}/${r.repo}`}> |
| 43 | + <a href={r.link} target="_blank" rel="noopener noreferrer" className="pinned-card"> |
| 44 | + <div className="pinned-row"> |
| 45 | + <span className="pinned-name">{r.repo}</span> |
| 46 | + <span className="pinned-meta"> |
| 47 | + {r.language && <span className="pinned-lang">{r.language}</span>} |
| 48 | + {r.stars > 0 && <span className="pinned-stars">★ {r.stars}</span>} |
| 49 | + </span> |
| 50 | + </div> |
| 51 | + {r.description && <div className="pinned-desc">{r.description}</div>} |
| 52 | + </a> |
| 53 | + </li> |
| 54 | + ))} |
| 55 | + </ul> |
| 56 | + </section> |
| 57 | + ) |
| 58 | +} |
0 commit comments