import React, { useCallback } from "react"; import { Card, Group, Stack, Text, ActionIcon, Indicator, Flex, Box } from "@mantine/core"; import { PlayIcon, PencilIcon } from "@phosphor-icons/react"; import { Match } from "@/features/matches/types"; import { useSheet } from "@/hooks/use-sheet"; import Sheet from "@/components/sheet/sheet"; import { useServerMutation } from "@/lib/tanstack-query/hooks"; import { endMatch, startMatch } from "@/features/matches/server"; import { tournamentKeys } from "@/features/tournaments/queries"; import { useQueryClient } from "@tanstack/react-query"; import { MatchForm } from "@/features/bracket/components/match-form"; import TeamAvatar from "@/components/team-avatar"; interface GroupMatchCardProps { match: Match; showControls?: boolean; } const GroupMatchCard: React.FC = ({ match, showControls }) => { const queryClient = useQueryClient(); const editSheet = useSheet(); const isReady = match.status === "ready"; const isStarted = match.status === "started"; const isEnded = match.status === "ended"; const homeWon = isEnded && match.home_cups !== undefined && match.away_cups !== undefined && match.home_cups > match.away_cups; const awayWon = isEnded && match.away_cups !== undefined && match.home_cups !== undefined && match.away_cups > match.home_cups; const start = useServerMutation({ mutationFn: startMatch, successMessage: "Match started!", onSuccess: () => { queryClient.invalidateQueries({ queryKey: tournamentKeys.details(match.tournament.id), }); }, }); const end = useServerMutation({ mutationFn: endMatch, onSuccess: () => { queryClient.invalidateQueries({ queryKey: tournamentKeys.details(match.tournament.id), }); editSheet.close(); }, }); const handleFormSubmit = useCallback( async (data: { home_cups: number; away_cups: number; ot_count: number; }) => { end.mutate({ data: { ...data, matchId: match.id, }, }); }, [end, match.id] ); const handleStartMatch = () => { start.mutate({ data: match.id }); }; const showStartButton = isReady && showControls; const showEditButton = isStarted && showControls; return ( <> {match.home && } {match.home?.name || "TBD"} {isEnded && match.home_cups !== undefined && ( {match.home_cups} )} {match.away && } {match.away?.name || "TBD"} {isEnded && match.away_cups !== undefined && ( {match.away_cups} )} {showStartButton && ( )} {showEditButton && ( )} {showControls && ( )} ); }; export default GroupMatchCard;