1- import React , { useState , useEffect } from 'react' ;
1+ import React , { useState , useEffect , useMemo } from 'react' ;
22import { useNavigate } from 'react-router-dom' ;
33import { getAllAttemptSummaries } from '../../lib/api' ;
44// --- FIX: Import both time formatters ---
@@ -22,36 +22,66 @@ interface SessionSummary {
2222 question_title : string ;
2323 question_difficulty : "Easy" | "Medium" | "Hard" ;
2424 partner_id : string ;
25- is_solved_successfully : boolean ;
25+ partner_username ?: string ;
26+ is_solved_successfully : boolean | null ;
2627 has_penalty : boolean ; // We need this to determine "Incomplete"
2728 started_at : string ;
28- time_taken_ms : number ;
29+ time_taken_ms : number | null ;
2930}
3031
31- const RecentSessionsList : React . FC < { userId : string , limit : number } > = ( { userId, limit } ) => {
32+ interface RecentSessionsListProps {
33+ userId : string ;
34+ limit ?: number ;
35+ summaries ?: SessionSummary [ ] ;
36+ }
37+
38+ const RecentSessionsList : React . FC < RecentSessionsListProps > = ( { userId, limit, summaries : providedSummaries } ) => {
3239 const navigate = useNavigate ( ) ;
3340 const [ sessions , setSessions ] = useState < SessionSummary [ ] > ( [ ] ) ;
3441 const [ loading , setLoading ] = useState ( true ) ;
3542
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+
3648 useEffect ( ( ) => {
37- if ( ! userId ) return ;
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+ }
3865
3966 const fetchRecent = async ( ) => {
4067 try {
4168 setLoading ( true ) ;
4269 const data = await getAllAttemptSummaries ( userId ) ;
4370 // Sort by date DESC (as getAllAttemptSummaries might not guarantee order)
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 ) ) ;
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 ) ;
4675 } catch ( error ) {
4776 console . error ( "Failed to fetch recent sessions:" , error ) ;
77+ setSessions ( [ ] ) ;
4878 } finally {
4979 setLoading ( false ) ;
5080 }
5181 } ;
5282
5383 fetchRecent ( ) ;
54- } , [ userId , limit ] ) ;
84+ } , [ userId , limit , summariesKey ] ) ;
5585
5686 const handleSessionClick = ( questionId : string ) => {
5787 // Navigate to the detail page for that question
@@ -91,7 +121,7 @@ const RecentSessionsList: React.FC<{ userId: string, limit: number }> = ({ userI
91121 < div className = "flex items-center gap-4 text-sm text-muted-foreground" >
92122 < div className = "flex items-center gap-1" >
93123 < User className = "h-3 w-3" />
94- { session . partner_id }
124+ { session . partner_username || session . partner_id }
95125 </ div >
96126 < div className = "flex items-center gap-1" >
97127 < Clock className = "h-3 w-3" />
@@ -108,11 +138,11 @@ const RecentSessionsList: React.FC<{ userId: string, limit: number }> = ({ userI
108138
109139 { /* API provides booleans, we derive the status */ }
110140 < Badge className = {
111- session . is_solved_successfully ? 'bg-green-500 text-white' : // "Passed" (blue )
112- session . has_penalty ? 'bg-orange-500 text-white' : // "Incomplete" (gray )
141+ session . is_solved_successfully === true ? 'bg-green-500 text-white' : // "Passed" (green )
142+ session . has_penalty ? 'bg-orange-500 text-white' : // "Incomplete" (orange )
113143 'bg-red-500 text-white' // "Failed" (red)
114144 } >
115- { session . is_solved_successfully ? "Passed" : ( session . has_penalty ? "Incomplete" : "Failed" ) }
145+ { session . is_solved_successfully === true ? "Passed" : ( session . has_penalty ? "Incomplete" : "Failed" ) }
116146 </ Badge >
117147 </ div >
118148 </ Card >
0 commit comments