Skip to content

Commit 09a2321

Browse files
committed
Display GitHub star count in Navbar and MobileMenu
1 parent da229de commit 09a2321

4 files changed

Lines changed: 59 additions & 7 deletions

File tree

apps/web/app/(site)/Navbar.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ const Links = [
112112
},
113113
];
114114

115-
export const Navbar = () => {
115+
interface NavbarProps {
116+
stars?: string;
117+
}
118+
119+
export const Navbar = ({ stars }: NavbarProps) => {
116120
const pathname = usePathname();
117121
const [showMobileMenu, setShowMobileMenu] = useState(false);
118122
const auth = useCurrentUser();
@@ -222,7 +226,7 @@ export const Navbar = () => {
222226
size="sm"
223227
className="w-full font-medium sm:w-auto"
224228
>
225-
Github
229+
{`GitHub${stars ? ` (${stars})` : ""}`}
226230
</Button>
227231
<Suspense
228232
fallback={
@@ -288,7 +292,11 @@ export const Navbar = () => {
288292
</nav>
289293
</header>
290294
{showMobileMenu && (
291-
<MobileMenu setShowMobileMenu={setShowMobileMenu} auth={auth} />
295+
<MobileMenu
296+
setShowMobileMenu={setShowMobileMenu}
297+
auth={auth}
298+
stars={stars}
299+
/>
292300
)}
293301
</>
294302
);

apps/web/app/(site)/layout.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import type { PropsWithChildren } from "react";
2+
import { formatStarCount, getGitHubStars } from "@/utils/github";
23
import { Intercom } from "../Layout/Intercom";
34
import { Footer } from "./Footer";
45
import { Navbar } from "./Navbar";
56

6-
export default function Layout(props: PropsWithChildren) {
7+
export default async function Layout(props: PropsWithChildren) {
8+
const starCount = await getGitHubStars();
9+
const stars = formatStarCount(starCount);
10+
711
return (
812
<>
9-
<Navbar />
13+
<Navbar stars={stars} />
1014
{props.children}
1115
<Footer />
1216
<Intercom />

apps/web/components/ui/MobileMenu.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { User } from "next-auth";
55
interface MobileMenuProps {
66
setShowMobileMenu: (showMobileMenu: boolean) => void;
77
auth: User | null;
8+
stars?: string;
89
}
910

1011
interface NavLink {
@@ -55,7 +56,11 @@ const externalLinks: NavLink[] = [
5556
},
5657
];
5758

58-
const MobileMenu: React.FC<MobileMenuProps> = ({ setShowMobileMenu, auth }) => {
59+
const MobileMenu: React.FC<MobileMenuProps> = ({
60+
setShowMobileMenu,
61+
auth,
62+
stars,
63+
}) => {
5964
return (
6065
<div className="block overflow-auto fixed top-0 left-0 z-40 px-4 w-full h-full bg-gray-2">
6166
<div className="pb-12">
@@ -84,7 +89,9 @@ const MobileMenu: React.FC<MobileMenuProps> = ({ setShowMobileMenu, auth }) => {
8489
>
8590
{link.icon}
8691
<span className="ml-2 text-lg font-medium text-gray-11">
87-
{link.text}
92+
{link.text === "Open Source" && stars
93+
? `${stars} Stars`
94+
: link.text}
8895
</span>
8996
</Link>
9097
</li>

apps/web/utils/github.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
interface GitHubRepoResponse {
2+
stargazers_count: number;
3+
}
4+
5+
export async function getGitHubStars(): Promise<number> {
6+
const response = await fetch("https://api.github.com/repos/CapSoftware/Cap", {
7+
headers: {
8+
Accept: "application/vnd.github.v3+json",
9+
"User-Agent": "Cap-Web",
10+
},
11+
next: {
12+
revalidate: 172800,
13+
},
14+
});
15+
16+
if (!response.ok) {
17+
return 0;
18+
}
19+
20+
const data: GitHubRepoResponse = await response.json();
21+
return data.stargazers_count;
22+
}
23+
24+
export function formatStarCount(count: number): string {
25+
if (count === 0) return "";
26+
if (count >= 1000) {
27+
const formatted = (count / 1000).toFixed(1);
28+
return formatted.endsWith(".0")
29+
? `${Math.floor(count / 1000)}k`
30+
: `${formatted}k`;
31+
}
32+
return count.toString();
33+
}

0 commit comments

Comments
 (0)