import { useMemo, memo } from "react";
import {
Stack,
Text,
Group,
UnstyledButton,
Container,
Box,
Center,
ThemeIcon,
Divider,
Alert,
} from "@mantine/core";
import { Tournament } from "@/features/tournaments/types";
import { CrownIcon, TreeStructureIcon, InfoIcon, ListDashes } from "@phosphor-icons/react";
import TeamAvatar from "@/components/team-avatar";
import ListLink from "@/components/list-link";
import { Podium } from "./podium";
interface TournamentStatsProps {
tournament: Tournament;
}
export const TournamentStats = memo(({ tournament }: TournamentStatsProps) => {
const matches = tournament.matches || [];
const nonByeMatches = useMemo(() =>
matches.filter((match) => !(match.status === 'tbd' && match.bye === true)),
[matches]
);
const isComplete = useMemo(() =>
nonByeMatches.length > 0 && nonByeMatches.every((match) => match.status === 'ended'),
[nonByeMatches]
);
const hasGroupStage = useMemo(() => {
return tournament.matches?.some((match) => match.round === -1) || false;
}, [tournament.matches]);
const sortedTeamStats = useMemo(() => {
return [...(tournament.team_stats || [])].sort((a, b) => {
if (b.wins !== a.wins) {
return b.wins - a.wins;
}
return b.total_cups_made - a.total_cups_made;
});
}, [tournament.team_stats]);
const teamStatsWithCalculations = useMemo(() => {
return sortedTeamStats.map((stat) => ({
...stat,
winPercentage: stat.matches > 0 ? (stat.wins / stat.matches) * 100 : 0,
avgCupsPerMatch: stat.matches > 0 ? stat.total_cups_made / stat.matches : 0,
})).sort((a, b) => b.winPercentage - a.winPercentage);;
}, [sortedTeamStats]);
const renderTeamStatsTable = () => {
if (!teamStatsWithCalculations.length) {
return (
No stats available yet
);
}
return (
Results
Sorted by win percentage
{teamStatsWithCalculations.map((stat, index) => {
const team = tournament.teams?.find(t => t.id === stat.team_id);
return (
{team ? (
) : (
)}
#{index + 1}
{stat.team_name}
{index === 0 && isComplete && (
)}
W
{stat.wins}
L
{stat.losses}
W%
{stat.winPercentage.toFixed(1)}%
AVG
{stat.avgCupsPerMatch.toFixed(1)}
CF
{stat.total_cups_made}
CA
{stat.total_cups_against}
{index < teamStatsWithCalculations.length - 1 && }
);
})}
);
};
return (
{tournament.regional && !hasGroupStage && (
}>
Earlier regional formats aren't supported in the app and order of matches or displayed winners may be unreliable.
)}
{!tournament.regional && }
{hasGroupStage && (
)}
{renderTeamStatsTable()}
);
});