Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions src/pages/ContributorProfile/ContributorProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,58 @@ type Profile = {
bio: string;
};

// --- FIX: Added separate error type to distinguish 404 vs other failures ---
type ErrorType = "not_found" | "api_error" | null;

export default function ContributorProfile() {
const { username } = useParams();
const [profile, setProfile] = useState<Profile | null>(null);
const [prs, setPRs] = useState<PR[]>([]);
const [loading, setLoading] = useState(true);
// --- FIX: New error state ---
const [error, setError] = useState<ErrorType>(null);

useEffect(() => {
async function fetchData() {
if (!username) return;

// --- FIX: Reset error on each fetch ---
setError(null);
Comment thread
mayurigade-hub marked this conversation as resolved.

try {
const userRes = await fetch(`https://api.github.com/users/${username}`);

if (!userRes.ok) {
if (userRes.status === 404) {
// --- FIX: 404 → "not_found", not a generic error ---
setError("not_found");
} else {
// --- FIX: Rate limit, server error, etc. → "api_error" ---
setError("api_error");
}
setProfile(null);
setPRs([]);
return;
}

const userData = await userRes.json();
setProfile(userData);

const prsRes = await fetch(
`https://api.github.com/search/issues?q=author:${username}+type:pr`
);
const prsData = await prsRes.json();
setPRs(prsData.items);
if (!prsRes.ok) {
setPRs([]);
toast.error("Failed to load pull requests.");
} else {
const prsData = await prsRes.json();
setPRs(prsData.items ?? []);
}
} catch {
// --- FIX: Network/connection failures → "api_error" ---
setError("api_error");
setProfile(null);
setPRs([]);
toast.error("Failed to fetch user data.");
} finally {
setLoading(false);
Expand All @@ -51,10 +82,24 @@ export default function ContributorProfile() {

if (loading) return <div className="text-center mt-10">Loading...</div>;

if (!profile)
// --- FIX: Show correct message based on error type ---
if (error === "not_found") {
return (
<div className="text-center mt-10 text-red-600">
User not found.
</div>
);
}

if (error === "api_error") {
return (
<div className="text-center mt-10 text-red-600">User not found.</div>
<div className="text-center mt-10 text-yellow-600">
Unable to load contributor profile. Please try again later.
</div>
);
}

if (!profile) return null;

return (
<div className="max-w-3xl mx-auto mt-2 mb-2 p-4 bg-white dark:bg-gray-800 dark:text-white shadow-xl rounded-xl">
Expand Down Expand Up @@ -98,4 +143,4 @@ export default function ContributorProfile() {
)}
</div>
);
}
}