'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import LeagueCard from '@/components/leagues/LeagueCard'; import CreateLeagueForm from '@/components/leagues/CreateLeagueForm'; import Button from '@/components/ui/Button'; import Card from '@/components/ui/Card'; import Input from '@/components/ui/Input'; import { League } from '@gridpilot/racing/domain/entities/League'; import { getLeagueRepository } from '@/lib/di-container'; export default function LeaguesPage() { const router = useRouter(); const [leagues, setLeagues] = useState([]); const [loading, setLoading] = useState(true); const [showCreateForm, setShowCreateForm] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const [sortBy, setSortBy] = useState('name'); useEffect(() => { loadLeagues(); }, []); const loadLeagues = async () => { try { const leagueRepo = getLeagueRepository(); const allLeagues = await leagueRepo.findAll(); setLeagues(allLeagues); } catch (error) { console.error('Failed to load leagues:', error); } finally { setLoading(false); } }; const handleLeagueClick = (leagueId: string) => { router.push(`/leagues/${leagueId}`); }; const filteredLeagues = leagues .filter((league) => { const matchesSearch = league.name.toLowerCase().includes(searchQuery.toLowerCase()) || league.description.toLowerCase().includes(searchQuery.toLowerCase()); return matchesSearch; }) .sort((a, b) => { switch (sortBy) { case 'name': return a.name.localeCompare(b.name); case 'recent': return new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime(); default: return 0; } }); if (loading) { return (
Loading leagues...
); } return (

Leagues

{leagues.length === 0 ? 'Create your first league to get started' : `${leagues.length} ${leagues.length === 1 ? 'league' : 'leagues'} available`}

{showCreateForm && (

Create New League

Experiment with different point systems

)} {leagues.length > 0 && (
setSearchQuery(e.target.value)} />
)} {leagues.length === 0 ? (

No leagues yet

Create one to get started. Alpha data resets on page reload.

) : ( <>

{filteredLeagues.length} {filteredLeagues.length === 1 ? 'league' : 'leagues'} found

{filteredLeagues.map((league) => ( handleLeagueClick(league.id)} /> ))}
{filteredLeagues.length === 0 && (

No leagues found matching your search.

)} )}
); }