|
1 | 1 | import * as React from "react"; |
2 | 2 | import { Star, GitFork, GitPullRequest } from "lucide-react"; |
3 | 3 | import { cn } from "@/lib/utils"; |
| 4 | +import type { GitHubStats } from "@/lib/github"; |
4 | 5 |
|
5 | 6 | export type GitHubMetric = "stars" | "forks" | "prs"; |
6 | 7 |
|
@@ -125,3 +126,151 @@ export function GitHubCounter({ |
125 | 126 | </a> |
126 | 127 | ); |
127 | 128 | } |
| 129 | + |
| 130 | +export interface RotatingGitHubCounterProps extends Omit< |
| 131 | + React.AnchorHTMLAttributes<HTMLAnchorElement>, |
| 132 | + "children" |
| 133 | +> { |
| 134 | + stats?: GitHubStats; |
| 135 | + intervalMs?: number; |
| 136 | + className?: string; |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Navbar GitHub counter that smoothly cycles between available traction metrics: |
| 141 | + * stars → forks → PRs → stars (every 3 seconds). |
| 142 | + * |
| 143 | + * Features: |
| 144 | + * - Client component; data fetched on server and passed via props. |
| 145 | + * - Short fade/slide transition between metrics. |
| 146 | + * - Link (href) and aria-label follow the currently visible metric. |
| 147 | + * - Pauses rotation on hover and focus. |
| 148 | + * - Respects prefers-reduced-motion: no rotation, stays on stars. |
| 149 | + * - No aria-live (avoids screen reader announcement spam every 3s). |
| 150 | + * - Fixed width across metrics to prevent navbar layout shifts. |
| 151 | + * - Skips metrics with no count (< 10 or null). If none have a count, renders static stars pill. |
| 152 | + */ |
| 153 | +export function RotatingGitHubCounter({ |
| 154 | + stats, |
| 155 | + intervalMs = 3000, |
| 156 | + className, |
| 157 | + ...props |
| 158 | +}: RotatingGitHubCounterProps) { |
| 159 | + const [currentIndex, setCurrentIndex] = React.useState(0); |
| 160 | + const [isPaused, setIsPaused] = React.useState(false); |
| 161 | + const [prefersReducedMotion, setPrefersReducedMotion] = React.useState(false); |
| 162 | + |
| 163 | + React.useEffect(() => { |
| 164 | + if (typeof window === "undefined" || !window.matchMedia) return; |
| 165 | + const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)"); |
| 166 | + setPrefersReducedMotion(mediaQuery.matches); |
| 167 | + |
| 168 | + const handleChange = (e: MediaQueryListEvent) => { |
| 169 | + setPrefersReducedMotion(e.matches); |
| 170 | + }; |
| 171 | + |
| 172 | + if (mediaQuery.addEventListener) { |
| 173 | + mediaQuery.addEventListener("change", handleChange); |
| 174 | + return () => mediaQuery.removeEventListener("change", handleChange); |
| 175 | + } else if (mediaQuery.addListener) { |
| 176 | + mediaQuery.addListener(handleChange); |
| 177 | + return () => mediaQuery.removeListener(handleChange); |
| 178 | + } |
| 179 | + }, []); |
| 180 | + |
| 181 | + // Filter metrics that have a valid formatted count (>= 10) |
| 182 | + // Preserves strict cycle order: stars -> forks -> prs |
| 183 | + const availableMetrics = React.useMemo(() => { |
| 184 | + const list: GitHubMetric[] = []; |
| 185 | + if (formatGitHubCount(stats?.stars) !== null) list.push("stars"); |
| 186 | + if (formatGitHubCount(stats?.forks) !== null) list.push("forks"); |
| 187 | + if (formatGitHubCount(stats?.prs) !== null) list.push("prs"); |
| 188 | + return list; |
| 189 | + }, [stats?.stars, stats?.forks, stats?.prs]); |
| 190 | + |
| 191 | + const hasValidCounts = availableMetrics.length > 0; |
| 192 | + |
| 193 | + // Rotation timer: cycle every 3s, pause on hover/focus, no rotation on reduced motion |
| 194 | + React.useEffect(() => { |
| 195 | + if (prefersReducedMotion || isPaused || availableMetrics.length <= 1) { |
| 196 | + return; |
| 197 | + } |
| 198 | + |
| 199 | + const timer = setInterval(() => { |
| 200 | + setCurrentIndex((prev) => (prev + 1) % availableMetrics.length); |
| 201 | + }, intervalMs); |
| 202 | + |
| 203 | + return () => clearInterval(timer); |
| 204 | + }, [prefersReducedMotion, isPaused, availableMetrics.length, intervalMs]); |
| 205 | + |
| 206 | + // If none have a count, show the static stars pill |
| 207 | + if (!hasValidCounts) { |
| 208 | + return <GitHubCounter metric="stars" count={null} className={className} {...props} />; |
| 209 | + } |
| 210 | + |
| 211 | + // Active metric: force stars if prefers-reduced-motion is true |
| 212 | + const activeMetric: GitHubMetric = prefersReducedMotion |
| 213 | + ? "stars" |
| 214 | + : availableMetrics[currentIndex % availableMetrics.length]; |
| 215 | + |
| 216 | + const config = metricConfig[activeMetric] ?? metricConfig.stars; |
| 217 | + const rawCount = stats?.[activeMetric] ?? null; |
| 218 | + const formattedCount = formatGitHubCount(rawCount); |
| 219 | + const MetricIcon = config.icon; |
| 220 | + |
| 221 | + const ariaLabel = formattedCount |
| 222 | + ? `${config.label} (${formattedCount} ${config.noun})` |
| 223 | + : config.label; |
| 224 | + |
| 225 | + return ( |
| 226 | + <a |
| 227 | + href={props.href ?? config.href} |
| 228 | + target="_blank" |
| 229 | + rel="noopener noreferrer" |
| 230 | + aria-label={ariaLabel} |
| 231 | + onMouseEnter={() => setIsPaused(true)} |
| 232 | + onMouseLeave={() => setIsPaused(false)} |
| 233 | + onFocus={() => setIsPaused(true)} |
| 234 | + onBlur={() => setIsPaused(false)} |
| 235 | + className={cn( |
| 236 | + "group inline-flex h-9 w-[136px] items-center justify-between 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", |
| 237 | + className, |
| 238 | + )} |
| 239 | + {...props} |
| 240 | + > |
| 241 | + {/* Left: GitHub logo */} |
| 242 | + <span className="flex items-center justify-center pl-3 pr-2.5"> |
| 243 | + <svg |
| 244 | + width="16" |
| 245 | + height="16" |
| 246 | + viewBox="0 0 24 24" |
| 247 | + fill="currentColor" |
| 248 | + aria-hidden="true" |
| 249 | + className="size-4 text-white/80 transition-colors group-hover:text-white" |
| 250 | + > |
| 251 | + <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" /> |
| 252 | + </svg> |
| 253 | + </span> |
| 254 | + |
| 255 | + {/* 1px vertical divider */} |
| 256 | + <span className="h-3.5 w-px bg-white/[0.1]" aria-hidden="true" /> |
| 257 | + |
| 258 | + {/* Right: Count and metric icon with fade/slide transition */} |
| 259 | + <span className="flex flex-1 items-center justify-center gap-1.5 pl-2 pr-3 overflow-hidden"> |
| 260 | + <span |
| 261 | + key={activeMetric} |
| 262 | + className="inline-flex items-center gap-1.5 transition-all duration-300 motion-safe:animate-in motion-safe:fade-in motion-safe:slide-in-from-bottom-1" |
| 263 | + > |
| 264 | + {formattedCount && ( |
| 265 | + <span className="text-xs font-medium sm:text-sm">{formattedCount}</span> |
| 266 | + )} |
| 267 | + <MetricIcon |
| 268 | + size={14} |
| 269 | + className="text-white/70 transition-colors group-hover:text-white" |
| 270 | + aria-hidden="true" |
| 271 | + /> |
| 272 | + </span> |
| 273 | + </span> |
| 274 | + </a> |
| 275 | + ); |
| 276 | +} |
0 commit comments