-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathGitHubAnalyzer.jsx
More file actions
357 lines (329 loc) Β· 12 KB
/
GitHubAnalyzer.jsx
File metadata and controls
357 lines (329 loc) Β· 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* GITHUB ANALYZER DASHBOARD TODOs
* -------------------------------
* Easy:
* - [ ] Add input validation for username
* - [ ] Show loading skeleton for profile data
* - [ ] Add error handling for invalid usernames
* - [ ] Display user avatar and basic info
* Medium:
* - [ ] Implement advanced stats (languages, stars, forks)
* - [ ] Add repository list with pagination
* - [ ] Show contribution graph/calendar
* - [ ] Add profile comparison feature
* Advanced:
* - [ ] Add GitHub API rate limit handling
* - [ ] Implement caching for API responses
* - [ ] Add export functionality (PDF/JSON)
* - [ ] Add analytics and insights
*/
import { useState, useEffect } from "react";
import Loading from "../components/Loading.jsx";
import ErrorMessage from "../components/ErrorMessage.jsx";
import Card from "../components/Card.jsx";
import HeroSection from "../components/HeroSection.jsx";
import GitHubImg from "../Images/GitHub.jpg"; // Assuming you have a GitHub image
export default function GitHubAnalyzer() {
const [username, setUsername] = useState("");
const [profile, setProfile] = useState(null);
const [repos, setRepos] = useState([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [compareUsername, setCompareUsername] = useState("");
const [compareProfile, setCompareProfile] = useState(null);
const [compareRepos, setCompareRepos] = useState([]);
useEffect(() => {
// Load from localStorage if available
const savedUsername = localStorage.getItem("githubUsername");
console.log('Loading from localStorage:', savedUsername);
if (savedUsername) {
setUsername(savedUsername);
fetchProfile(savedUsername);
}
}, []);
async function fetchProfile(user) {
console.log('fetchProfile called with user:', user);
if (!user.trim()) {
console.log('User is empty, returning');
return;
}
try {
setLoading(true);
setError(null);
console.log('Starting fetch for profile');
// Fetch user profile
const profileRes = await fetch(`https://api.github.com/users/${user}`);
console.log('Profile response status:', profileRes.status);
if (!profileRes.ok) {
if (profileRes.status === 404) {
throw new Error("User not found");
}
throw new Error("Failed to fetch profile");
}
const profileData = await profileRes.json();
console.log('Profile data received:', profileData);
setProfile(profileData);
localStorage.setItem("githubUsername", user);
// Fetch repositories
const reposRes = await fetch(
`https://api.github.com/users/${user}/repos?sort=updated&per_page=100`
);
console.log('Repos response status:', reposRes.status);
if (reposRes.ok) {
const reposData = await reposRes.json();
console.log('Repos data received, count:', reposData.length);
setRepos(reposData);
} else {
console.log('Failed to fetch repos');
}
} catch (e) {
console.log('Error in fetchProfile:', e);
setError(e.message);
setProfile(null);
setRepos([]);
} finally {
setLoading(false);
console.log('fetchProfile completed');
}
}
async function fetchCompareProfile(user) {
if (!user.trim()) return;
try {
const profileRes = await fetch(`https://api.github.com/users/${user}`);
if (!profileRes.ok) {
if (profileRes.status === 404) {
throw new Error("User not found");
}
throw new Error("Failed to fetch profile");
}
const profileData = await profileRes.json();
setCompareProfile(profileData);
const reposRes = await fetch(
`https://api.github.com/users/${user}/repos?sort=updated&per_page=100`
);
if (reposRes.ok) {
const reposData = await reposRes.json();
setCompareRepos(reposData);
}
} catch (e) {
setError(e.message);
setCompareProfile(null);
setCompareRepos([]);
}
}
const handleSubmit = (e) => {
e.preventDefault();
console.log('Form submitted with username:', username);
fetchProfile(username);
};
const handleCompare = (e) => {
e.preventDefault();
fetchCompareProfile(compareUsername);
};
const calculateStats = (userRepos) => {
console.log('Calculating stats for repos count:', userRepos.length);
const stats = {
totalStars: 0,
totalForks: 0,
languages: {},
topLanguages: [],
};
userRepos.forEach((repo) => {
stats.totalStars += repo.stargazers_count || 0;
stats.totalForks += repo.forks_count || 0;
if (repo.language) {
stats.languages[repo.language] = (stats.languages[repo.language] || 0) + 1;
}
});
// Get top 5 languages
stats.topLanguages = Object.entries(stats.languages)
.sort(([, a], [, b]) => b - a)
.slice(0, 5);
console.log('Calculated stats:', stats);
return stats;
};
const profileStats = profile ? calculateStats(repos) : null;
console.log('Profile stats:', profileStats);
const compareStats = compareProfile ? calculateStats(compareRepos) : null;
console.log('Compare stats:', compareStats);
return (
<div>
<HeroSection
image={GitHubImg}
title={
<>
GitHub <span style={{ color: "black" }}>Profile Analyzer</span>
</>
}
subtitle="Analyze GitHub profiles with advanced statistics and comparison tools"
/>
<h2>π GitHub Profile Analyzer</h2>
<form onSubmit={handleSubmit} style={{ marginBottom: "2rem" }}>
<input
value={username}
onChange={(e) => setUsername(e.target.value)}
placeholder="Enter GitHub username"
style={{ marginRight: "1rem" }}
/>
<button type="submit">Analyze Profile</button>
</form>
{loading && <Loading />}
<ErrorMessage error={error} />
{profile && (
<div className="grid">
{/* Profile Overview */}
<Card title="Profile Overview" size="large">
<div style={{ display: "flex", alignItems: "center", gap: "1rem" }}>
<img
src={profile.avatar_url}
alt={profile.login}
style={{
width: 80,
height: 80,
borderRadius: "50%",
border: "2px solid #ddd",
}}
/>
<div>
<h3>{profile.name || profile.login}</h3>
<p>@{profile.login}</p>
{profile.bio && <p>{profile.bio}</p>}
{profile.location && <p>π {profile.location}</p>}
{profile.company && <p>π’ {profile.company}</p>}
<p>Joined: {new Date(profile.created_at).toLocaleDateString()}</p>
</div>
</div>
</Card>
{/* Basic Stats */}
<Card title="Basic Statistics">
<div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: "1rem" }}>
<div>
<strong>Followers:</strong> {profile.followers?.toLocaleString()}
</div>
<div>
<strong>Following:</strong> {profile.following?.toLocaleString()}
</div>
<div>
<strong>Public Repos:</strong> {profile.public_repos?.toLocaleString()}
</div>
<div>
<strong>Public Gists:</strong> {profile.public_gists?.toLocaleString()}
</div>
</div>
</Card>
{/* Advanced Stats */}
{profileStats && (
<Card title="Advanced Statistics">
<div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: "1rem" }}>
<div>
<strong>Total Stars:</strong> {profileStats.totalStars.toLocaleString()}
</div>
<div>
<strong>Total Forks:</strong> {profileStats.totalForks.toLocaleString()}
</div>
<div>
<strong>Top Languages:</strong>
<ul style={{ marginTop: "0.5rem", paddingLeft: "1rem" }}>
{profileStats.topLanguages.map(([lang, count]) => (
<li key={lang}>
{lang}: {count} repos
</li>
))}
</ul>
</div>
<div>
<strong>Average Stars per Repo:</strong>{" "}
{(profileStats.totalStars / repos.length).toFixed(1)}
</div>
</div>
</Card>
)}
{/* Recent Repositories */}
<Card title="Recent Repositories">
<div style={{ maxHeight: "300px", overflowY: "auto" }}>
{repos.slice(0, 10).map((repo) => (
<div
key={repo.id}
style={{
padding: "0.5rem",
borderBottom: "1px solid #eee",
display: "flex",
justifyContent: "space-between",
alignItems: "center",
}}
>
<div>
<a
href={repo.html_url}
target="_blank"
rel="noopener noreferrer"
style={{ textDecoration: "none", color: "#0366d6" }}
>
{repo.name}
</a>
{repo.language && (
<span style={{ marginLeft: "0.5rem", fontSize: "0.8rem", color: "#586069" }}>
{repo.language}
</span>
)}
</div>
<div style={{ display: "flex", gap: "0.5rem", fontSize: "0.8rem" }}>
<span>β {repo.stargazers_count}</span>
<span>π΄ {repo.forks_count}</span>
</div>
</div>
))}
</div>
</Card>
</div>
)}
{/* Profile Comparison Section */}
{profile && (
<div style={{ marginTop: "3rem" }}>
<h3>π Profile Comparison</h3>
<form onSubmit={handleCompare} style={{ marginBottom: "2rem" }}>
<input
value={compareUsername}
onChange={(e) => setCompareUsername(e.target.value)}
placeholder="Enter username to compare"
style={{ marginRight: "1rem" }}
/>
<button type="submit">Compare</button>
</form>
{compareProfile && (
<div className="grid">
<Card title={`@${profile.login} vs @${compareProfile.login}`}>
<div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: "2rem" }}>
<div>
<h4>{profile.name || profile.login}</h4>
<p>Followers: {profile.followers}</p>
<p>Following: {profile.following}</p>
<p>Repos: {profile.public_repos}</p>
{profileStats && (
<>
<p>Total Stars: {profileStats.totalStars}</p>
<p>Total Forks: {profileStats.totalForks}</p>
</>
)}
</div>
<div>
<h4>{compareProfile.name || compareProfile.login}</h4>
<p>Followers: {compareProfile.followers}</p>
<p>Following: {compareProfile.following}</p>
<p>Repos: {compareProfile.public_repos}</p>
{compareStats && (
<>
<p>Total Stars: {compareStats.totalStars}</p>
<p>Total Forks: {compareStats.totalForks}</p>
</>
)}
</div>
</div>
</Card>
</div>
)}
</div>
)}
</div>
);
}