-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgithub-link.tsx
More file actions
62 lines (52 loc) · 1.44 KB
/
github-link.tsx
File metadata and controls
62 lines (52 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"use client";
import useSWR from "swr";
import { cn } from "@/utils/cn";
import { globals } from "@/globals";
import { GitHub } from "@/components/ui/svgs/github";
import { buttonVariants } from "@/components/ui/button";
import { ExternalLink } from "@/components/ui/external-link";
interface GitHubRepo {
stargazers_count: number;
}
const fetcher = (url: string) => fetch(url).then((res) => res.json());
const formatStars = (count: number): string => {
if (count >= 1000) {
return `${(count / 1000).toFixed(1).replace(/\.0$/, "")}k`;
}
return count.toString();
};
const GithubLink = () => {
const repoPath = "pheralb/code-blocks";
const { data } = useSWR<GitHubRepo>(
`https://api.github.com/repos/${repoPath}`,
fetcher,
{
revalidateOnFocus: false,
revalidateOnReconnect: false,
},
);
const stars = data?.stargazers_count;
return (
<ExternalLink
title="GitHub Repository"
href={globals.githubUrl}
className={cn(
buttonVariants({
variant: "ghost",
className: "group w-auto",
}),
"px-2",
)}
>
<div className="flex items-center gap-2">
<GitHub width={18} height={18} />
{data && stars !== undefined && (
<span className="font-mono text-xs text-neutral-600 dark:text-neutral-400">
{formatStars(stars)}
</span>
)}
</div>
</ExternalLink>
);
};
export default GithubLink;