import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "~/components/ui/card";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "~/components/ui/table";
import { Badge } from "~/components/ui/badge";
import { TrendingUp, TrendingDown, Minus, Flag, CheckCircle2, Star } from "lucide-react";
import { TeamOwnerBadge } from "~/components/ui/team-owner-badge";
interface SeasonStanding {
id: string;
championshipPoints: string; // Decimal as string
position: number;
previousPosition?: number | null; // For showing movement
participant: {
id: string;
name: string;
};
}
interface TeamOwnership {
participantId: string;
teamName: string;
teamId: string;
ownerName?: string;
}
interface SeasonStandingsProps {
standings: SeasonStanding[];
teamOwnerships?: TeamOwnership[]; // Which teams own which participants
userParticipantIds?: string[]; // Participants drafted by the current user
showOwnership?: boolean;
isFinalized?: boolean; // Whether season is complete
title?: string;
description?: string;
}
/**
* SeasonStandings component - Displays F1-style championship standings
*
* Features:
* - Shows participants ranked by championship points
* - Positions auto-calculated from points (highest = 1st)
* - Optional ownership hints with team avatars
* - Movement indicators (position changes)
* - Handles ties (same points = same position)
*/
function getMovementIndicator(
currentPosition: number,
previousPosition?: number | null
) {
if (!previousPosition) return null;
const change = previousPosition - currentPosition; // Positive means moved up
if (change > 0) {
return (
+{change}
);
} else if (change < 0) {
return (
{change}
);
} else {
return (
);
}
}
function getPositionBadge(position: number, isTied: boolean) {
const suffix = position === 1 ? "st" : position === 2 ? "nd" : position === 3 ? "rd" : "th";
const positionText = isTied ? `T${position}` : `${position}${suffix}`;
return {positionText};
}
export function SeasonStandings({
standings,
teamOwnerships = [],
userParticipantIds = [],
showOwnership = true,
isFinalized = false,
title = "Championship Standings",
description: _description,
}: SeasonStandingsProps) {
const userParticipantSet = new Set(userParticipantIds);
// Create ownership map for fast lookup
const ownershipMap = new Map();
teamOwnerships.forEach((ownership) => {
ownershipMap.set(ownership.participantId, ownership);
});
// Get ownership info for a participant
const getOwnership = (participantId: string): TeamOwnership | null => {
if (!showOwnership) return null;
return ownershipMap.get(participantId) || null;
};
// Check if multiple participants share the same position
const checkIfTied = (standing: SeasonStanding): boolean => {
return standings.some(
(other) =>
other.id !== standing.id && other.position === standing.position
);
};
// Count top 8 finishers (those who will get fantasy points)
const top8Count = standings.filter((s) => s.position <= 8).length;
return (
{title}
{isFinalized ? (
Season complete - Fantasy points assigned to top 8 finishers
) : (
<>
Current championship standings. Positions calculated from points
(highest = 1st).
{top8Count > 0 && (
Top 8 finishers will receive fantasy points when season completes.
)}
>
)}
{standings.length === 0 ? (
No standings data available yet.
Championship points will appear here once results are entered.
) : (
<>
Pos
{standings.some((s) => s.previousPosition) && (
Change
)}
Participant
Points
{showOwnership && (
Drafted By
)}
{standings
.toSorted((a, b) => a.position - b.position)
.map((standing) => {
const ownership = getOwnership(standing.participant.id);
const isTied = checkIfTied(standing);
const isTop8 = standing.position <= 8;
const isOwned = userParticipantSet.has(standing.participant.id);
let rowClass = "";
if (isOwned && isTop8) {
rowClass = "bg-electric/8 border-l-2 border-l-electric font-medium";
} else if (isOwned && !isTop8) {
rowClass = "bg-muted/20 border-l-2 border-l-muted-foreground/40 opacity-80";
} else if (isTop8) {
rowClass = standing.position <= 3 ? "bg-muted/30 font-medium" : "";
} else {
rowClass = "opacity-60";
}
return (
{getPositionBadge(standing.position, isTied)}
{standings.some((s) => s.previousPosition) && (
{getMovementIndicator(
standing.position,
standing.previousPosition
)}
)}
{standing.participant.name}
{isOwned && (
)}
{Math.round(parseFloat(standing.championshipPoints))}
{showOwnership && (
{ownership ? (
) : (
-
)}
)}
);
})}
{!isFinalized && top8Count > 0 && (
{top8Count}
{" "}
participant{top8Count !== 1 ? "s" : ""} currently in top 8 will
receive fantasy points when season completes.
)}
>
)}
);
}