1- // src/pages /dashboard/LeaderBoard/leaderboard.tsx
1+ // src/components /dashboard/LeaderBoard/leaderboard.tsx
22import React , { JSX , useState } from "react" ;
33import { motion } from "framer-motion" ;
44import {
@@ -129,18 +129,25 @@ function TopPerformerCard({
129129 ) ;
130130}
131131
132- // Define the time period type
133- type TimePeriod = "all" | "weekly" | "monthly" | "yearly" ;
134-
135132export 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