Skip to content

Commit cfb379c

Browse files
authored
Merge pull request #605 from Adez017/Experiment
fixed the filters PR Modal
2 parents 57944ea + 950b8ee commit cfb379c

File tree

3 files changed

+202
-130
lines changed

3 files changed

+202
-130
lines changed

src/components/dashboard/LeaderBoard/PRListModal.tsx

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from "react";
33
import { motion, AnimatePresence } from "framer-motion";
44
import { FaTimes, FaExternalLinkAlt, FaGithub } from "react-icons/fa";
55
import { useColorMode } from "@docusaurus/theme-common";
6+
import { useCommunityStatsContext } from "../../../lib/statsProvider";
67

78
interface PRDetails {
89
title: string;
@@ -30,9 +31,15 @@ interface PRListModalProps {
3031
export default function PRListModal({ contributor, isOpen, onClose }: PRListModalProps): JSX.Element | null {
3132
const { colorMode } = useColorMode();
3233
const isDark = colorMode === "dark";
34+
35+
// Get filtered PRs from context
36+
const { getFilteredPRsForContributor, currentTimeFilter } = useCommunityStatsContext();
3337

3438
if (!contributor) return null;
3539

40+
// Get filtered PRs instead of using contributor.prDetails
41+
const filteredPRs = getFilteredPRsForContributor(contributor.username);
42+
3643
const formatDate = (dateString: string) => {
3744
const date = new Date(dateString);
3845
return date.toLocaleDateString('en-US', {
@@ -54,6 +61,17 @@ export default function PRListModal({ contributor, isOpen, onClose }: PRListModa
5461
}
5562
};
5663

64+
// Helper function to get filter display text
65+
const getFilterDisplayText = (filter: string) => {
66+
switch (filter) {
67+
case 'week': return 'This Week';
68+
case 'month': return 'This Month';
69+
case 'year': return 'This Year';
70+
case 'all': return 'All Time';
71+
default: return 'All Time';
72+
}
73+
};
74+
5775
return (
5876
<AnimatePresence>
5977
{isOpen && (
@@ -89,7 +107,13 @@ export default function PRListModal({ contributor, isOpen, onClose }: PRListModa
89107
{contributor.username}'s Pull Requests
90108
</h2>
91109
<p className={`pr-modal-subtitle ${isDark ? "dark" : "light"}`}>
92-
{contributor.prs} merged PR{contributor.prs !== 1 ? 's' : ''}{contributor.points} points
110+
{/*Show filtered count and add filter info */}
111+
{filteredPRs.length} merged PR{filteredPRs.length !== 1 ? 's' : ''}{filteredPRs.length * 10} points
112+
{currentTimeFilter !== 'all' && (
113+
<span style={{ marginLeft: '8px', opacity: 0.7 }}>
114+
({getFilterDisplayText(currentTimeFilter)})
115+
</span>
116+
)}
93117
</p>
94118
</div>
95119
</div>
@@ -104,9 +128,10 @@ export default function PRListModal({ contributor, isOpen, onClose }: PRListModa
104128

105129
{/* Modal Body */}
106130
<div className={`pr-modal-body ${isDark ? "dark" : "light"}`}>
107-
{contributor.prDetails && contributor.prDetails.length > 0 ? (
131+
{/*Use filteredPRs instead of contributor.prDetails */}
132+
{filteredPRs && filteredPRs.length > 0 ? (
108133
<div className="pr-list">
109-
{contributor.prDetails.map((pr, index) => (
134+
{filteredPRs.map((pr, index) => (
110135
<motion.div
111136
key={`${pr.repoName}-${pr.number}`}
112137
className={`pr-item ${isDark ? "dark" : "light"}`}
@@ -148,9 +173,17 @@ export default function PRListModal({ contributor, isOpen, onClose }: PRListModa
148173
) : (
149174
<div className={`pr-empty-state ${isDark ? "dark" : "light"}`}>
150175
<FaGithub className="pr-empty-icon" />
151-
<p>No pull request details available</p>
176+
<p>
177+
{currentTimeFilter === 'all'
178+
? 'No pull request details available'
179+
: `No PRs found for ${getFilterDisplayText(currentTimeFilter).toLowerCase()}`
180+
}
181+
</p>
152182
<p className="pr-empty-subtitle">
153-
PR details might not be loaded yet or this contributor has no merged PRs.
183+
{currentTimeFilter === 'all'
184+
? 'PR details might not be loaded yet or this contributor has no merged PRs.'
185+
: `Try selecting a different time period or check "All Time" to see all PRs.`
186+
}
154187
</p>
155188
</div>
156189
)}

src/components/dashboard/LeaderBoard/leaderboard.tsx

Lines changed: 32 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// src/pages/dashboard/LeaderBoard/leaderboard.tsx
1+
// src/components/dashboard/LeaderBoard/leaderboard.tsx
22
import React, { JSX, useState } from "react";
33
import { motion } from "framer-motion";
44
import {
@@ -129,18 +129,25 @@ function TopPerformerCard({
129129
);
130130
}
131131

132-
// Define the time period type
133-
type TimePeriod = "all" | "weekly" | "monthly" | "yearly";
134-
135132
export default function LeaderBoard(): JSX.Element {
136-
const { contributors, stats, loading, error } = useCommunityStatsContext();
133+
// Get time filter functions from context
134+
const {
135+
contributors,
136+
stats,
137+
loading,
138+
error,
139+
currentTimeFilter,
140+
setTimeFilter
141+
} = useCommunityStatsContext();
142+
137143
const { colorMode } = useColorMode();
138144
const isDark = colorMode === "dark";
139145

140146
const [searchQuery, setSearchQuery] = useState("");
141147
const [currentPage, setCurrentPage] = useState(1);
142148
const [selectedContributor, setSelectedContributor] = useState<Contributor | null>(null);
143149
const [isModalOpen, setIsModalOpen] = useState(false);
150+
const [isSelectChanged, setIsSelectChanged] = useState(false);
144151
const itemsPerPage = 10;
145152

146153
// Modal handlers
@@ -162,82 +169,17 @@ export default function LeaderBoard(): JSX.Element {
162169
: [])
163170
: contributors;
164171

165-
166-
const [timePeriod, setTimePeriod] = useState<TimePeriod>("all");
167-
const [isSelectChanged, setIsSelectChanged] = useState(false);
168-
169172

170-
// Get contributions within the selected time period
171-
const getContributionsWithinTimePeriod = (contributors: Contributor[]) => {
172-
if (timePeriod === "all") return contributors;
173-
174-
// Get date threshold based on selected time period
175-
const now = new Date();
176-
let threshold = new Date();
177-
178-
switch (timePeriod) {
179-
case "weekly":
180-
threshold.setDate(now.getDate() - 7); // Past 7 days
181-
break;
182-
case "monthly":
183-
threshold.setMonth(now.getMonth() - 1); // Past month
184-
break;
185-
case "yearly":
186-
threshold.setFullYear(now.getFullYear() - 1); // Past year
187-
break;
188-
}
189-
190-
// Since we don't have the actual PR dates in the component,
191-
// we'll simulate filtering by reducing the PR counts by a factor
192-
// In a real implementation, you would filter based on actual PR dates
193-
return contributors.map(contributor => {
194-
// Apply a random factor based on time period to simulate date filtering
195-
// This is just for demonstration - in a real app you'd use actual date data
196-
let factor = 1;
197-
switch (timePeriod) {
198-
case "weekly":
199-
factor = 0.1 + Math.random() * 0.1; // Keep 10-20% for weekly
200-
break;
201-
case "monthly":
202-
factor = 0.3 + Math.random() * 0.2; // Keep 30-50% for monthly
203-
break;
204-
case "yearly":
205-
factor = 0.7 + Math.random() * 0.2; // Keep 70-90% for yearly
206-
break;
207-
}
208-
209-
const filteredPrs = Math.floor(contributor.prs * factor);
210-
return {
211-
...contributor,
212-
prs: filteredPrs,
213-
points: filteredPrs * 10, // Assuming each PR is worth 10 points
214-
};
215-
}).filter(contributor => contributor.prs > 0); // Remove contributors with 0 PRs
216-
};
217-
218-
// Filter out excluded users, apply time period filter, and then apply search filter
219-
const filteredContributors = getContributionsWithinTimePeriod(contributors)
173+
// Filter out excluded users and apply search filter
174+
const filteredContributors = contributors
220175
.filter((contributor) =>
221176
!EXCLUDED_USERS.some(excludedUser =>
222177
contributor.username.toLowerCase() === excludedUser.toLowerCase()
223178
)
224179
)
225180
.filter((contributor) =>
226181
contributor.username.toLowerCase().includes(searchQuery.toLowerCase())
227-
)
228-
// Re-sort contributors after filtering to ensure proper ranking
229-
.sort((a, b) => {
230-
// First sort by points (descending)
231-
if (b.points !== a.points) {
232-
return b.points - a.points;
233-
}
234-
// If points are equal, sort by PRs (descending)
235-
if (b.prs !== a.prs) {
236-
return b.prs - a.prs;
237-
}
238-
// If both points and PRs are equal, sort alphabetically by username (ascending)
239-
return a.username.localeCompare(b.username);
240-
});
182+
);
241183

242184
const totalPages = Math.ceil(filteredContributors.length / itemsPerPage);
243185
const indexOfLast = currentPage * itemsPerPage;
@@ -343,6 +285,17 @@ export default function LeaderBoard(): JSX.Element {
343285
return "regular";
344286
};
345287

288+
// Helper function for time filter display
289+
const getTimeFilterLabel = (filter: string) => {
290+
switch (filter) {
291+
case 'week': return '📊 This Week';
292+
case 'month': return '📆 This Month';
293+
case 'year': return '📅 This Year';
294+
case 'all': return '🏆 All Time';
295+
default: return '🏆 All Time';
296+
}
297+
};
298+
346299
return (
347300
<div className={`leaderboard-container ${isDark ? "dark" : "light"}`}>
348301
<div className="leaderboard-content">
@@ -368,24 +321,24 @@ export default function LeaderBoard(): JSX.Element {
368321
<label htmlFor="time-period-filter" className="filter-label">Time Period:</label>
369322
<select
370323
id="time-period-filter"
371-
value={timePeriod}
324+
value={currentTimeFilter}
372325
onChange={(e) => {
373-
setTimePeriod(e.target.value as TimePeriod);
326+
// Use setTimeFilter from context
327+
setTimeFilter(e.target.value as any);
374328
setCurrentPage(1);
375329
setIsSelectChanged(true);
376330
setTimeout(() => setIsSelectChanged(false), 1200);
377331
}}
378332
className={`time-filter-select ${isDark ? "dark" : "light"} ${isSelectChanged ? 'highlight-change' : ''}`}
379333
>
380334
<option value="all">🏆 All Time</option>
381-
<option value="yearly">📅 Past Year</option>
382-
<option value="monthly">📆 Past Month</option>
383-
<option value="weekly">📊 Past Week</option>
335+
<option value="year">📅 This Year</option>
336+
<option value="month">📆 This Month</option>
337+
<option value="week">📊 This Week</option>
384338
</select>
385339
</div>
386340
</div>
387341
<div className="top-performers-grid">
388-
389342
<TopPerformerCard contributor={filteredContributors[1]} rank={2} onPRClick={handlePRClick} />
390343
<TopPerformerCard contributor={filteredContributors[0]} rank={1} onPRClick={handlePRClick} />
391344
<TopPerformerCard contributor={filteredContributors[2]} rank={3} onPRClick={handlePRClick} />

0 commit comments

Comments
 (0)