import { Link } from "react-router"; import { getAuth } from "@clerk/react-router/server"; import { findLeagueById } from "~/models/league"; import { findCurrentSeason } from "~/models/season"; import { findTeamsBySeasonId } from "~/models/team"; import type { Route } from "./+types/$leagueId"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "~/components/ui/card"; import { Button } from "~/components/ui/button"; export async function loader(args: Route.LoaderArgs) { const { userId } = await getAuth(args); const { params } = args; const { leagueId } = params; // Fetch league const league = await findLeagueById(leagueId); if (!league) { throw new Response("League not found", { status: 404 }); } // Fetch current season const season = await findCurrentSeason(leagueId); // Fetch teams for current season const teams = season ? await findTeamsBySeasonId(season.id) : []; return { league, season, teams, currentUserId: userId, }; } export default function LeagueHome({ loaderData }: Route.ComponentProps) { const { league, season, teams, currentUserId } = loaderData; return (

{league.name}

{season && (

{season.year} Season • Status:{" "} {season.status.replace("_", " ")}

)}
Teams {teams.length} team{teams.length !== 1 ? "s" : ""} in this league
{teams.map((team) => { const isOwned = team.ownerId === currentUserId; return ( {team.name} {team.ownerId ? ( isOwned ? ( Your Team ) : ( "Owned" ) ) : ( Available )} ); })}
League Info

Created

{new Date(league.createdAt).toLocaleDateString()}

{season && (

Season Status

{season.status.replace("_", " ")}

)}
); }