'use client'; import React, { useEffect, useMemo, useState } from 'react'; import Link from 'next/link'; import Image from 'next/image'; import MembershipStatus from '@/components/leagues/MembershipStatus'; import FeatureLimitationTooltip from '@/components/alpha/FeatureLimitationTooltip'; import { getLeagueCoverClasses } from '@/lib/leagueCovers'; import { getDriverRepository, getDriverStats, getAllDriverRankings, getImageService, } from '@/lib/di-container'; import type { DriverDTO } from '@gridpilot/racing/application/dto/DriverDTO'; import { EntityMappers } from '@gridpilot/racing/application/mappers/EntityMappers'; import DriverSummaryPill from '@/components/profile/DriverSummaryPill'; interface LeagueHeaderProps { leagueId: string; leagueName: string; description?: string | null; ownerId: string; ownerName: string; } export default function LeagueHeader({ leagueId, leagueName, description, ownerId, ownerName, }: LeagueHeaderProps) { const imageService = getImageService(); const coverUrl = imageService.getLeagueCover(leagueId); const logoUrl = imageService.getLeagueLogo(leagueId); const [ownerDriver, setOwnerDriver] = useState(null); useEffect(() => { let isMounted = true; async function loadOwner() { try { const driverRepo = getDriverRepository(); const entity = await driverRepo.findById(ownerId); if (!entity || !isMounted) return; setOwnerDriver(EntityMappers.toDriverDTO(entity)); } catch (err) { console.error('Failed to load league owner for header:', err); } } loadOwner(); return () => { isMounted = false; }; }, [ownerId]); const ownerSummary = useMemo(() => { if (!ownerDriver) { return null; } const stats = getDriverStats(ownerDriver.id); const allRankings = getAllDriverRankings(); let rating: number | null = stats?.rating ?? null; let rank: number | null = null; if (stats) { if (typeof stats.overallRank === 'number' && stats.overallRank > 0) { rank = stats.overallRank; } else { const indexInGlobal = allRankings.findIndex( (stat) => stat.driverId === stats.driverId, ); if (indexInGlobal !== -1) { rank = indexInGlobal + 1; } } if (rating === null) { const globalEntry = allRankings.find( (stat) => stat.driverId === stats.driverId, ); if (globalEntry) { rating = globalEntry.rating; } } } return { driver: ownerDriver, rating, rank, }; }, [ownerDriver]); return (

{leagueName}

Alpha: Single League
{description && (

{description}

)}
Owner {ownerSummary ? (
) : (
{ownerName}
)}
); }