|
| 1 | +import { faStar } from '@fortawesome/free-solid-svg-icons' |
| 2 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' |
| 3 | +import { GitHubIcon } from 'nextra/icons' |
| 4 | +import Link from 'next/link' |
| 5 | + |
| 6 | +async function getStarCount() { |
| 7 | + try { |
| 8 | + const res = await fetch('https://api.github.com/repos/FOSSBilling/FOSSBilling', { |
| 9 | + next: { revalidate: 7200 } // Cache for 2 hours |
| 10 | + }) |
| 11 | + |
| 12 | + if (!res.ok) { |
| 13 | + throw new Error('Failed to fetch stars') |
| 14 | + } |
| 15 | + |
| 16 | + const data = await res.json() |
| 17 | + return data.stargazers_count |
| 18 | + } catch (error) { |
| 19 | + console.error('Error fetching star count:', error) |
| 20 | + return null |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function formatCompactNumber(number: number) { |
| 25 | + const formatter = Intl.NumberFormat("en", { notation: "compact" }); |
| 26 | + return formatter.format(number); |
| 27 | +} |
| 28 | + |
| 29 | +export default async function GitHubStarButton() { |
| 30 | + const stars = await getStarCount() |
| 31 | + |
| 32 | + return ( |
| 33 | + <Link |
| 34 | + href="https://github.com/FOSSBilling/FOSSBilling" |
| 35 | + target="_blank" |
| 36 | + className="group flex items-center gap-2 rounded-md bg-black/10 px-3 py-1.5 text-sm font-medium text-gray-700 transition-colors hover:bg-black/20 dark:bg-white/10 dark:text-gray-200 dark:hover:bg-white/20" |
| 37 | + aria-label="Star FOSSBilling on GitHub" |
| 38 | + > |
| 39 | + <GitHubIcon className="h-4 w-4" /> |
| 40 | + <span>Star</span> |
| 41 | + {stars !== null && ( |
| 42 | + <> |
| 43 | + <div className="h-4 w-px bg-gray-300 dark:bg-gray-600" /> |
| 44 | + <div className="flex items-center gap-1"> |
| 45 | + <FontAwesomeIcon icon={faStar} className="h-3 w-3 text-yellow-500" /> |
| 46 | + <span>{formatCompactNumber(stars)}</span> |
| 47 | + </div> |
| 48 | + </> |
| 49 | + )} |
| 50 | + </Link> |
| 51 | + ) |
| 52 | +} |
0 commit comments