Skip to content

Commit 52ad6ed

Browse files
committed
Redesign GitHub button
1 parent e36eb11 commit 52ad6ed

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed
Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
1+
import { useEffect, useState } from "react";
2+
3+
import { Button } from "../ui/button";
14
import { GithubButton } from "../ui/github-button";
5+
import { AppLoading } from "./AppLoading";
6+
import { Github } from "lucide-react";
27

38
export function AppGitHubButton() {
4-
return <GithubButton />;
9+
const [stars, setStars] = useState<number | null>(null);
10+
const [error, setError] = useState<string | null>(null);
11+
12+
// Fetch repo data from GitHub API.
13+
useEffect(() => {
14+
async function fetchStars() {
15+
try {
16+
const response = await fetch(
17+
"https://api.github.com/repos/LibreSplit/LibreSplit",
18+
{
19+
headers: { Accept: "application/vnd.github+json" },
20+
},
21+
);
22+
23+
if (!response.ok)
24+
throw new Error(`GitHub API error: ${response.status}`);
25+
26+
const data = await response.json();
27+
setStars(data.stargazers_count);
28+
} catch (err) {
29+
setError((err as Error).message);
30+
}
31+
}
32+
33+
fetchStars();
34+
}, []);
35+
36+
// Show whilst API request loads.
37+
if (stars == null) {
38+
return <AppLoading />;
39+
}
40+
41+
// Show in the case of an error.
42+
if (error) {
43+
return (
44+
<Button>
45+
<a href="https://github.com/LibreSplit/LibreSplit">
46+
<Github />
47+
</a>
48+
</Button>
49+
);
50+
}
51+
52+
// Show full component.
53+
return (
54+
<GithubButton
55+
repoUrl="https://github.com/LibreSplit/LibreSplit"
56+
label="GitHub"
57+
initialStars={0}
58+
targetStars={stars}
59+
/>
60+
);
561
}

0 commit comments

Comments
 (0)