"use client"; import { useState, useEffect } from 'react'; import { Star, GitFork, Eye, ExternalLink } from 'lucide-react'; import { Button } from './button'; import { cn } from '../../lib/utils'; interface GitHubStarsProps { className?: string; size?: 'sm' | 'md' | 'lg'; showForks?: boolean; showWatchers?: boolean; repoUrl?: string; } interface GitHubStats { stars: number; forks: number; watchers: number; isLive: boolean; } export function GitHubStars({ className, size = 'md', showForks = true, showWatchers = false, repoUrl = 'https://github.com/moonui/moonui' }: GitHubStarsProps) { const [stats, setStats] = useState(null); const [isLoading, setIsLoading] = useState(true); useEffect(() => { const fetchStats = async () => { try { const response = await fetch('/api/github-stars'); const data = await response.json(); setStats(data); } catch (error) { console.error('Failed to fetch GitHub stats:', error); // Set fallback data setStats({ stars: 1240, forks: 89, watchers: 156, isLive: false }); } finally { setIsLoading(false); } }; fetchStats(); }, []); const formatNumber = (num: number) => { if (num >= 1000) { return (num / 1000).toFixed(1) + 'k'; } return num.toString(); }; const sizeClasses = { sm: 'text-sm gap-2', md: 'text-base gap-3', lg: 'text-lg gap-4' }; const iconSizes = { sm: 14, md: 16, lg: 20 }; if (isLoading) { return (
{showForks && (
)}
); } if (!stats) { return null; } return (
{formatNumber(stats.stars)} {showForks && ( {formatNumber(stats.forks)} )} {showWatchers && ( {formatNumber(stats.watchers)} )} {!stats.isLive && ( (demo) )}
); } export function GitHubStarButton({ className, size = 'md', repoUrl = 'https://github.com/moonui/moonui' }: { className?: string; size?: 'sm' | 'md' | 'lg'; repoUrl?: string; }) { const [stars, setStars] = useState(null); const [isLoading, setIsLoading] = useState(true); useEffect(() => { const fetchStats = async () => { try { const response = await fetch('/api/github-stars'); const data = await response.json(); setStars(data.stars); } catch (error) { console.error('Failed to fetch GitHub stats:', error); setStars(1240); } finally { setIsLoading(false); } }; fetchStats(); }, []); const formatNumber = (num: number) => { if (num >= 1000) { return (num / 1000).toFixed(1) + 'k'; } return num.toString(); }; return ( ); }