1
1
import { useState , useEffect } from 'react' ;
2
2
import { GithubOutlined , StarFilled } from '@ant-design/icons' ;
3
- import { Tooltip , Badge } from 'antd' ;
3
+ import { Tooltip , Spin } from 'antd' ;
4
4
5
5
interface GitHubStarCounterProps {
6
6
owner : string ;
@@ -87,47 +87,69 @@ const GitHubStarCounter: React.FC<GitHubStarCounterProps> = ({
87
87
fetchStarCount ( ) ;
88
88
} , [ owner , repo , cacheTime ] ) ;
89
89
90
- // 容器样式
90
+ // 格式化星星数量,大于1000显示为1k+的形式
91
+ const formatStarCount = ( count : number ) : string => {
92
+ if ( count >= 1000 ) {
93
+ const formattedCount = ( count / 1000 ) . toFixed ( 1 ) ;
94
+ // 如果小数点后是0,则不显示小数点
95
+ return formattedCount . endsWith ( '.0' )
96
+ ? `${ formattedCount . slice ( 0 , - 2 ) } k`
97
+ : `${ formattedCount } k` ;
98
+ }
99
+ return count . toString ( ) ;
100
+ } ;
101
+
102
+ // 美化后的容器样式
91
103
const containerStyle = {
92
104
display : 'inline-flex' ,
93
105
alignItems : 'center' ,
94
106
justifyContent : 'center' ,
95
107
fontSize : '13px' ,
96
- color : '#333 ' ,
108
+ color : '#24292e ' ,
97
109
cursor : 'pointer' ,
98
- transition : 'transform 0.2s ease, color 0.2s ease ' ,
99
- height : '100% ' ,
100
- borderRadius : '4px ' ,
110
+ transition : 'all 0.3s cubic-bezier(0.4, 0, 0.2, 1) ' ,
111
+ height : '28px ' ,
112
+ borderRadius : '15px ' ,
101
113
textDecoration : 'none' ,
102
- padding : '0 8px' ,
103
- fontWeight : 600 ,
114
+ padding : '0 12px' ,
115
+ fontWeight : 500 ,
116
+ border : '1px solid rgba(36, 41, 46, 0.1)' ,
117
+ background : 'linear-gradient(to bottom, #fafbfc, #f6f8fa)' ,
118
+ boxShadow : '0 1px 2px rgba(0, 0, 0, 0.05)' ,
119
+ } ;
120
+
121
+ // 悬停样式
122
+ const hoverStyle = {
123
+ background : 'linear-gradient(to bottom, #f6f8fa, #e1e4e8)' ,
124
+ boxShadow : '0 1px 3px rgba(0, 0, 0, 0.1)' ,
125
+ transform : 'translateY(-1px)' ,
126
+ borderColor : 'rgba(36, 41, 46, 0.2)' ,
104
127
} ;
105
128
106
129
// GitHub图标样式
107
130
const githubIconStyle = {
108
- fontSize : '22px ' ,
131
+ fontSize : '16px ' ,
109
132
color : '#24292e' ,
110
- marginRight : '2px' ,
111
- verticalAlign : 'middle'
133
+ marginRight : '4px' ,
112
134
} ;
113
135
114
136
// Star图标样式
115
137
const starIconStyle = {
116
138
fontSize : '14px' ,
117
- color : '#ff9800' ,
118
- marginRight : '3px' ,
139
+ color : '#f1c40f' ,
140
+ marginRight : '4px' ,
141
+ filter : 'drop-shadow(0px 1px 1px rgba(0, 0, 0, 0.1))' ,
142
+ } ;
143
+
144
+ // 数字样式
145
+ const countStyle = {
146
+ fontWeight : 600 ,
147
+ color : '#24292e' ,
119
148
} ;
120
149
121
- // 徽章样式
122
- const badgeStyle = {
123
- backgroundColor : '#24292e' ,
124
- color : 'white' ,
125
- fontSize : '12px' ,
126
- padding : '1px 6px' ,
127
- borderRadius : '10px' ,
128
- fontWeight : 'bold' ,
129
- marginLeft : '4px' ,
130
- display : 'inline-block' ,
150
+ // 加载动画样式
151
+ const spinnerStyle = {
152
+ margin : '0 5px' ,
131
153
} ;
132
154
133
155
return (
@@ -138,19 +160,22 @@ const GitHubStarCounter: React.FC<GitHubStarCounterProps> = ({
138
160
rel = "noopener noreferrer"
139
161
style = { containerStyle }
140
162
onMouseOver = { ( e ) => {
141
- e . currentTarget . style . color = '#1890ff' ;
142
- e . currentTarget . style . transform = 'scale(1.05)' ;
163
+ Object . assign ( e . currentTarget . style , hoverStyle ) ;
143
164
} }
144
165
onMouseOut = { ( e ) => {
145
- e . currentTarget . style . color = '#333' ;
146
- e . currentTarget . style . transform = 'scale(1)' ;
166
+ // 重置为原始样式
167
+ Object . assign ( e . currentTarget . style , containerStyle ) ;
147
168
} }
148
169
>
149
170
< GithubOutlined style = { githubIconStyle } />
150
- < div style = { { display : 'flex' , alignItems : 'center' , marginLeft : '5px' } } >
151
- < StarFilled style = { starIconStyle } />
152
- < span > { loading ? '...' : starCount } </ span >
153
- </ div >
171
+ < StarFilled style = { starIconStyle } />
172
+ { loading ? (
173
+ < Spin size = "small" style = { spinnerStyle } />
174
+ ) : (
175
+ < span style = { countStyle } >
176
+ { starCount !== null ? formatStarCount ( starCount ) : '0' }
177
+ </ span >
178
+ ) }
154
179
</ a >
155
180
</ Tooltip >
156
181
) ;
0 commit comments