From 70c826f5cc13739c7e4b2755c87e7fc275bc1d5e Mon Sep 17 00:00:00 2001 From: eshanized Date: Sat, 11 Jan 2025 22:54:32 +0530 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20perf:=20improve=20url=20fe?= =?UTF-8?q?tch=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/home/StatsSection.tsx | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/components/home/StatsSection.tsx b/src/components/home/StatsSection.tsx index 5e16293f..50cf1656 100644 --- a/src/components/home/StatsSection.tsx +++ b/src/components/home/StatsSection.tsx @@ -14,14 +14,18 @@ export function StatsSection() { { label: "Security Tools", value: "Loading...", icon: Wrench }, { label: "GitHub Stars", value: "Loading...", icon: Star }, ]); - - const [error, setError] = useState(false); + + const [loading, setLoading] = useState(true); // Loading state + const [error, setError] = useState(null); // Error message const fetchGitHubStats = async (username: string) => { try { const userResponse = await fetch(`https://api.github.com/users/${username}`); const userData = await userResponse.json(); + // Check for invalid response + if (userData.message) throw new Error("User not found"); + const repos = []; let page = 1; @@ -43,14 +47,16 @@ export function StatsSection() { { label: "Repositories", value: `${repos.length}`, icon: Wrench }, { label: "Stars", value: `${starsCount}`, icon: Star }, ]); + setLoading(false); } catch (error) { console.error("Error fetching GitHub data:", error); - setError(true); + setError("Unable to fetch GitHub data. Please try again later."); + setLoading(false); } }; useEffect(() => { - const username = "Snigdha-OS"; // Replace with the desired GitHub username + const username = "Snigdha-OS"; // Replace with desired GitHub username fetchGitHubStats(username); }, []); @@ -78,10 +84,10 @@ export function StatsSection() { {/* Stats Grid */} - {error ? ( -
- Unable to fetch data. Please try again later. -
+ {loading ? ( +
Loading data...
+ ) : error ? ( +
{error}
) : (
{stats.map((stat, index) => ( @@ -95,8 +101,8 @@ export function StatsSection() { ease: "easeOut", }} whileHover={{ - scale: 1.1, - boxShadow: "0px 20px 40px rgba(0, 0, 0, 0.2)", + scale: 1.05, + boxShadow: "0px 20px 40px rgba(0, 0, 0, 0.1)", }} className="flex flex-col items-center p-8 bg-white rounded-xl shadow-lg hover:shadow-2xl transition-transform duration-300" >