Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 9180572

Browse files
authored
fix: github star rounding (#667)
1 parent cebf5c3 commit 9180572

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/components/GitHubStarCount.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ const formatStarCount = (count: number) => {
1212
if (count >= 1000000) {
1313
return (count / 1000000).toFixed(1).replace(/\.0$/, '') + 'M'
1414
} else if (count >= 1000) {
15-
return (count / 1000).toFixed(1).replace(/\.0$/, '') + 'k'
15+
const rounded = Math.round(count / 100) / 10 // Round to the nearest decimal
16+
return rounded.toFixed(1).replace(/\.0$/, '') + 'k'
1617
}
17-
return count
18+
return count.toString()
1819
}
1920

20-
const GitHubStarCount = ({ className }: { className?: string }) => {
21-
const [starCount, setStarCount] = useState(1100)
21+
const DEFAULT_STAR_COUNT = 1150
22+
23+
const GitHubStarCount = ({ className }: { className: string }) => {
24+
const [starCount, setStarCount] = useState(DEFAULT_STAR_COUNT)
2225

2326
useEffect(() => {
2427
const fetchStarCount = async () => {
@@ -27,11 +30,12 @@ const GitHubStarCount = ({ className }: { className?: string }) => {
2730
const cachedTimestamp = localStorage.getItem(STAR_COUNT_TIMESTAMP_KEY)
2831
const currentTime = new Date().getTime()
2932

30-
// Check if cached data exists and is still valid
3133
if (
3234
cachedStarCount &&
3335
cachedTimestamp &&
34-
currentTime - parseInt(cachedTimestamp) < CACHE_DURATION
36+
currentTime - parseInt(cachedTimestamp) < CACHE_DURATION &&
37+
// Ensure cached value is at least the new default value
38+
parseInt(cachedStarCount) >= DEFAULT_STAR_COUNT
3539
) {
3640
setStarCount(JSON.parse(cachedStarCount))
3741
} else {

0 commit comments

Comments
 (0)