Skip to content

Create professional demos and 3D mockups in seconds, directly in your browser

feat: enhance GitHubStars component with error handling and authorization headers

CristianOlivera1f6b6fd2c73cb
diff --git a/.env.example b/.env.example
index 31656b5..df21db0 100644
--- a/.env.example
+++ b/.env.example
@@ -2,6 +2,10 @@ UNSPLASH_ACCESS_KEY=x.y.z
UNSPLASH_SECRET_KEY=x.y.z
PEXELS_API_KEY=x.y.z
PIXABAY_API_KEY=x.y.z
+NEXT_PUBLIC_SUPABASE_URL=x.y.z
+NEXT_PUBLIC_SUPABASE_ANON_KEY=x.y.z
+NEXT_PUBLIC_SITE_URL=https://openvid.dev
+NEXT_PUBLIC_GITHUB_TOKEN=x.y.z
# Supabase Configuration
NEXT_PUBLIC_SUPABASE_URL=x.y.z
diff --git a/components/ui/GitHubStars.tsx b/components/ui/GitHubStars.tsx
index 0d7476c..3a0eab5 100644
--- a/components/ui/GitHubStars.tsx
+++ b/components/ui/GitHubStars.tsx
@@ -1,3 +1,5 @@
+"use client";
+
import { useEffect, useState } from "react";
import { Icon } from "@iconify/react";
@@ -5,10 +7,21 @@ export default function GitHubBadge() {
const [stars, setStars] = useState<number | null>(null);
useEffect(() => {
- fetch("https://api.github.com/repos/CristianOlivera1/openvid")
- .then((res) => res.json())
- .then((data) => setStars(data.stargazers_count))
- .catch(() => setStars(null));
+ fetch("https://api.github.com/repos/CristianOlivera1/openvid", {
+ headers: {
+ "Authorization": `Bearer ${process.env.NEXT_PUBLIC_GITHUB_TOKEN}`,
+ "Accept": "application/vnd.github+json"
+ }
+ })
+ .then((res) => {
+ if (!res.ok) throw new Error("GitHub Error");
+ return res.json();
+ })
+ .then((data) => setStars(data?.stargazers_count ?? null))
+ .catch((err) => {
+ console.warn("[GitHubBadge] Failed to fetch stars:", err);
+ setStars(null);
+ });
}, []);
const formatStars = (count: number | null): string => {
@@ -16,7 +29,7 @@ export default function GitHubBadge() {
if (count >= 1000) {
return `${(count / 1000).toFixed(1)}K`;
}
- return count.toString();
+ return count !== undefined ? count.toString() : "0";
};
return (
@@ -25,7 +38,7 @@ export default function GitHubBadge() {
target="_blank"
rel="noopener noreferrer"
className="relative inline-flex items-center gap-2 px-3 py-1 sm:py-1.5 rounded-full bg-linear-to-r from-yellow-400 via-amber-500 to-yellow-600 text-black font-semibold text-sm shadow-xl hover:scale-105 active:scale-95 transition-all duration-200 border border-white/20 select-none whitespace-nowrap"
- aria-label={`Repositorio de GitHub, ${stars ?? ""} estrellas`}
+ aria-label={`GitHub repository, ${stars ?? ""} stars`}
>
<div
className="absolute -top-px left-4 right-4 h-px bg-linear-to-r from-transparent via-white/90 to-transparent z-10"
@@ -43,4 +56,4 @@ export default function GitHubBadge() {
</div>
</a>
);
-}
\ No newline at end of file
+}