-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
251 lines (214 loc) · 9.41 KB
/
Copy pathscript.js
File metadata and controls
251 lines (214 loc) · 9.41 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
document.addEventListener("DOMContentLoaded", function(){
// DOM Elements
const searchBtn = document.getElementById("search-btn");
const usernameInput = document.getElementById("user-input");
const statsContainer = document.getElementById("stats-container");
const emptyState = document.getElementById("lm-empty-state");
const errorState = document.getElementById("lm-error-state");
const errorMsg = document.getElementById("lm-error-msg");
const retryBtn = document.getElementById("lm-retry-btn");
const skeleton = document.getElementById("lm-skeleton");
const easyProgressCircle = document.querySelector(".easy-progress");
const mediumProgressCircle = document.querySelector(".medium-progress");
const hardProgressCircle = document.querySelector(".hard-progress");
const easyLabel = document.getElementById("easy-label");
const mediumLabel = document.getElementById("medium-label");
const hardLabel = document.getElementById("hard-label");
const cardStatsContainer = document.getElementById("stats-card");
const copyBtn = document.getElementById("lm-copy-btn");
const downloadBtn = document.getElementById("lm-download-btn");
const themeBtn = document.getElementById("lm-theme-btn");
const root = document.querySelector(".lm-root");
const searchForm = document.getElementById("lm-search-form");
let currentStats = null;
// Theme toggle
themeBtn.addEventListener("click", function() {
const currentTheme = root.getAttribute("data-theme");
const newTheme = currentTheme === "dark" ? "light" : "dark";
root.setAttribute("data-theme", newTheme);
localStorage.setItem("lm-theme", newTheme);
});
// Load saved theme
const savedTheme = localStorage.getItem("lm-theme") || "dark";
root.setAttribute("data-theme", savedTheme);
// Validation
function validateUsername(username) {
if (username.trim() === "") {
alert("Username cannot be empty");
return false;
}
const regex = /^[a-zA-Z0-9_-]{1,16}$/;
const isMatching = regex.test(username);
if (!isMatching) {
alert("Invalid username");
}
return isMatching;
}
// Fetch user stats
async function fetchUserDetails(username) {
const url = `https://leetcode-stats-api.herokuapp.com/${username}`;
try {
searchBtn.setAttribute("aria-busy", "true");
skeleton.style.display = "flex";
statsContainer.hidden = false;
emptyState.hidden = true;
errorState.hidden = true;
const response = await fetch(url);
if (!response.ok) {
throw new Error("Unable to fetch user details");
}
const parsedData = await response.json();
if (parsedData.status === "error" || !parsedData.totalSolved) {
throw new Error("No data found for this user");
}
currentStats = parsedData;
displayUserStats(parsedData);
skeleton.style.display = "none";
} catch (error) {
console.error(error);
showError(error.message || "Failed to fetch data. Please try again.");
} finally {
searchBtn.setAttribute("aria-busy", "false");
}
}
function showError(message) {
errorMsg.textContent = message;
errorState.hidden = false;
statsContainer.hidden = true;
emptyState.hidden = true;
skeleton.style.display = "none";
}
// Update progress circles
function updateProgress(solved, total, label, circle) {
const progressDegree = total > 0 ? (solved / total) * 100 : 0;
circle.style.setProperty("--progress-degree", progressDegree);
circle.setAttribute("aria-valuenow", Math.round(progressDegree));
// Animate the number
animateCounter(label, 0, solved);
}
// Animate counter
function animateCounter(element, start, end) {
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
element.textContent = `${end}`;
return;
}
const duration = 900;
const startTime = Date.now();
function update() {
const elapsed = Date.now() - startTime;
const progress = Math.min(elapsed / duration, 1);
const current = Math.floor(start + (end - start) * progress);
element.textContent = current;
if (progress < 1) {
requestAnimationFrame(update);
} else {
element.textContent = end;
}
}
requestAnimationFrame(update);
}
// Get total submissions from calendar
function getTotalSubmissions(submissionCalendar) {
if (!submissionCalendar) return 0;
return Object.values(submissionCalendar).reduce((a, b) => a + b, 0);
}
// Build stats card HTML
function buildStatsCard(data) {
const submissionCalendar = data.submissionCalendar || {};
const totalSubmissions = getTotalSubmissions(submissionCalendar);
const daysActive = Object.keys(submissionCalendar).length || 0;
const avgPerDay = daysActive > 0 ? (totalSubmissions / daysActive) : 0;
const maxStreak = data.maxStreak || data.maxConsecutiveDays || 0;
const solvedTotalQuestions = data.totalSolved || 0;
const totalQuestions = data.totalQuestions || 0;
const cardHTML = `
<div class="card-item">
<div class="item-label">Total Submissions</div>
<div class="item-value">${totalSubmissions}</div>
</div>
<div class="card-item">
<div class="item-label">Problems Solved</div>
<div class="item-value">${solvedTotalQuestions}</div>
</div>
<div class="card-item">
<div class="item-label">Total Problems</div>
<div class="item-value">${totalQuestions}</div>
</div>
<div class="card-item">
<div class="item-label">Days Active</div>
<div class="item-value">${daysActive}</div>
</div>
`;
return cardHTML;
}
// Display user stats
function displayUserStats(parsedData) {
if (!parsedData || parsedData.status !== "success") {
showError("No data found");
return;
}
const totalQuestions = parsedData.totalQuestions;
const totalEasy = parsedData.totalEasy;
const totalMedium = parsedData.totalMedium;
const totalHard = parsedData.totalHard;
const solvedEasyQuestions = parsedData.easySolved || 0;
const solvedMediumQuestions = parsedData.mediumSolved || 0;
const solvedHardQuestions = parsedData.hardSolved || 0;
// Update progress circles
updateProgress(solvedEasyQuestions, totalEasy, easyLabel, easyProgressCircle);
updateProgress(solvedMediumQuestions, totalMedium, mediumLabel, mediumProgressCircle);
updateProgress(solvedHardQuestions, totalHard, hardLabel, hardProgressCircle);
// Render stats card
if (cardStatsContainer) {
cardStatsContainer.innerHTML = buildStatsCard(parsedData);
}
}
// Copy stats to clipboard
copyBtn.addEventListener("click", function() {
if (!currentStats) return;
const text = `LeetMetric Stats for ${currentStats.username}:
Total Solved: ${currentStats.totalSolved}
Total Questions: ${currentStats.totalQuestions}
Easy: ${currentStats.easySolved}/${currentStats.totalEasy}
Medium: ${currentStats.mediumSolved}/${currentStats.totalMedium}
Hard: ${currentStats.hardSolved}/${currentStats.totalHard}`;
navigator.clipboard.writeText(text).then(() => {
const originalText = copyBtn.textContent;
copyBtn.textContent = "✓ Copied!";
setTimeout(() => {
copyBtn.textContent = originalText;
}, 1500);
}).catch(err => console.error("Copy failed:", err));
});
// Download stats as JSON
downloadBtn.addEventListener("click", function() {
if (!currentStats) return;
const blob = new Blob([JSON.stringify(currentStats, null, 2)], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = `leetmetric_${currentStats.username}.json`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// Search form submit
searchForm.addEventListener("submit", function(e) {
e.preventDefault();
const username = usernameInput.value.trim();
if (validateUsername(username)) {
fetchUserDetails(username);
}
});
// Retry button
retryBtn.addEventListener("click", function() {
emptyState.hidden = false;
errorState.hidden = true;
statsContainer.hidden = true;
skeleton.style.display = "none";
usernameInput.focus();
});
// Initial focus
usernameInput.focus();
});