Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
198 lines (182 loc) · 6.62 KB
/
Copy pathapp.js
File metadata and controls
198 lines (182 loc) · 6.62 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
const resultsDiv = document.getElementById("results");
const fetchButton = document.getElementById("fetchButton");
const themeToggleButton = document.getElementById("themeToggle");
const compareModeCheckbox = document.getElementById("compareMode");
const username2Input = document.getElementById("username2");
const modeHint = document.getElementById("modeHint");
compareModeCheckbox.addEventListener("change", () => {
const enabled = compareModeCheckbox.checked;
username2Input.disabled = !enabled;
modeHint.innerHTML = enabled
? "Shows users who follow <em>both</em> of you, and users whom <em>both</em> of you follow."
: "Shows users who follow you <em>and</em> whom you follow back.";
if (!enabled) username2Input.value = "";
});
// Mock data for fallback
const mockUsers = Array.from({ length: 10 }, (_, i) => ({
login: `mockUser${i + 1}`,
avatar_url: `https://via.placeholder.com/60?text=User${i + 1}`,
html_url: `https://example.com/mockUser${i + 1}`,
}));
const mockFollowers = mockUsers.slice(0, 7);
const mockFollowing = mockUsers.slice(3, 10);
// Function to fetch paginated data from GitHub API
async function fetchGitHubData(endpoint) {
let results = [];
let url = endpoint;
while (url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
response.status === 403
? "Rate limit exceeded"
: `API error: ${response.status}`
);
}
results = results.concat(await response.json());
const linkHeader = response.headers.get("link");
url = linkHeader?.includes('rel="next"')
? linkHeader
.split(",")
.find((link) => link.includes('rel="next"'))
.split(";")[0]
.trim()
.replace(/<|>/g, "")
: null;
}
return results;
}
// Display the results as a grid
function displayResults(message, data = []) {
resultsDiv.innerHTML = message ? `<p>${message}</p>` : "";
if (data.length) {
resultsDiv.innerHTML += `
<div class="results">
${data
.map(
(user) => `
<a href="${user.html_url}" target="_blank" rel="noopener noreferrer" class="card">
<img src="${user.avatar_url}" alt="${user.login}'s avatar">
<span>${user.login}</span>
</a>`
)
.join("")}
</div>`;
}
}
// Display individual sections for "Mutual Followers" and "Mutual Following"
function displaySection(title, data = []) {
const section = document.createElement("div");
section.classList.add("results-section");
section.innerHTML = title ? `<h2>${title}</h2>` : "";
if (data.length) {
section.innerHTML += `
<div class="results">
${data
.map(
(user) => `
<a href="${user.html_url}" target="_blank" rel="noopener noreferrer" class="card">
<img src="${user.avatar_url}" alt="${user.login}'s avatar">
<span>${user.login}</span>
</a>`
)
.join("")}
</div>`;
} else {
section.innerHTML += "<p>No results found.</p>";
}
resultsDiv.appendChild(section);
}
// Get mutual users from multiple lists
function getMutualUsers(lists) {
return lists.reduce((acc, list) => {
const usernames = list.map((user) => user.login);
return acc.filter((user) => usernames.includes(user.login));
});
}
// Main function to handle single or multiple username input
async function findMutualFollowers(usernames) {
resultsDiv.innerHTML = "<p>Loading...</p>"; // Show loading message
try {
const trimmedUsernames = usernames
.split(",")
.map((username) => username.trim())
.filter((username) => username);
if (trimmedUsernames.length === 0) {
resultsDiv.innerHTML = "<p>Please enter at least one username.</p>";
return;
}
if (trimmedUsernames.length === 1) {
// Single user: Fetch their followers and following
const username = trimmedUsernames[0];
const [followers, following] = await Promise.all([
fetchGitHubData(`https://api.github.com/users/${username}/followers`),
fetchGitHubData(`https://api.github.com/users/${username}/following`),
]);
const mutualFollowers = followers.filter((f) =>
following.some((followed) => followed.login === f.login)
);
resultsDiv.innerHTML = ""; // Clear loading
displayResults(
mutualFollowers.length ? "" : "No mutual followers found.",
mutualFollowers
);
} else {
// Multiple users: Fetch followers/following for all users
const userFollowers = await Promise.all(
trimmedUsernames.map((username) =>
fetchGitHubData(`https://api.github.com/users/${username}/followers`)
)
);
const userFollowing = await Promise.all(
trimmedUsernames.map((username) =>
fetchGitHubData(`https://api.github.com/users/${username}/following`)
)
);
// Get mutual followers and following
const mutualFollowers = getMutualUsers(userFollowers);
const mutualFollowing = getMutualUsers(userFollowing);
resultsDiv.innerHTML = ""; // Clear loading
displaySection("Mutual Followers:", mutualFollowers);
displaySection("Mutual Following:", mutualFollowing);
}
} catch (error) {
// Fallback to mock data on API failure
if (error.message.includes("Rate limit exceeded")) {
resultsDiv.innerHTML = "<p>Rate limit exceeded. Showing mock data.</p>";
if (usernames.split(",").length === 1) {
displayResults("", mockFollowers);
} else {
const mutualFollowers = mockFollowers.filter((f) =>
mockFollowing.some((followed) => followed.login === f.login)
);
displaySection("Mutual Followers:", mutualFollowers);
displaySection("Mutual Following:", mockFollowing);
}
} else {
resultsDiv.innerHTML = `<p>Error: ${error.message}</p>`;
}
}
}
// Event listeners
fetchButton.addEventListener("click", () => {
const user1 = document.getElementById("username").value.trim();
if (!user1) {
resultsDiv.innerHTML = "<p>Please enter a GitHub username.</p>";
return;
}
const user2 = compareModeCheckbox.checked ? username2Input.value.trim() : "";
if (compareModeCheckbox.checked && !user2) {
resultsDiv.innerHTML = "<p>Please enter a second username for comparison.</p>";
return;
}
const usernameInput = user2 ? `${user1},${user2}` : user1;
findMutualFollowers(usernameInput);
});
themeToggleButton.addEventListener("click", () =>
document.body.classList.toggle("dark")
);
window.addEventListener("message", (e) => {
if (e.data?.type !== "theme") return;
document.body.classList.toggle("dark", e.data.theme === "dark");
});