'use client'; import { useState, useEffect } from 'react'; import { useRouter, useParams } from 'next/navigation'; import Button from '@/components/ui/Button'; import Card from '@/components/ui/Card'; import JoinLeagueButton from '@/components/leagues/JoinLeagueButton'; import LeagueMembers from '@/components/leagues/LeagueMembers'; import LeagueSchedule from '@/components/leagues/LeagueSchedule'; import LeagueAdmin from '@/components/leagues/LeagueAdmin'; import StandingsTable from '@/components/leagues/StandingsTable'; import { League } from '@gridpilot/racing/domain/entities/League'; import { Standing } from '@gridpilot/racing/domain/entities/Standing'; import { Driver } from '@gridpilot/racing/domain/entities/Driver'; import { getLeagueRepository, getRaceRepository, getDriverRepository, getStandingRepository } from '@/lib/di-container'; import { getMembership, isOwnerOrAdmin, getCurrentDriverId } from '@/lib/racingLegacyFacade'; export default function LeagueDetailPage() { const router = useRouter(); const params = useParams(); const leagueId = params.id as string; const [league, setLeague] = useState(null); const [owner, setOwner] = useState(null); const [standings, setStandings] = useState([]); const [drivers, setDrivers] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [activeTab, setActiveTab] = useState<'overview' | 'schedule' | 'standings' | 'members' | 'admin'>('overview'); const [refreshKey, setRefreshKey] = useState(0); const currentDriverId = getCurrentDriverId(); const membership = getMembership(leagueId, currentDriverId); const isAdmin = isOwnerOrAdmin(leagueId, currentDriverId); const loadLeagueData = async () => { try { const leagueRepo = getLeagueRepository(); const raceRepo = getRaceRepository(); const driverRepo = getDriverRepository(); const leagueData = await leagueRepo.findById(leagueId); if (!leagueData) { setError('League not found'); setLoading(false); return; } setLeague(leagueData); // Load owner data const ownerData = await driverRepo.findById(leagueData.ownerId); setOwner(ownerData); // Load standings const standingRepo = getStandingRepository(); const allStandings = await standingRepo.findAll(); const leagueStandings = allStandings.filter(s => s.leagueId === leagueId); setStandings(leagueStandings); // Load all drivers for standings const allDrivers = await driverRepo.findAll(); setDrivers(allDrivers); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to load league'); } finally { setLoading(false); } }; useEffect(() => { loadLeagueData(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [leagueId]); if (loading) { return (
Loading league...
); } if (error || !league) { return (
{error || 'League not found'}
); } const handleMembershipChange = () => { setRefreshKey(prev => prev + 1); loadLeagueData(); }; return ( <> {/* Action Card */} {!membership && (

Join This League

Become a member to participate in races and track your progress

)} {/* Overview section switcher (in-page, not primary tabs) */}
{isAdmin && ( )}
{/* Tab Content */} {activeTab === 'overview' && (
{/* League Info */}

League Information

{owner ? owner.name : `ID: ${league.ownerId.slice(0, 8)}...`}

{new Date(league.createdAt).toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })}

League Settings

{league.settings.pointsSystem.toUpperCase()}

{league.settings.sessionDuration} minutes

{league.settings.qualifyingFormat}

{/* Quick Actions */}

Quick Actions

{membership ? ( <> ) : ( )}
)} {activeTab === 'schedule' && ( )} {activeTab === 'standings' && (

Standings

)} {activeTab === 'members' && (

League Members

)} {activeTab === 'admin' && isAdmin && ( )} ); }