1- import React , { useState , useEffect , useMemo } from 'react' ;
1+ import React , { useState , useEffect } from 'react' ;
22import { useNavigate } from 'react-router-dom' ;
33import { getAllAttemptSummaries } from '../../lib/api' ;
44// --- FIX: Import both time formatters ---
@@ -22,66 +22,36 @@ interface SessionSummary {
2222 question_title : string ;
2323 question_difficulty : "Easy" | "Medium" | "Hard" ;
2424 partner_id : string ;
25- partner_username ?: string ;
26- is_solved_successfully : boolean | null ;
25+ is_solved_successfully : boolean ;
2726 has_penalty : boolean ; // We need this to determine "Incomplete"
2827 started_at : string ;
29- time_taken_ms : number | null ;
28+ time_taken_ms : number ;
3029}
3130
32- interface RecentSessionsListProps {
33- userId : string ;
34- limit ?: number ;
35- summaries ?: SessionSummary [ ] ;
36- }
37-
38- const RecentSessionsList : React . FC < RecentSessionsListProps > = ( { userId, limit, summaries : providedSummaries } ) => {
31+ const RecentSessionsList : React . FC < { userId : string , limit : number } > = ( { userId, limit } ) => {
3932 const navigate = useNavigate ( ) ;
4033 const [ sessions , setSessions ] = useState < SessionSummary [ ] > ( [ ] ) ;
4134 const [ loading , setLoading ] = useState ( true ) ;
4235
43- // Create a stable reference for providedSummaries to avoid dependency array issues
44- const summariesKey = useMemo ( ( ) => {
45- return providedSummaries ? JSON . stringify ( providedSummaries . map ( s => s . session_id ) ) : null ;
46- } , [ providedSummaries ] ) ;
47-
4836 useEffect ( ( ) => {
49- // If summaries are provided, use them directly
50- if ( providedSummaries && providedSummaries . length >= 0 ) {
51- const sortedData = [ ...providedSummaries ] . sort ( ( a :SessionSummary , b :SessionSummary ) =>
52- new Date ( b . started_at ) . getTime ( ) - new Date ( a . started_at ) . getTime ( )
53- ) ;
54- setSessions ( limit ? sortedData . slice ( 0 , limit ) : sortedData ) ;
55- setLoading ( false ) ;
56- return ;
57- }
58-
59- // Otherwise, fetch from API
60- if ( ! userId ) {
61- setLoading ( false ) ;
62- setSessions ( [ ] ) ;
63- return ;
64- }
37+ if ( ! userId ) return ;
6538
6639 const fetchRecent = async ( ) => {
6740 try {
6841 setLoading ( true ) ;
6942 const data = await getAllAttemptSummaries ( userId ) ;
7043 // Sort by date DESC (as getAllAttemptSummaries might not guarantee order)
71- const sortedData = data ? data . sort ( ( a :SessionSummary , b :SessionSummary ) =>
72- new Date ( b . started_at ) . getTime ( ) - new Date ( a . started_at ) . getTime ( )
73- ) : [ ] ;
74- setSessions ( limit ? sortedData . slice ( 0 , limit ) : sortedData ) ;
44+ const sortedData = data ? data . sort ( ( a :SessionSummary , b :SessionSummary ) => new Date ( b . started_at ) . getTime ( ) - new Date ( a . started_at ) . getTime ( ) ) : [ ] ;
45+ setSessions ( sortedData . slice ( 0 , limit ) ) ;
7546 } catch ( error ) {
7647 console . error ( "Failed to fetch recent sessions:" , error ) ;
77- setSessions ( [ ] ) ;
7848 } finally {
7949 setLoading ( false ) ;
8050 }
8151 } ;
8252
8353 fetchRecent ( ) ;
84- } , [ userId , limit , summariesKey ] ) ;
54+ } , [ userId , limit ] ) ;
8555
8656 const handleSessionClick = ( questionId : string ) => {
8757 // Navigate to the detail page for that question
@@ -121,7 +91,7 @@ const RecentSessionsList: React.FC<RecentSessionsListProps> = ({ userId, limit,
12191 < div className = "flex items-center gap-4 text-sm text-muted-foreground" >
12292 < div className = "flex items-center gap-1" >
12393 < User className = "h-3 w-3" />
124- { session . partner_username || session . partner_id }
94+ { session . partner_id }
12595 </ div >
12696 < div className = "flex items-center gap-1" >
12797 < Clock className = "h-3 w-3" />
@@ -138,11 +108,11 @@ const RecentSessionsList: React.FC<RecentSessionsListProps> = ({ userId, limit,
138108
139109 { /* API provides booleans, we derive the status */ }
140110 < Badge className = {
141- session . is_solved_successfully === true ? 'bg-green-500 text-white' : // "Passed" (green )
142- session . has_penalty ? 'bg-orange-500 text-white' : // "Incomplete" (orange )
111+ session . is_solved_successfully ? 'bg-green-500 text-white' : // "Passed" (blue )
112+ session . has_penalty ? 'bg-orange-500 text-white' : // "Incomplete" (gray )
143113 'bg-red-500 text-white' // "Failed" (red)
144114 } >
145- { session . is_solved_successfully === true ? "Passed" : ( session . has_penalty ? "Incomplete" : "Failed" ) }
115+ { session . is_solved_successfully ? "Passed" : ( session . has_penalty ? "Incomplete" : "Failed" ) }
146116 </ Badge >
147117 </ div >
148118 </ Card >
0 commit comments