Skip to content

Commit 0b53767

Browse files
author
pari
committed
Solved the needed changes suggested by github bot
1 parent 5e214c5 commit 0b53767

2 files changed

Lines changed: 58 additions & 9 deletions

File tree

src/components/DeveloperSummary.tsx

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,66 @@ interface DeveloperProfile {
1818

1919
interface Props {
2020
username: string;
21+
token?: string;
2122
}
2223

23-
const DeveloperSummary: React.FC<Props> = ({ username }) => {
24+
const DeveloperSummary: React.FC<Props> = ({ username, token }) => {
2425
const [profile, setProfile] =
2526
useState<DeveloperProfile | null>(null);
2627

27-
useEffect(() => {
28-
if (!username) return;
28+
useEffect(() => {
29+
if (!username.trim()) {
30+
setProfile(null);
31+
return;
32+
}
2933

30-
fetch(`https://api.github.com/users/${username}`)
31-
.then((res) => res.json())
32-
.then((data) => setProfile(data))
33-
.catch(console.error);
34-
}, [username]);
34+
const controller = new AbortController();
35+
36+
const timeout = setTimeout(async () => {
37+
try {
38+
setProfile(null);
39+
40+
const response = await fetch(
41+
`https://api.github.com/users/${username}`,
42+
{
43+
signal: controller.signal,
44+
...(token && {
45+
headers: {
46+
Authorization: `Bearer ${token}`,
47+
},
48+
}),
49+
}
50+
);
51+
52+
if (!response.ok) {
53+
const errorData =
54+
await response.json().catch(() => null);
55+
56+
throw new Error(
57+
errorData?.message ||
58+
`GitHub request failed (${response.status})`
59+
);
60+
}
61+
62+
const data = await response.json();
63+
64+
setProfile(data);
65+
} catch (error) {
66+
if (
67+
error instanceof Error &&
68+
error.name !== "AbortError"
69+
) {
70+
console.error(error);
71+
setProfile(null);
72+
}
73+
}
74+
}, 500);
75+
76+
return () => {
77+
clearTimeout(timeout);
78+
controller.abort();
79+
};
80+
}, [username, token]);
3581

3682
if (!profile) return null;
3783

src/pages/Tracker/Tracker.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,10 @@ const Home: React.FC = () => {
243243
</Box>
244244
</form>
245245
</Paper>
246-
<DeveloperSummary username={username} />
246+
<DeveloperSummary
247+
username={username}
248+
token={token}
249+
/>
247250
{/* Filters */}
248251
<Box sx={{ mb: 2, display: "flex", flexWrap: "wrap", gap: 2 }}>
249252
<TextField

0 commit comments

Comments
 (0)