|
| 1 | +import * as React from "react"; |
| 2 | +import { Star, GitFork, GitPullRequest } from "lucide-react"; |
| 3 | +import { cn } from "@/lib/utils"; |
| 4 | + |
| 5 | +export type GitHubMetric = "stars" | "forks" | "prs"; |
| 6 | + |
| 7 | +export interface GitHubCounterProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> { |
| 8 | + metric?: GitHubMetric; |
| 9 | + count?: number | null; |
| 10 | + hideCountOnMobile?: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Format a raw GitHub count into a friendly bucket followed by `+`. |
| 15 | + * - Numbers below 10 (or missing/0) return `null` so no count is shown. |
| 16 | + * - 10-99 round down to the nearest 10 (e.g. 10+, 40+). |
| 17 | + * - 100-999 round down to the nearest 50 (e.g. 150+, 500+, 950+). |
| 18 | + * - 1000+ use Intl.NumberFormat compact notation rounded down to 1 decimal place (e.g. 1k+, 1.2k+, 12.5k+). |
| 19 | + */ |
| 20 | +export function formatGitHubCount(count?: number | null): string | null { |
| 21 | + if (count == null || count < 10) { |
| 22 | + return null; |
| 23 | + } |
| 24 | + if (count < 100) { |
| 25 | + const bucket = Math.floor(count / 10) * 10; |
| 26 | + return `${bucket}+`; |
| 27 | + } |
| 28 | + if (count < 1000) { |
| 29 | + const bucket = Math.floor(count / 50) * 50; |
| 30 | + return `${bucket}+`; |
| 31 | + } |
| 32 | + const rounded = Math.floor(count / 100) * 100; |
| 33 | + const formatted = new Intl.NumberFormat("en", { |
| 34 | + notation: "compact", |
| 35 | + maximumFractionDigits: 1, |
| 36 | + }) |
| 37 | + .format(rounded) |
| 38 | + .toLowerCase(); |
| 39 | + return `${formatted}+`; |
| 40 | +} |
| 41 | + |
| 42 | +const metricConfig = { |
| 43 | + stars: { |
| 44 | + label: "Star babelize-elements on GitHub", |
| 45 | + noun: "stars", |
| 46 | + href: "https://github.com/babelize/babelize-elements", |
| 47 | + icon: Star, |
| 48 | + }, |
| 49 | + forks: { |
| 50 | + label: "Fork babelize-elements on GitHub", |
| 51 | + noun: "forks", |
| 52 | + href: "https://github.com/babelize/babelize-elements/forks", |
| 53 | + icon: GitFork, |
| 54 | + }, |
| 55 | + prs: { |
| 56 | + label: "Pull requests for babelize-elements on GitHub", |
| 57 | + noun: "PRs", |
| 58 | + href: "https://github.com/babelize/babelize-elements/pulls", |
| 59 | + icon: GitPullRequest, |
| 60 | + }, |
| 61 | +} as const; |
| 62 | + |
| 63 | +export function GitHubCounter({ |
| 64 | + metric = "stars", |
| 65 | + count, |
| 66 | + hideCountOnMobile = false, |
| 67 | + className, |
| 68 | + ...props |
| 69 | +}: GitHubCounterProps) { |
| 70 | + const config = metricConfig[metric] ?? metricConfig.stars; |
| 71 | + const formattedCount = formatGitHubCount(count); |
| 72 | + const MetricIcon = config.icon; |
| 73 | + |
| 74 | + const ariaLabel = formattedCount |
| 75 | + ? `${config.label} (${formattedCount} ${config.noun})` |
| 76 | + : config.label; |
| 77 | + |
| 78 | + return ( |
| 79 | + <a |
| 80 | + href={props.href ?? config.href} |
| 81 | + target="_blank" |
| 82 | + rel="noopener noreferrer" |
| 83 | + aria-label={ariaLabel} |
| 84 | + className={cn( |
| 85 | + "group inline-flex h-9 items-center rounded-full border border-white/[0.1] bg-white/[0.03] text-sm text-white/80 transition-colors hover:bg-white/5 hover:text-white", |
| 86 | + className, |
| 87 | + )} |
| 88 | + {...props} |
| 89 | + > |
| 90 | + {/* Left: GitHub logo */} |
| 91 | + <span className="flex items-center justify-center pl-3 pr-2.5"> |
| 92 | + <svg |
| 93 | + width="16" |
| 94 | + height="16" |
| 95 | + viewBox="0 0 24 24" |
| 96 | + fill="currentColor" |
| 97 | + aria-hidden="true" |
| 98 | + className="size-4 text-white/80 transition-colors group-hover:text-white" |
| 99 | + > |
| 100 | + <path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12" /> |
| 101 | + </svg> |
| 102 | + </span> |
| 103 | + |
| 104 | + {/* 1px vertical divider */} |
| 105 | + <span className="h-3.5 w-px bg-white/[0.1]" aria-hidden="true" /> |
| 106 | + |
| 107 | + {/* Right: Count and metric icon */} |
| 108 | + <span className="flex items-center gap-1.5 pl-2.5 pr-3"> |
| 109 | + {formattedCount && ( |
| 110 | + <span |
| 111 | + className={cn( |
| 112 | + "text-xs font-medium sm:text-sm", |
| 113 | + hideCountOnMobile && "hidden sm:inline", |
| 114 | + )} |
| 115 | + > |
| 116 | + {formattedCount} |
| 117 | + </span> |
| 118 | + )} |
| 119 | + <MetricIcon |
| 120 | + size={14} |
| 121 | + className="text-white/70 transition-colors group-hover:text-white" |
| 122 | + aria-hidden="true" |
| 123 | + /> |
| 124 | + </span> |
| 125 | + </a> |
| 126 | + ); |
| 127 | +} |
0 commit comments