'use client'; import React, { useState, useEffect } from 'react'; import { useParams, usePathname, useRouter } from 'next/navigation'; import Breadcrumbs from '@/components/layout/Breadcrumbs'; import LeagueHeader from '@/components/leagues/LeagueHeader'; import { getLeagueRepository, getDriverRepository, getLeagueMembershipRepository } from '@/lib/di-container'; import { useEffectiveDriverId } from '@/lib/currentDriver'; import { isLeagueAdminOrHigherRole } from '@/lib/leagueRoles'; import type { League } from '@gridpilot/racing/domain/entities/League'; export default function LeagueLayout({ children, }: { children: React.ReactNode; }) { const params = useParams(); const pathname = usePathname(); const router = useRouter(); const leagueId = params.id as string; const currentDriverId = useEffectiveDriverId(); const [league, setLeague] = useState(null); const [ownerName, setOwnerName] = useState(''); const [isAdmin, setIsAdmin] = useState(false); const [loading, setLoading] = useState(true); useEffect(() => { async function loadLeague() { try { const leagueRepo = getLeagueRepository(); const driverRepo = getDriverRepository(); const membershipRepo = getLeagueMembershipRepository(); const leagueData = await leagueRepo.findById(leagueId); if (!leagueData) { setLoading(false); return; } setLeague(leagueData); const owner = await driverRepo.findById(leagueData.ownerId); setOwnerName(owner ? owner.name : `${leagueData.ownerId.slice(0, 8)}...`); // Check if current user is admin const membership = await membershipRepo.getMembership(leagueId, currentDriverId); setIsAdmin(membership ? isLeagueAdminOrHigherRole(membership.role) : false); } catch (error) { console.error('Failed to load league:', error); } finally { setLoading(false); } } loadLeague(); }, [leagueId, currentDriverId]); if (loading) { return (
Loading league...
); } if (!league) { return (
League not found
); } // Define tab configuration const baseTabs = [ { label: 'Overview', href: `/leagues/${leagueId}`, exact: true }, { label: 'Schedule', href: `/leagues/${leagueId}/schedule`, exact: false }, { label: 'Standings', href: `/leagues/${leagueId}/standings`, exact: false }, { label: 'Rulebook', href: `/leagues/${leagueId}/rulebook`, exact: false }, ]; const adminTabs = [ { label: 'Stewarding', href: `/leagues/${leagueId}/stewarding`, exact: false }, { label: 'Settings', href: `/leagues/${leagueId}/settings`, exact: false }, ]; const tabs = isAdmin ? [...baseTabs, ...adminTabs] : baseTabs; // Determine active tab const activeTab = tabs.find(tab => tab.exact ? pathname === tab.href : pathname.startsWith(tab.href) ); return (
{/* Tab Navigation */}
{tabs.map((tab) => ( ))}
{children}
); }