Skip to content

Commit ab5a15f

Browse files
fetch star count from repo
1 parent 4f1aefb commit ab5a15f

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/app/_actions/github.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { cache } from "react";
2+
3+
interface GitHubResponse {
4+
stargazers_count: number;
5+
}
6+
7+
export const getStarCount = cache(async () => {
8+
try {
9+
const response = await fetch(
10+
"https://api.github.com/repos/ahmedkhaleel2004/gitdiagram",
11+
{
12+
headers: {
13+
Accept: "application/vnd.github.v3+json",
14+
},
15+
next: {
16+
revalidate: 300, // Cache for 5 minutes
17+
},
18+
},
19+
);
20+
21+
if (!response.ok) {
22+
throw new Error("Failed to fetch star count");
23+
}
24+
25+
const data = (await response.json()) as GitHubResponse;
26+
return data.stargazers_count;
27+
} catch (error) {
28+
console.error("Error fetching star count:", error);
29+
return null;
30+
}
31+
});

src/app/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Hero from "~/components/hero";
44
export default function HomePage() {
55
return (
66
<main className="flex-grow p-8">
7-
<div className="mx-auto my-4 max-w-4xl lg:my-12">
7+
<div className="mx-auto my-4 max-w-4xl lg:my-8">
88
<Hero />
99
<div className="mt-12"></div>
1010
<p className="mx-auto mt-8 max-w-2xl text-center text-lg">

src/components/header.tsx

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import React from "react";
22
import Link from "next/link";
33
import { FaGithub } from "react-icons/fa";
4+
import { getStarCount } from "~/app/_actions/github";
5+
6+
export async function Header() {
7+
const starCount = await getStarCount();
8+
9+
const formatStarCount = (count: number | null) => {
10+
if (!count) return "0";
11+
if (count >= 1000) {
12+
return `${(count / 1000).toFixed(1)}k`;
13+
}
14+
return count.toString();
15+
};
416

5-
export function Header() {
617
return (
718
<header className="border-b-[3px] border-black">
819
<div className="mx-auto flex h-16 max-w-4xl items-center justify-between px-8">
@@ -32,7 +43,7 @@ export function Header() {
3243
</Link>
3344
<span className="flex items-center gap-1 text-sm font-medium text-black">
3445
<span className="text-amber-400"></span>
35-
1.3k
46+
{formatStarCount(starCount)}
3647
</span>
3748
</nav>
3849
</div>

0 commit comments

Comments
 (0)