import { Stack, Text, Group, Box, Divider, Paper } from "@mantine/core";
import { TeamInfo } from "@/features/teams/types";
import { useTeamHeadToHead } from "../queries";
import { useMemo, useEffect, useState, Suspense } from "react";
import { CrownIcon } from "@phosphor-icons/react";
import MatchList from "./match-list";
import TeamHeadToHeadSkeleton from "./team-head-to-head-skeleton";
interface TeamHeadToHeadSheetProps {
team1: TeamInfo;
team2: TeamInfo;
isOpen?: boolean;
}
const TeamHeadToHeadContent = ({ team1, team2, isOpen = true }: TeamHeadToHeadSheetProps) => {
const [shouldFetch, setShouldFetch] = useState(false);
useEffect(() => {
if (isOpen && !shouldFetch) {
setShouldFetch(true);
}
}, [isOpen, shouldFetch]);
const { data: matches, isLoading } = useTeamHeadToHead(team1.id, team2.id, shouldFetch);
const stats = useMemo(() => {
if (!matches || matches.length === 0) {
return {
team1Wins: 0,
team2Wins: 0,
team1CupsFor: 0,
team2CupsFor: 0,
team1CupsAgainst: 0,
team2CupsAgainst: 0,
team1AvgMargin: 0,
team2AvgMargin: 0,
};
}
let team1Wins = 0;
let team2Wins = 0;
let team1CupsFor = 0;
let team2CupsFor = 0;
let team1CupsAgainst = 0;
let team2CupsAgainst = 0;
let team1TotalWinMargin = 0;
let team2TotalWinMargin = 0;
matches.forEach((match) => {
const isTeam1Home = match.home?.id === team1.id;
const team1Cups = isTeam1Home ? match.home_cups : match.away_cups;
const team2Cups = isTeam1Home ? match.away_cups : match.home_cups;
if (team1Cups > team2Cups) {
team1Wins++;
team1TotalWinMargin += (team1Cups - team2Cups);
} else if (team2Cups > team1Cups) {
team2Wins++;
team2TotalWinMargin += (team2Cups - team1Cups);
}
team1CupsFor += team1Cups;
team2CupsFor += team2Cups;
team1CupsAgainst += team2Cups;
team2CupsAgainst += team1Cups;
});
const team1AvgMargin = team1Wins > 0
? team1TotalWinMargin / team1Wins
: 0;
const team2AvgMargin = team2Wins > 0
? team2TotalWinMargin / team2Wins
: 0;
return {
team1Wins,
team2Wins,
team1CupsFor,
team2CupsFor,
team1CupsAgainst,
team2CupsAgainst,
team1AvgMargin,
team2AvgMargin,
};
}, [matches, team1.id]);
if (isLoading) {
return (
Loading...
);
}
if (!matches || matches.length === 0) {
return (
These teams have not faced each other yet.
);
}
const totalMatches = stats.team1Wins + stats.team2Wins;
const leader = stats.team1Wins > stats.team2Wins ? team1 : stats.team2Wins > stats.team1Wins ? team2 : null;
return (
{team1.name}
vs
{team2.name}
{stats.team1Wins}
{team1.name}
-
{stats.team2Wins}
{team2.name}
{leader && (
{leader.name} leads the series
)}
{!leader && totalMatches > 0 && (
Series is tied
)}
Stats Comparison
{stats.team1CupsFor}
cups
Total Cups
cups
{stats.team2CupsFor}
{totalMatches > 0 ? (stats.team1CupsFor / totalMatches).toFixed(1) : '0.0'}
avg
Avg Cups/Match
avg
{totalMatches > 0 ? (stats.team2CupsFor / totalMatches).toFixed(1) : '0.0'}
{!isNaN(stats.team1AvgMargin) ? stats.team1AvgMargin.toFixed(1) : '0.0'}
margin
Avg Win Margin
margin
{!isNaN(stats.team2AvgMargin) ? stats.team2AvgMargin.toFixed(1) : '0.0'}
Match History ({totalMatches} match{totalMatches !== 1 ? 'es' : ''})
);
};
const TeamHeadToHeadSheet = (props: TeamHeadToHeadSheetProps) => {
return (
}>
);
};
export default TeamHeadToHeadSheet;