'use client'; import { useState, useEffect } from 'react'; import Card from '@/components/ui/Card'; import { getStandingRepository, getLeagueRepository, getTeamMembershipRepository } from '@/lib/di-container'; import { EntityMappers } from '@gridpilot/racing/application/mappers/EntityMappers'; import type { LeagueDTO } from '@gridpilot/racing/application/dto/LeagueDTO'; interface TeamStandingsProps { teamId: string; leagues: string[]; } interface TeamLeagueStanding { leagueId: string; leagueName: string; position: number; points: number; wins: number; racesCompleted: number; } export default function TeamStandings({ teamId, leagues }: TeamStandingsProps) { const [standings, setStandings] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { const loadStandings = async () => { const standingRepo = getStandingRepository(); const leagueRepo = getLeagueRepository(); const teamMembershipRepo = getTeamMembershipRepository(); const members = await teamMembershipRepo.getTeamMembers(teamId); const memberIds = members.map(m => m.driverId); const teamStandings: TeamLeagueStanding[] = []; for (const leagueId of leagues) { const league = await leagueRepo.findById(leagueId); if (!league) continue; const leagueStandings = await standingRepo.findByLeagueId(leagueId); // Calculate team points (sum of all team members) let totalPoints = 0; let totalWins = 0; let totalRaces = 0; for (const standing of leagueStandings) { if (memberIds.includes(standing.driverId)) { totalPoints += standing.points; totalWins += standing.wins; totalRaces = Math.max(totalRaces, standing.racesCompleted); } } // Calculate team position (simplified - based on total points) const allTeamPoints = leagueStandings .filter(s => memberIds.includes(s.driverId)) .reduce((sum, s) => sum + s.points, 0); const position = leagueStandings .filter((_, idx, arr) => { const teamPoints = arr .filter(s => memberIds.includes(s.driverId)) .reduce((sum, s) => sum + s.points, 0); return teamPoints > allTeamPoints; }).length + 1; teamStandings.push({ leagueId, leagueName: league.name, position, points: totalPoints, wins: totalWins, racesCompleted: totalRaces, }); } setStandings(teamStandings); setLoading(false); }; loadStandings(); }, [teamId, leagues]); if (loading) { return (
Loading standings...
); } return (

League Standings

{standings.map((standing) => (

{standing.leagueName}

P{standing.position}
{standing.points}
Points
{standing.wins}
Wins
{standing.racesCompleted}
Races
))}
{standings.length === 0 && (
No standings available yet.
)}
); }