import { Link } from "react-router"; import { useState } from "react"; import type { Route } from "./+types/$leagueId.sports-seasons.$sportsSeasonId"; import { loader } from "./$leagueId.sports-seasons.$sportsSeasonId.server"; import { SportSeasonDisplay } from "~/components/scoring/SportSeasonDisplay"; import { EventSchedule } from "~/components/sport-season/EventSchedule"; import { RegularSeasonStandings } from "~/components/sport-season/RegularSeasonStandings"; import { GroupStageStandings } from "~/components/sport-season/GroupStageStandings"; import { Button } from "~/components/ui/button"; import { cn } from "~/lib/utils"; import { ArrowLeft } from "lucide-react"; export function meta({ data }: Route.MetaArgs): Route.MetaDescriptors { return [{ title: `${data?.sportsSeason?.name ?? "Sport Season"} — ${data?.league?.name ?? "League"} - Brackt` }]; } export { loader }; type BracketView = "standings" | "playoffs" | "finished" | "groups"; export default function SportSeasonDetail({ loaderData, }: Route.ComponentProps) { const { league, season, sportsSeason, scoringPattern, playoffMatches, playoffRounds, preEliminatedParticipants, participantPoints, partialScoreParticipantIds, seasonStandings, qpStandings, teamOwnerships, userParticipantIds, upcomingEvents, recentEvents, regularSeasonStandings, groupStandings, bracketTemplateId, } = loaderData; const hasBracket = playoffMatches && playoffMatches.length > 0; const hasStandings = regularSeasonStandings && regularSeasonStandings.length > 0; const showOtLosses = hasStandings && regularSeasonStandings.some((s) => s.otLosses !== null); const simulatorType = sportsSeason.sport?.simulatorType; const standingsDisplayMode = simulatorType === "nhl_bracket" ? "nhl-divisions" : simulatorType === "mlb_bracket" ? "mlb-divisions" : "flat"; const playoffSpots = simulatorType === "nba_bracket" || simulatorType === "afl_bracket" ? 10 : simulatorType === "mls_bracket" ? 9 : 8; const ownershipMap = Object.fromEntries( teamOwnerships.map((o) => [ o.participantId, { teamName: o.teamName, ownerName: o.ownerName ?? "", teamId: o.teamId }, ]) ); // Show the 3-way toggle for bracket sports with regular season standings (NBA/AFL/etc.) const showToggle = hasStandings && scoringPattern === "playoff_bracket"; // Show Groups/Bracket toggle for tournaments with a group stage AND a knockout bracket. // Independent of showToggle — a sport can have both standings and group stage. const hasGroupStage = groupStandings.length > 0; const showGroupStageToggle = hasGroupStage && hasBracket; const showAnyToggle = showToggle || showGroupStageToggle; const [view, setView] = useState(() => { if (showGroupStageToggle) { return sportsSeason.status === "completed" ? "finished" : "playoffs"; } if (!hasBracket) return "standings"; if (sportsSeason.status === "completed") return "finished"; return "playoffs"; }); const TOGGLE_VIEWS: { value: BracketView; label: string }[] = showGroupStageToggle ? [ { value: "groups", label: "Groups" }, ...(showToggle ? [{ value: "standings" as BracketView, label: "Standings" }] : []), { value: "playoffs", label: "Bracket" }, ...(sportsSeason.status === "completed" ? [{ value: "finished" as BracketView, label: "Final" }] : []), ] : [ { value: "standings", label: "Standings" }, { value: "playoffs", label: "Playoffs" }, { value: "finished", label: "Finished" }, ]; const bracketDisplay = (bracketMode: "bracket" | "rankings") => ( = (sportsSeason.totalMajors || 0) && !sportsSeason.qualifyingPointsFinalized } teamOwnerships={teamOwnerships} userParticipantIds={userParticipantIds} scoringRules={season} showOwnership={true} /> ); const standingsDisplay = ( ); return (

{sportsSeason.sport.name}

{sportsSeason.name} • {league.name} • {season.year} Season

{showAnyToggle && (
{TOGGLE_VIEWS.map(({ value, label }) => ( ))}
)}
{showAnyToggle ? (
{view === "groups" && ( )} {view === "standings" && standingsDisplay} {view === "playoffs" && bracketDisplay("bracket")} {view === "finished" && bracketDisplay("rankings")}
) : ( <> {/* Regular season standings above bracket when no bracket exists yet */} {hasStandings && !hasBracket && (
{standingsDisplay}
)} {/* Event schedule — hidden for bracket sports */} {scoringPattern !== "playoff_bracket" && (upcomingEvents.length > 0 || recentEvents.length > 0) && (
)} {/* Group stage standings */} {groupStandings.length > 0 && (
)} {/* Bracket — hidden when standings exist but bracket is empty */} {!(scoringPattern === "playoff_bracket" && hasStandings && !hasBracket) && bracketDisplay("bracket")} {/* Regular season standings below bracket once bracket exists */} {hasStandings && hasBracket && (
{standingsDisplay}
)} )}
); }