|
| 1 | +import AutoAwesomeIcon from "@mui/icons-material/AutoAwesome"; |
| 2 | +import StarBorderRoundedIcon from "@mui/icons-material/StarBorderRounded"; |
| 3 | +import StarRoundedIcon from "@mui/icons-material/StarRounded"; |
| 4 | +import { Box, type SxProps, type Theme } from "@mui/material"; |
| 5 | +import { openUrl } from "@tauri-apps/plugin-opener"; |
| 6 | +import type React from "react"; |
| 7 | + |
| 8 | +interface GitHubStarBadgeProps { |
| 9 | + count: number; |
| 10 | + url: string; |
| 11 | +} |
| 12 | + |
| 13 | +type TierStyle = { |
| 14 | + icon: React.ReactElement; |
| 15 | + containerSx: SxProps<Theme>; |
| 16 | + textSx: SxProps<Theme>; |
| 17 | + iconSx: SxProps<Theme>; |
| 18 | +}; |
| 19 | + |
| 20 | +const getTierStyle = (count: number): TierStyle => { |
| 21 | + // Legendary (> 6k) |
| 22 | + if (count > 6000) { |
| 23 | + return { |
| 24 | + icon: <AutoAwesomeIcon />, |
| 25 | + containerSx: { |
| 26 | + border: "1px solid rgba(255, 215, 0, 0.5)", |
| 27 | + background: |
| 28 | + "linear-gradient(90deg, rgba(255,215,0,0.1), rgba(255,69,0,0.15))", |
| 29 | + animation: "pulse 2s ease-in-out infinite", |
| 30 | + "@keyframes pulse": { |
| 31 | + "0%, 100%": { |
| 32 | + transform: "scale(1)", |
| 33 | + }, |
| 34 | + "50%": { |
| 35 | + transform: "scale(1.05)", |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + textSx: { |
| 40 | + background: "linear-gradient(135deg, #FFD700 0%, #FF4500 100%)", |
| 41 | + WebkitBackgroundClip: "text", |
| 42 | + WebkitTextFillColor: "transparent", |
| 43 | + backgroundClip: "text", |
| 44 | + }, |
| 45 | + iconSx: { |
| 46 | + background: "linear-gradient(135deg, #FFD700 0%, #FF4500 100%)", |
| 47 | + WebkitBackgroundClip: "text", |
| 48 | + WebkitTextFillColor: "transparent", |
| 49 | + backgroundClip: "text", |
| 50 | + }, |
| 51 | + }; |
| 52 | + } |
| 53 | + |
| 54 | + // Epic (1k - 6k) |
| 55 | + if (count >= 1000) { |
| 56 | + return { |
| 57 | + icon: <StarRoundedIcon />, |
| 58 | + containerSx: { |
| 59 | + border: "1px solid #FFB11B", |
| 60 | + bgcolor: "rgba(255, 177, 27, 0.15)", |
| 61 | + boxShadow: "0 0 12px rgba(255, 177, 27, 0.4)", |
| 62 | + }, |
| 63 | + textSx: { |
| 64 | + color: "#FFB11B", |
| 65 | + }, |
| 66 | + iconSx: { |
| 67 | + color: "#FFB11B", |
| 68 | + }, |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + // Solid (100 - 1k) |
| 73 | + if (count >= 100) { |
| 74 | + return { |
| 75 | + icon: <StarRoundedIcon />, |
| 76 | + containerSx: { |
| 77 | + border: "1px solid rgba(246, 211, 45, 0.3)", |
| 78 | + bgcolor: "rgba(246, 211, 45, 0.1)", |
| 79 | + }, |
| 80 | + textSx: { |
| 81 | + color: "#E6EDF3", |
| 82 | + }, |
| 83 | + iconSx: { |
| 84 | + color: "#F6D32D", |
| 85 | + }, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + // Indie (< 100) |
| 90 | + return { |
| 91 | + icon: <StarBorderRoundedIcon />, |
| 92 | + containerSx: { |
| 93 | + border: "1px solid rgba(255, 255, 255, 0.1)", |
| 94 | + bgcolor: "transparent", |
| 95 | + }, |
| 96 | + textSx: { |
| 97 | + color: "#8B949E", |
| 98 | + }, |
| 99 | + iconSx: { |
| 100 | + color: "#8B949E", |
| 101 | + }, |
| 102 | + }; |
| 103 | +}; |
| 104 | + |
| 105 | +export const GitHubStarBadge = ({ count, url }: GitHubStarBadgeProps) => { |
| 106 | + const formatStars = (starCount: number): string => { |
| 107 | + if (starCount >= 1000) { |
| 108 | + return `${(starCount / 1000).toFixed(1)}k`; |
| 109 | + } |
| 110 | + return starCount.toString(); |
| 111 | + }; |
| 112 | + |
| 113 | + const handleClick = async () => { |
| 114 | + try { |
| 115 | + await openUrl(url); |
| 116 | + } catch (error) { |
| 117 | + console.error("Error opening repository URL:", error); |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + const tierStyle = getTierStyle(count); |
| 122 | + |
| 123 | + return ( |
| 124 | + <Box |
| 125 | + onClick={handleClick} |
| 126 | + sx={{ |
| 127 | + display: "inline-flex", |
| 128 | + alignItems: "center", |
| 129 | + gap: 0.75, |
| 130 | + px: 1.5, |
| 131 | + py: 0.5, |
| 132 | + borderRadius: 1.5, |
| 133 | + cursor: "pointer", |
| 134 | + transition: "all 0.3s ease", |
| 135 | + fontFamily: "JetBrains Mono, monospace", |
| 136 | + fontWeight: 700, |
| 137 | + fontSize: "0.875rem", |
| 138 | + "&:hover": { |
| 139 | + transform: "translateY(-2px)", |
| 140 | + opacity: 0.9, |
| 141 | + }, |
| 142 | + ...tierStyle.containerSx, |
| 143 | + }} |
| 144 | + > |
| 145 | + <Box |
| 146 | + component="span" |
| 147 | + sx={{ |
| 148 | + display: "flex", |
| 149 | + alignItems: "center", |
| 150 | + fontSize: "1rem", |
| 151 | + ...tierStyle.iconSx, |
| 152 | + }} |
| 153 | + > |
| 154 | + {tierStyle.icon} |
| 155 | + </Box> |
| 156 | + <Box component="span" sx={tierStyle.textSx}> |
| 157 | + {formatStars(count)} |
| 158 | + </Box> |
| 159 | + </Box> |
| 160 | + ); |
| 161 | +}; |
0 commit comments