1
+ import React , { useEffect , useState } from 'react' ;
2
+ import styled from 'styled-components' ;
3
+ import { getRepositoryStarCount } from '../../services/github' ;
4
+
5
+ const StarButtonContainer = styled . a `
6
+ display: inline-flex;
7
+ align-items: center;
8
+ justify-content: center;
9
+ margin-left: 1.5rem;
10
+ padding: 0.5rem 1.25rem;
11
+ background-color: ${ ( { theme } ) => theme . colors . primary } ;
12
+ color: white;
13
+ border-radius: ${ ( { theme } ) => theme . borderRadius . medium } ;
14
+ font-weight: 600;
15
+ transition: all 0.2s;
16
+
17
+ svg {
18
+ margin-right: 0.5rem;
19
+ }
20
+
21
+ &:hover {
22
+ background-color: ${ ( { theme } ) => theme . colors . primaryDark } ;
23
+ transform: translateY(-2px);
24
+ }
25
+ ` ;
26
+
27
+ const StarCount = styled . span `
28
+ display: inline-flex;
29
+ align-items: center;
30
+ margin-left: 0.5rem;
31
+
32
+ svg {
33
+ margin-right: 0.25rem;
34
+ }
35
+ ` ;
36
+
37
+ interface GitHubStarButtonProps {
38
+ repoOwner : string ;
39
+ repoName : string ;
40
+ }
41
+
42
+ const GitHubStarButton : React . FC < GitHubStarButtonProps > = ( { repoOwner, repoName } ) => {
43
+ const [ starCount , setStarCount ] = useState < number | null > ( null ) ;
44
+ const repoUrl = `https://github.com/${ repoOwner } /${ repoName } ` ;
45
+
46
+ useEffect ( ( ) => {
47
+ const fetchStarCount = async ( ) => {
48
+ try {
49
+ const count = await getRepositoryStarCount ( repoOwner , repoName ) ;
50
+ setStarCount ( count ) ;
51
+ } catch ( error ) {
52
+ console . error ( '获取Star数量失败:' , error ) ;
53
+ }
54
+ } ;
55
+
56
+ fetchStarCount ( ) ;
57
+ } , [ repoOwner , repoName ] ) ;
58
+
59
+ // 格式化星星数量,超过1000用k表示
60
+ const formatStarCount = ( count : number ) : string => {
61
+ if ( count >= 1000 ) {
62
+ return `${ ( count / 1000 ) . toFixed ( 1 ) } k` ;
63
+ }
64
+ return count . toString ( ) ;
65
+ } ;
66
+
67
+ return (
68
+ < StarButtonContainer href = { repoUrl } target = "_blank" >
69
+ < svg width = "20" height = "20" viewBox = "0 0 24 24" fill = "none" xmlns = "http://www.w3.org/2000/svg" >
70
+ < path d = "M12 2C6.477 2 2 6.477 2 12C2 16.419 4.865 20.166 8.839 21.489C9.339 21.581 9.5 21.27 9.5 21.006V19.505C6.735 20.126 6.151 18.141 6.151 18.141C5.681 17.021 5.031 16.711 5.031 16.711C4.151 16.105 5.101 16.117 5.101 16.117C6.101 16.19 6.651 17.141 6.651 17.141C7.551 18.542 9.101 18.19 9.541 17.939C9.631 17.293 9.901 16.839 10.201 16.591C7.975 16.339 5.651 15.486 5.651 11.677C5.651 10.55 6.051 9.631 6.676 8.901C6.576 8.651 6.226 7.626 6.776 6.227C6.776 6.227 7.626 5.961 9.501 7.281C10.301 7.061 11.151 6.951 12.001 6.949C12.851 6.949 13.701 7.061 14.501 7.281C16.376 5.959 17.226 6.227 17.226 6.227C17.776 7.626 17.426 8.651 17.326 8.901C17.952 9.631 18.347 10.55 18.347 11.677C18.347 15.496 16.018 16.335 13.786 16.581C14.151 16.886 14.486 17.491 14.486 18.421V21.006C14.486 21.271 14.646 21.586 15.156 21.486C19.135 20.162 21.996 16.417 21.996 12C21.996 6.477 17.52 2 12 2Z" fill = "currentColor" />
71
+ </ svg >
72
+ GitHub
73
+ { starCount !== null && (
74
+ < StarCount >
75
+ < svg width = "16" height = "16" viewBox = "0 0 24 24" fill = "none" xmlns = "http://www.w3.org/2000/svg" >
76
+ < path d = "M12 2L15.09 8.26L22 9.27L17 14.14L18.18 21.02L12 17.77L5.82 21.02L7 14.14L2 9.27L8.91 8.26L12 2Z" fill = "currentColor" />
77
+ </ svg >
78
+ { formatStarCount ( starCount ) }
79
+ </ StarCount >
80
+ ) }
81
+ </ StarButtonContainer >
82
+ ) ;
83
+ } ;
84
+
85
+ export default GitHubStarButton ;
0 commit comments