-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55e7b20
commit b5acd2e
Showing
2 changed files
with
46 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { Icon, faGithub } from '@rivet-gg/icons'; | ||
import { cn } from '@rivet-gg/components'; | ||
|
||
interface GitHubStarsProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> { | ||
repo?: string; | ||
} | ||
|
||
function formatNumber(num: number): string { | ||
if (num >= 1000) { | ||
return `${(num / 1000).toFixed(1)}k`; | ||
} | ||
return num.toString(); | ||
} | ||
|
||
export async function GitHubStars({ repo = 'rivet-gg/rivet', className, ...props }: GitHubStarsProps) { | ||
try { | ||
const response = await fetch(`https://api.github.com/repos/${repo}`); | ||
const data = await response.json(); | ||
const { stargazers_count: stars } = data; | ||
|
||
return ( | ||
<a | ||
href={`https://github.com/${repo}`} | ||
target='_blank' | ||
rel='noreferrer' | ||
className={cn( | ||
"md:bg-white/10 rounded-md px-4 h-10 flex items-center gap-2 md:hover:bg-white/20 transition-colors", | ||
className | ||
)} | ||
{...props}> | ||
<Icon icon={faGithub} /> <span className="hidden md:inline">{stars ? `${formatNumber(stars)} Stars` : 'GitHub'}</span> | ||
</a> | ||
); | ||
} catch (err) { | ||
console.error('Failed to fetch stars', err); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters