|
| 1 | +import React, { useEffect, useState } from 'react' |
| 2 | + |
| 3 | +const GITHUB_REPO = 'sonofmagic/weapp-tailwindcss' |
| 4 | +const CACHE_KEY = 'weapp-tailwindcss:github-stars' |
| 5 | +const CACHE_TTL = 1000 * 60 * 30 // 30 minutes |
| 6 | + |
| 7 | +async function fetchStars(): Promise<number | null> { |
| 8 | + try { |
| 9 | + const response = await fetch(`https://api.github.com/repos/${GITHUB_REPO}`) |
| 10 | + if (!response.ok) { |
| 11 | + return null |
| 12 | + } |
| 13 | + |
| 14 | + const data = await response.json() |
| 15 | + return typeof data.stargazers_count === 'number' ? data.stargazers_count : null |
| 16 | + } |
| 17 | + catch (error) { |
| 18 | + console.error('Failed to fetch GitHub stars', error) |
| 19 | + return null |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function readCache(): number | null { |
| 24 | + if (typeof window === 'undefined') { |
| 25 | + return null |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + const payload = window.sessionStorage.getItem(CACHE_KEY) |
| 30 | + if (!payload) { |
| 31 | + return null |
| 32 | + } |
| 33 | + |
| 34 | + const parsed = JSON.parse(payload) as { value: number, expires: number } |
| 35 | + if (Date.now() > parsed.expires) { |
| 36 | + return null |
| 37 | + } |
| 38 | + |
| 39 | + return parsed.value |
| 40 | + } |
| 41 | + catch (error) { |
| 42 | + console.warn('Failed to read cached GitHub stars', error) |
| 43 | + return null |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function writeCache(value: number) { |
| 48 | + if (typeof window === 'undefined') { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + try { |
| 53 | + const payload = JSON.stringify({ value, expires: Date.now() + CACHE_TTL }) |
| 54 | + window.sessionStorage.setItem(CACHE_KEY, payload) |
| 55 | + } |
| 56 | + catch (error) { |
| 57 | + console.warn('Failed to cache GitHub stars', error) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +export default function HeroGithubBadge(): JSX.Element { |
| 62 | + const [stars, setStars] = useState<number | null>(() => readCache()) |
| 63 | + |
| 64 | + useEffect(() => { |
| 65 | + if (stars != null) { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + let cancelled = false |
| 70 | + fetchStars().then((value) => { |
| 71 | + if (cancelled || value == null) { |
| 72 | + return |
| 73 | + } |
| 74 | + writeCache(value) |
| 75 | + setStars(value) |
| 76 | + }) |
| 77 | + |
| 78 | + return () => { |
| 79 | + cancelled = true |
| 80 | + } |
| 81 | + }, [stars]) |
| 82 | + |
| 83 | + return ( |
| 84 | + <a |
| 85 | + className="hero-github-badge" |
| 86 | + href="https://github.com/sonofmagic/weapp-tailwindcss" |
| 87 | + target="_blank" |
| 88 | + rel="noreferrer" |
| 89 | + aria-label="Star sonofmagic/weapp-tailwindcss on GitHub" |
| 90 | + > |
| 91 | + <span className="hero-github-badge__icon" aria-hidden="true"> |
| 92 | + ★ |
| 93 | + </span> |
| 94 | + <span className="hero-github-badge__label">GitHub</span> |
| 95 | + <span className="hero-github-badge__count"> |
| 96 | + {stars != null ? stars.toLocaleString() : '—'} |
| 97 | + </span> |
| 98 | + </a> |
| 99 | + ) |
| 100 | +} |
0 commit comments