|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import useSWR from "swr"; |
| 4 | + |
| 5 | +import { cn } from "@/utils/cn"; |
1 | 6 | import { globals } from "@/globals"; |
| 7 | +import { StarIcon } from "lucide-react"; |
2 | 8 |
|
3 | 9 | import { GitHub } from "@/components/ui/svgs/github"; |
4 | | -import { ExternalLink } from "@/components/ui/external-link"; |
5 | 10 | 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 | +}; |
6 | 25 |
|
7 | 26 | 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 | + |
8 | 39 | return ( |
9 | 40 | <ExternalLink |
10 | 41 | title="GitHub Repository" |
11 | 42 | 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 | + )} |
16 | 50 | > |
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> |
18 | 60 | </ExternalLink> |
19 | 61 | ); |
20 | 62 | }; |
|
0 commit comments