Skip to content

Commit a1cce4a

Browse files
feat(website): add GitHub counter in navbar and community
- Add reusable GitHubCounter segmented pill component supporting stars, forks, and prs metrics - Add formatGitHubCount utility for friendly count bucketing and compact notation - Add server-side cached GitHub API fetch with 1-hour revalidation - Replace static GitHub links in navbar and mobile menu with GitHubCounter - Add unit tests in test/github-counter.test.tsx - Closes #36
1 parent 7cdf183 commit a1cce4a

6 files changed

Lines changed: 320 additions & 47 deletions

File tree

‎src/app/page.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,23 @@ import { Community } from "@/components/landing/community";
66
import { Faq } from "@/components/landing/faq";
77
import { Footer } from "@/components/landing/footer";
88

9+
import { getGitHubStats } from "@/lib/github";
10+
911
export const metadata: Metadata = {
1012
alternates: { canonical: "/" },
1113
};
1214

13-
export default function Home() {
15+
export default async function Home() {
16+
const stats = await getGitHubStats();
17+
1418
return (
1519
<div className="flex flex-1 flex-col">
16-
<Navbar />
20+
<Navbar starsCount={stats.stars} />
1721
<main>
1822
<Hero />
1923
<Features />
2024

21-
<Community />
25+
<Community stats={stats} />
2226
<Faq />
2327
</main>
2428
<Footer />

‎src/components/landing/community.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Reveal } from "./reveal";
2+
import { GitHubCounter } from "./github-counter";
3+
import type { GitHubStats } from "@/lib/github";
24

35
const communityLinks = [
46
{
@@ -51,7 +53,11 @@ const communityLinks = [
5153
},
5254
];
5355

54-
export function Community() {
56+
export interface CommunityProps {
57+
stats?: GitHubStats;
58+
}
59+
60+
export function Community({ stats }: CommunityProps = {}) {
5561
return (
5662
<section className="relative overflow-hidden py-24 md:py-32">
5763
<div className="relative mx-auto max-w-6xl px-4 sm:px-6 lg:px-8">
@@ -66,6 +72,13 @@ export function Community() {
6672
Connect with other developers, get help, and contribute to the project
6773
</p>
6874
</Reveal>
75+
<Reveal delay={0.15}>
76+
<div className="mt-6 flex flex-wrap items-center justify-center gap-3">
77+
<GitHubCounter metric="stars" count={stats?.stars} />
78+
<GitHubCounter metric="forks" count={stats?.forks} />
79+
<GitHubCounter metric="prs" count={stats?.prs} />
80+
</div>
81+
</Reveal>
6982
</div>
7083

7184
<div className="mt-16 grid gap-6 sm:grid-cols-3">
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import * as React from "react";
2+
import { Star, GitFork, GitPullRequest } from "lucide-react";
3+
import { cn } from "@/lib/utils";
4+
5+
export type GitHubMetric = "stars" | "forks" | "prs";
6+
7+
export interface GitHubCounterProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
8+
metric?: GitHubMetric;
9+
count?: number | null;
10+
hideCountOnMobile?: boolean;
11+
}
12+
13+
/**
14+
* Format a raw GitHub count into a friendly bucket followed by `+`.
15+
* - Numbers below 10 (or missing/0) return `null` so no count is shown.
16+
* - 10-99 round down to the nearest 10 (e.g. 10+, 40+).
17+
* - 100-999 round down to the nearest 50 (e.g. 150+, 500+, 950+).
18+
* - 1000+ use Intl.NumberFormat compact notation rounded down to 1 decimal place (e.g. 1k+, 1.2k+, 12.5k+).
19+
*/
20+
export function formatGitHubCount(count?: number | null): string | null {
21+
if (count == null || count < 10) {
22+
return null;
23+
}
24+
if (count < 100) {
25+
const bucket = Math.floor(count / 10) * 10;
26+
return `${bucket}+`;
27+
}
28+
if (count < 1000) {
29+
const bucket = Math.floor(count / 50) * 50;
30+
return `${bucket}+`;
31+
}
32+
const rounded = Math.floor(count / 100) * 100;
33+
const formatted = new Intl.NumberFormat("en", {
34+
notation: "compact",
35+
maximumFractionDigits: 1,
36+
})
37+
.format(rounded)
38+
.toLowerCase();
39+
return `${formatted}+`;
40+
}
41+
42+
const metricConfig = {
43+
stars: {
44+
label: "Star babelize-elements on GitHub",
45+
noun: "stars",
46+
href: "https://github.com/babelize/babelize-elements",
47+
icon: Star,
48+
},
49+
forks: {
50+
label: "Fork babelize-elements on GitHub",
51+
noun: "forks",
52+
href: "https://github.com/babelize/babelize-elements/forks",
53+
icon: GitFork,
54+
},
55+
prs: {
56+
label: "Pull requests for babelize-elements on GitHub",
57+
noun: "PRs",
58+
href: "https://github.com/babelize/babelize-elements/pulls",
59+
icon: GitPullRequest,
60+
},
61+
} as const;
62+
63+
export function GitHubCounter({
64+
metric = "stars",
65+
count,
66+
hideCountOnMobile = false,
67+
className,
68+
...props
69+
}: GitHubCounterProps) {
70+
const config = metricConfig[metric] ?? metricConfig.stars;
71+
const formattedCount = formatGitHubCount(count);
72+
const MetricIcon = config.icon;
73+
74+
const ariaLabel = formattedCount
75+
? `${config.label} (${formattedCount} ${config.noun})`
76+
: config.label;
77+
78+
return (
79+
<a
80+
href={props.href ?? config.href}
81+
target="_blank"
82+
rel="noopener noreferrer"
83+
aria-label={ariaLabel}
84+
className={cn(
85+
"group inline-flex h-9 items-center rounded-full border border-white/[0.1] bg-white/[0.03] text-sm text-white/80 transition-colors hover:bg-white/5 hover:text-white",
86+
className,
87+
)}
88+
{...props}
89+
>
90+
{/* Left: GitHub logo */}
91+
<span className="flex items-center justify-center pl-3 pr-2.5">
92+
<svg
93+
width="16"
94+
height="16"
95+
viewBox="0 0 24 24"
96+
fill="currentColor"
97+
aria-hidden="true"
98+
className="size-4 text-white/80 transition-colors group-hover:text-white"
99+
>
100+
<path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12" />
101+
</svg>
102+
</span>
103+
104+
{/* 1px vertical divider */}
105+
<span className="h-3.5 w-px bg-white/[0.1]" aria-hidden="true" />
106+
107+
{/* Right: Count and metric icon */}
108+
<span className="flex items-center gap-1.5 pl-2.5 pr-3">
109+
{formattedCount && (
110+
<span
111+
className={cn(
112+
"text-xs font-medium sm:text-sm",
113+
hideCountOnMobile && "hidden sm:inline",
114+
)}
115+
>
116+
{formattedCount}
117+
</span>
118+
)}
119+
<MetricIcon
120+
size={14}
121+
className="text-white/70 transition-colors group-hover:text-white"
122+
aria-hidden="true"
123+
/>
124+
</span>
125+
</a>
126+
);
127+
}

‎src/components/landing/navbar.tsx‎

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Link from "next/link";
44
import Image from "next/image";
55
import { useState } from "react";
66
import { Menu, X, CornerDownLeft } from "lucide-react";
7+
import { GitHubCounter } from "./github-counter";
78

89
const navLinks = [
910
{ label: "Home", href: "/" },
@@ -12,7 +13,11 @@ const navLinks = [
1213
{ label: "Contribute", href: "/docs/getting-started/contributing" },
1314
];
1415

15-
export function Navbar() {
16+
export interface NavbarProps {
17+
starsCount?: number | null;
18+
}
19+
20+
export function Navbar({ starsCount }: NavbarProps = {}) {
1621
const [isOpen, setIsOpen] = useState(false);
1722

1823
return (
@@ -39,44 +44,14 @@ export function Navbar() {
3944

4045
{/* Right side actions */}
4146
<div className="flex items-center gap-2 sm:gap-3">
42-
{/* GitHub button - desktop */}
47+
{/* GitHub counter - desktop */}
4348
<div className="hidden sm:block">
44-
<a
45-
href="https://github.com/babelize/babelize-elements"
46-
target="_blank"
47-
rel="noopener noreferrer"
48-
className="inline-flex size-9 items-center justify-center rounded-full border border-white/[0.1] text-white/80 transition-colors hover:bg-white/5 hover:text-white"
49-
>
50-
<svg
51-
width="18"
52-
height="18"
53-
viewBox="0 0 24 24"
54-
fill="currentColor"
55-
aria-hidden="true"
56-
>
57-
<path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12" />
58-
</svg>
59-
</a>
49+
<GitHubCounter metric="stars" count={starsCount} />
6050
</div>
6151

62-
{/* GitHub button - mobile */}
52+
{/* GitHub counter - mobile */}
6353
<div className="sm:hidden">
64-
<a
65-
href="https://github.com/babelize/babelize-elements"
66-
target="_blank"
67-
rel="noopener noreferrer"
68-
className="inline-flex size-8 items-center justify-center rounded-full border border-white/[0.1] text-white/80 transition-colors hover:bg-white/5"
69-
>
70-
<svg
71-
width="14"
72-
height="14"
73-
viewBox="0 0 24 24"
74-
fill="currentColor"
75-
aria-hidden="true"
76-
>
77-
<path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12" />
78-
</svg>
79-
</a>
54+
<GitHubCounter metric="stars" count={starsCount} hideCountOnMobile />
8055
</div>
8156

8257
{/* Get Started button */}
@@ -121,14 +96,13 @@ export function Navbar() {
12196
{link.label}
12297
</Link>
12398
))}
124-
<a
125-
href="https://github.com/babelize/babelize-elements"
126-
target="_blank"
127-
rel="noopener noreferrer"
128-
className="mt-1 rounded-xl border border-white/[0.1] px-4 py-2.5 text-center text-sm text-white/80"
129-
>
130-
Star on GitHub
131-
</a>
99+
<div className="mt-1 flex justify-center">
100+
<GitHubCounter
101+
metric="stars"
102+
count={starsCount}
103+
className="w-full justify-center py-2.5 h-auto text-center"
104+
/>
105+
</div>
132106
</div>
133107
</div>
134108
)}

‎src/lib/github.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
export interface GitHubStats {
2+
stars?: number | null;
3+
forks?: number | null;
4+
prs?: number | null;
5+
}
6+
7+
const REPO_URL = "https://api.github.com/repos/babelize/babelize-elements";
8+
const PRS_URL =
9+
"https://api.github.com/search/issues?q=repo:babelize/babelize-elements+is:pr+is:open";
10+
11+
/**
12+
* Fetch GitHub metrics on the server with time-based revalidation (1 hour).
13+
* Uses public unauthenticated endpoints and returns null values on failure
14+
* so the frontend never shows 0 or an error.
15+
*/
16+
export async function getGitHubStats(): Promise<GitHubStats> {
17+
const fallback: GitHubStats = {
18+
stars: null,
19+
forks: null,
20+
prs: null,
21+
};
22+
23+
try {
24+
const headers = {
25+
Accept: "application/vnd.github+json",
26+
"User-Agent": "babelize-elements",
27+
};
28+
29+
const [repoRes, prsRes] = await Promise.allSettled([
30+
fetch(REPO_URL, {
31+
headers,
32+
next: { revalidate: 3600 },
33+
}),
34+
fetch(PRS_URL, {
35+
headers,
36+
next: { revalidate: 3600 },
37+
}),
38+
]);
39+
40+
const stats: GitHubStats = { ...fallback };
41+
42+
if (repoRes.status === "fulfilled" && repoRes.value.ok) {
43+
const data = await repoRes.value.json();
44+
if (typeof data.stargazers_count === "number") {
45+
stats.stars = data.stargazers_count;
46+
}
47+
if (typeof data.forks_count === "number") {
48+
stats.forks = data.forks_count;
49+
}
50+
}
51+
52+
if (prsRes.status === "fulfilled" && prsRes.value.ok) {
53+
const data = await prsRes.value.json();
54+
if (typeof data.total_count === "number") {
55+
stats.prs = data.total_count;
56+
}
57+
}
58+
59+
return stats;
60+
} catch {
61+
return fallback;
62+
}
63+
}

0 commit comments

Comments
 (0)