Skip to content

Commit 4d79e92

Browse files
authored
Merge pull request #626 from Adez017/Experiment
Add levels to the Leaderboard
2 parents f60f26b + fdc5c9b commit 4d79e92

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

src/lib/statsProvider.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ interface PRDetails {
4848
mergedAt: string;
4949
repoName: string;
5050
number: number;
51+
points: number;
5152
}
5253

5354
interface Contributor {
@@ -75,6 +76,7 @@ interface PullRequestItem {
7576
title?: string;
7677
html_url?: string;
7778
number?: number;
79+
labels?: Array<{ name: string }>;
7880
}
7981

8082
// Enhanced contributor type for internal processing (stores all PRs)
@@ -96,6 +98,33 @@ const MAX_CONCURRENT_REQUESTS = 8;
9698
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutes cache
9799
const MAX_PAGES_PER_REPO = 20;
98100

101+
// Function to calculate points based on PR labels
102+
const calculatePointsForPR = (labels?: Array<{ name: string }>): number => {
103+
if (!labels || labels.length === 0) {
104+
return 0; // No points if no labels
105+
}
106+
107+
const labelNames = labels.map(label => label.name.toLowerCase());
108+
109+
// Check if PR has the "recode" label
110+
if (!labelNames.includes('recode')) {
111+
return 0; // No points if "recode" label is missing
112+
}
113+
114+
// Check for level labels and assign points accordingly with new point system
115+
const levelPointsMap: { [key: string]: number } = {
116+
'level 1': 10,
117+
'level 2': 30,
118+
'level 3': 50,
119+
};
120+
const matchedLevel = labelNames.find(label => levelPointsMap.hasOwnProperty(label));
121+
if (matchedLevel) {
122+
return levelPointsMap[matchedLevel];
123+
}
124+
125+
return 0; // No points if no level label
126+
};
127+
99128
// Time filter utility functions
100129
const getTimeFilterDate = (filter: TimeFilter): Date | null => {
101130
const now = new Date();
@@ -163,11 +192,14 @@ export function CommunityStatsProvider({ children }: CommunityStatsProviderProps
163192
isPRInTimeRange(pr.mergedAt, currentTimeFilter)
164193
);
165194

195+
// Calculate total points from all filtered PRs
196+
const totalPoints = filteredPRs.reduce((sum, pr) => sum + pr.points, 0);
197+
166198
return {
167199
username: contributor.username,
168200
avatar: contributor.avatar,
169201
profile: contributor.profile,
170-
points: filteredPRs.length * POINTS_PER_PR,
202+
points: totalPoints,
171203
prs: filteredPRs.length,
172204
prDetails: filteredPRs, // For backward compatibility, though we'll use the new function
173205
};
@@ -319,6 +351,9 @@ export function CommunityStatsProvider({ children }: CommunityStatsProviderProps
319351
}
320352
const contributor = contributorMap.get(username)!;
321353

354+
// Calculate points for this PR based on labels
355+
const prPoints = calculatePointsForPR(pr.labels);
356+
322357
// Add detailed PR information to the full list
323358
if (pr.title && pr.html_url && pr.merged_at && pr.number) {
324359
contributor.allPRDetails.push({
@@ -327,6 +362,7 @@ export function CommunityStatsProvider({ children }: CommunityStatsProviderProps
327362
mergedAt: pr.merged_at,
328363
repoName,
329364
number: pr.number,
365+
points: prPoints,
330366
});
331367
}
332368
});

0 commit comments

Comments
 (0)