|
| 1 | +import { useEffect, useState, type ReactNode } from 'react'; |
| 2 | + |
| 3 | +import styles from './index.module.css'; |
| 4 | + |
| 5 | +type GitHubSocialProofVariant = 'hero' | 'project'; |
| 6 | + |
| 7 | +interface GitHubSocialProofProps { |
| 8 | + repo: string; |
| 9 | + fallbackStars: number; |
| 10 | + variant: GitHubSocialProofVariant; |
| 11 | +} |
| 12 | + |
| 13 | +interface GitHubRepository { |
| 14 | + stargazers_count: number; |
| 15 | +} |
| 16 | + |
| 17 | +const starCountRequests = new Map<string, Promise<number | null>>(); |
| 18 | + |
| 19 | +function fetchStarCount(repo: string): Promise<number | null> { |
| 20 | + const cachedRequest = starCountRequests.get(repo); |
| 21 | + if (cachedRequest) return cachedRequest; |
| 22 | + |
| 23 | + const request = fetch(`https://api.github.com/repos/${repo}`) |
| 24 | + .then(async (response) => { |
| 25 | + if (!response.ok) return null; |
| 26 | + const repository = (await response.json()) as GitHubRepository; |
| 27 | + return Number.isSafeInteger(repository.stargazers_count) |
| 28 | + ? repository.stargazers_count |
| 29 | + : null; |
| 30 | + }) |
| 31 | + .catch(() => null); |
| 32 | + starCountRequests.set(repo, request); |
| 33 | + return request; |
| 34 | +} |
| 35 | + |
| 36 | +function useGitHubStars(repo: string, fallbackStars: number): number { |
| 37 | + const [stars, setStars] = useState(fallbackStars); |
| 38 | + |
| 39 | + useEffect(() => { |
| 40 | + let active = true; |
| 41 | + fetchStarCount(repo).then((count) => { |
| 42 | + if (active && count !== null) setStars(count); |
| 43 | + }); |
| 44 | + return () => { |
| 45 | + active = false; |
| 46 | + }; |
| 47 | + }, [repo]); |
| 48 | + |
| 49 | + return stars; |
| 50 | +} |
| 51 | + |
| 52 | +function formatStars(stars: number): string { |
| 53 | + if (stars < 1_000) return String(stars); |
| 54 | + return `${(stars / 1_000).toFixed(stars < 10_000 ? 1 : 0).replace('.0', '')}k`; |
| 55 | +} |
| 56 | + |
| 57 | +function StarIcon(): ReactNode { |
| 58 | + return ( |
| 59 | + <svg viewBox="0 0 24 24" aria-hidden="true"> |
| 60 | + <path d="m12 3 2.8 5.7 6.2.9-4.5 4.4 1.1 6.2-5.6-3-5.6 3 1.1-6.2L3 9.6l6.2-.9L12 3Z" /> |
| 61 | + </svg> |
| 62 | + ); |
| 63 | +} |
| 64 | + |
| 65 | +function StarCount({ |
| 66 | + stars, |
| 67 | + label = 'stars', |
| 68 | +}: { |
| 69 | + stars: number; |
| 70 | + label?: string; |
| 71 | +}): ReactNode { |
| 72 | + return ( |
| 73 | + <span className={styles.starCount} aria-live="polite"> |
| 74 | + <span className={styles.count}>{formatStars(stars)}</span> |
| 75 | + <span className={styles.label}>{label}</span> |
| 76 | + </span> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +export default function GitHubSocialProof({ |
| 81 | + repo, |
| 82 | + fallbackStars, |
| 83 | + variant, |
| 84 | +}: GitHubSocialProofProps): ReactNode { |
| 85 | + const stars = useGitHubStars(repo, fallbackStars); |
| 86 | + const starCount = <StarCount stars={stars} />; |
| 87 | + |
| 88 | + if (variant === 'project') { |
| 89 | + return ( |
| 90 | + <span className={styles.project}> |
| 91 | + <StarIcon /> |
| 92 | + {starCount} |
| 93 | + </span> |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + return ( |
| 98 | + <a |
| 99 | + className={styles.hero} |
| 100 | + href={`https://github.com/${repo}`} |
| 101 | + target="_blank" |
| 102 | + rel="noopener noreferrer" |
| 103 | + aria-label={`GitHub repository: ${formatStars(stars)} stars`} |
| 104 | + > |
| 105 | + <StarIcon /> |
| 106 | + <StarCount stars={stars} label="GitHub stars" /> |
| 107 | + </a> |
| 108 | + ); |
| 109 | +} |
0 commit comments