diff --git a/src/components/Header.astro b/src/components/Header.astro index 01919dac..094b87a6 100644 --- a/src/components/Header.astro +++ b/src/components/Header.astro @@ -86,16 +86,24 @@ const currentPath = Astro.url.pathname;
+ = 1000) { + return (count / 1000).toFixed(count >= 10000 ? 0 : 1).replace(/\.0$/, '') + 'k'; + } + return String(count); + } + + async function loadGitHubStars() { + const targets = document.querySelectorAll('[data-github-stars]'); + if (!targets.length) return; + const CACHE_KEY = 'gh-stars-future-agi'; + const CACHE_TTL = 10 * 60 * 1000; + try { + const cached = localStorage.getItem(CACHE_KEY); + if (cached) { + const { count, ts } = JSON.parse(cached); + if (Date.now() - ts < CACHE_TTL && typeof count === 'number') { + targets.forEach(el => { el.textContent = formatStars(count); }); + return; + } + } + } catch {} + try { + const res = await fetch('https://api.github.com/repos/future-agi/future-agi'); + if (!res.ok) throw new Error('GitHub API error'); + const data = await res.json(); + const count = data.stargazers_count || 0; + targets.forEach(el => { el.textContent = formatStars(count); }); + try { localStorage.setItem(CACHE_KEY, JSON.stringify({ count, ts: Date.now() })); } catch {} + } catch { + targets.forEach(el => { el.textContent = '★'; }); + } + } + loadGitHubStars(); + document.addEventListener('astro:page-load', loadGitHubStars); })();