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
25 changes: 25 additions & 0 deletions scripts/fetch-user-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,17 @@ async function fetchUserInfo(username) {
}

// 2. Fetch live profile ranking from the wrapper API
let liveSolved = null;
const livePromise = fetchWithTimeout(liveApiUrl).then(async (res) => {
if (res.ok) {
const apiData = await res.json();
ranking = apiData.ranking || 0;
contest = apiData.contest || null;
liveSolved = {
easy: apiData.easySolved || 0,
medium: apiData.mediumSolved || 0,
hard: apiData.hardSolved || 0,
};
} else {
throw new Error(`LeetCode API wrapper returned status ${res.status}`);
}
Expand All @@ -85,6 +91,25 @@ async function fetchUserInfo(username) {
history = Array.isArray(history) ? history : [];
history.sort((a, b) => new Date(a.date) - new Date(b.date));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this sorting line below the logic where we insert today's count? While today's entry will technically always be the most recent date, it's generally safer and better practice to sort the array after all insertions or modifications have been made. That way the data is guaranteed to be in the correct order regardless of how it's constructed in the future.


// Update or append today's entry with live solved counts
if (liveSolved) {
const today = new Date().toISOString().split("T")[0];
let todayEntry = history.find((entry) => entry.date === today);
if (!todayEntry) {
todayEntry = {
date: today,
easy: liveSolved.easy,
medium: liveSolved.medium,
hard: liveSolved.hard,
};
history.push(todayEntry);
} else {
todayEntry.easy = liveSolved.easy;
todayEntry.medium = liveSolved.medium;
todayEntry.hard = liveSolved.hard;
}
}

return {
username,
ranking,
Expand Down
Loading