-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.tsx
More file actions
62 lines (57 loc) · 1.75 KB
/
Copy pathindex.tsx
File metadata and controls
62 lines (57 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import React, { useState, useEffect } from 'react';
import { FaGithub } from 'react-icons/fa';
import styles from './styles.module.css';
function formatStars(count: number | null): string {
if (count === null || isNaN(count)) return '0';
if (count < 1000) return count.toLocaleString();
if (count < 10000) {
const rounded = Math.round(count / 100) / 10;
return `${rounded.toFixed(1)}k`;
}
return `${Math.round(count / 1000)}k`;
}
export default function GitHubStar(): React.ReactElement {
const [stars, setStars] = useState<number | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
async function fetchStars() {
try {
const response = await fetch('https://api.github.com/repos/vectorlessflow/vectorless');
const data = await response.json();
setStars(data.stargazers_count);
} catch (error) {
console.error('Error fetching GitHub stars:', error);
} finally {
setLoading(false);
}
}
fetchStars();
}, []);
return (
<div className={styles.githubStarContainer}>
<a
href="https://github.com/vectorlessflow/vectorless"
target="_blank"
rel="noopener noreferrer"
className={styles.githubStarButton}
>
<FaGithub size={16} />
<span className={styles.githubStarText}>Star</span>
</a>
{loading ? (
<div className={styles.githubStarCount}>
<span className={styles.spinner}>…</span>
</div>
) : (
<a
href="https://github.com/vectorlessflow/vectorless"
target="_blank"
rel="noopener noreferrer"
className={styles.githubStarCount}
>
{formatStars(stars)}
</a>
)}
</div>
);
}