Skip to content

Commit 4ada0e0

Browse files
authored
Add reusable star count component with abbreviated default to 1.1k (#159)
Co-authored-by: Chris Tate <ctate@users.noreply.github.com>
1 parent 0121b39 commit 4ada0e0

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

components/github-stars-button.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
import { Button } from '@/components/ui/button'
44
import { GitHubIcon } from '@/components/icons/github-icon'
5+
import { formatAbbreviatedNumber } from '@/lib/utils/format-number'
56

67
const GITHUB_REPO_URL = 'https://github.com/vercel-labs/coding-agent-template'
78

89
interface GitHubStarsButtonProps {
910
initialStars?: number
1011
}
1112

12-
export function GitHubStarsButton({ initialStars = 1056 }: GitHubStarsButtonProps) {
13+
export function GitHubStarsButton({ initialStars = 1100 }: GitHubStarsButtonProps) {
1314
return (
1415
<Button asChild variant="ghost" size="sm" className="h-8 px-2 sm:px-3 gap-1.5">
1516
<a href={GITHUB_REPO_URL} target="_blank" rel="noopener noreferrer" className="flex items-center">
1617
<GitHubIcon className="h-3.5 w-3.5" />
17-
<span className="text-sm">{initialStars.toLocaleString()}</span>
18+
<span className="text-sm">{formatAbbreviatedNumber(initialStars)}</span>
1819
</a>
1920
</Button>
2021
)

lib/utils/format-number.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Formats a number into an abbreviated form (e.g., 1.1k, 2.5M)
3+
*
4+
* @param num - The number to format
5+
* @returns The formatted string
6+
*
7+
* @example
8+
* formatAbbreviatedNumber(1109) // "1.1k"
9+
* formatAbbreviatedNumber(1500) // "1.5k"
10+
* formatAbbreviatedNumber(1000000) // "1M"
11+
* formatAbbreviatedNumber(500) // "500"
12+
*/
13+
export function formatAbbreviatedNumber(num: number): string {
14+
if (num >= 1000000) {
15+
const formatted = (num / 1000000).toFixed(1)
16+
// Remove .0 if present
17+
return formatted.endsWith('.0') ? `${Math.floor(num / 1000000)}M` : `${formatted}M`
18+
}
19+
20+
if (num >= 1000) {
21+
const formatted = (num / 1000).toFixed(1)
22+
// Remove .0 if present
23+
return formatted.endsWith('.0') ? `${Math.floor(num / 1000)}k` : `${formatted}k`
24+
}
25+
26+
return num.toString()
27+
}

0 commit comments

Comments
 (0)