Skip to content

Commit 2f21766

Browse files
committed
feat: improve GitHub link component with star count display
1 parent d57a927 commit 2f21766

1 file changed

Lines changed: 48 additions & 6 deletions

File tree

apps/website/src/components/github-link.tsx

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,62 @@
1+
"use client";
2+
3+
import useSWR from "swr";
4+
5+
import { cn } from "@/utils/cn";
16
import { globals } from "@/globals";
7+
import { StarIcon } from "lucide-react";
28

39
import { GitHub } from "@/components/ui/svgs/github";
4-
import { ExternalLink } from "@/components/ui/external-link";
510
import { buttonVariants } from "@/components/ui/button";
11+
import { ExternalLink } from "@/components/ui/external-link";
12+
13+
interface GitHubRepo {
14+
stargazers_count: number;
15+
}
16+
17+
const fetcher = (url: string) => fetch(url).then((res) => res.json());
18+
19+
const formatStars = (count: number): string => {
20+
if (count >= 1000) {
21+
return `${(count / 1000).toFixed(1).replace(/\.0$/, "")}k`;
22+
}
23+
return count.toString();
24+
};
625

726
const GithubLink = () => {
27+
const repoPath = "pheralb/code-blocks";
28+
const { data } = useSWR<GitHubRepo>(
29+
`https://api.github.com/repos/${repoPath}`,
30+
fetcher,
31+
{
32+
revalidateOnFocus: false,
33+
revalidateOnReconnect: false,
34+
},
35+
);
36+
37+
const stars = data?.stargazers_count;
38+
839
return (
940
<ExternalLink
1041
title="GitHub Repository"
1142
href={globals.githubUrl}
12-
className={buttonVariants({
13-
variant: "ghost",
14-
size: "icon",
15-
})}
43+
className={cn(
44+
buttonVariants({
45+
variant: "ghost",
46+
className: "group w-auto",
47+
}),
48+
"px-2",
49+
)}
1650
>
17-
<GitHub width={18} height={18} />
51+
<div className="flex items-center gap-2">
52+
<GitHub width={18} height={18} />
53+
{data && stars !== undefined && (
54+
<div className="flex items-center gap-1 font-mono text-xs text-neutral-600 dark:text-neutral-400">
55+
<span>{formatStars(stars)}</span>
56+
<StarIcon size={12} />
57+
</div>
58+
)}
59+
</div>
1860
</ExternalLink>
1961
);
2062
};

0 commit comments

Comments
 (0)