import { useCallback, useState } from "react"; import { Alert, Button, Flex, Group, Paper, Stack, Text } from "@mantine/core"; import { CheckIcon, LightningIcon } from "@phosphor-icons/react"; import { Match } from "@/features/matches/types"; import { useAuth } from "@/contexts/auth-context"; import { useReportMatchScore, useConfirmMatchScore, useClearMatchReport, } from "@/features/matches/queries"; import { useSheet } from "@/hooks/use-sheet"; import Sheet from "@/components/sheet/sheet"; import { MatchForm } from "./match-form"; import toast from "@/lib/sonner"; import { Trans, useLingui } from "@lingui/react/macro"; const teamId = (t: Match["home"]): string | undefined => !t ? undefined : typeof t === "string" ? t : t.id; const teamPlayerIds = (t: Match["home"]): string[] => t && typeof t !== "string" ? (t.players ?? []).map((p) => p.id) : []; const teamName = (t: Match["home"]): string | undefined => t && typeof t !== "string" ? t.name : undefined; interface MatchReportProps { match: Match; compact?: boolean; } export const MatchReport: React.FC = ({ match, compact }) => { const { user } = useAuth(); const formSheet = useSheet(); const { t } = useLingui(); const [acknowledgedReport, setAcknowledgedReport] = useState( null ); const homeId = teamId(match.home); const onHome = !!user?.id && teamPlayerIds(match.home).includes(user.id); const onAway = !!user?.id && teamPlayerIds(match.away).includes(user.id); const isParticipant = onHome || onAway; const hasPending = match.reported_by_team != null && match.reported_home_cups != null && match.reported_away_cups != null; const reportedByHome = hasPending && match.reported_by_team === homeId; const reportingTeam = hasPending ? reportedByHome ? match.home : match.away : undefined; const opposingTeam = reportedByHome ? match.away : match.home; const reportedByMine = hasPending && ((reportedByHome && onHome) || (!reportedByHome && onAway)); const pendingSignature = hasPending ? [ match.reported_by_team, match.reported_home_cups, match.reported_away_cups, match.reported_ot_count ?? 0, ].join(":") : null; const sheetTakenOver = formSheet.isOpen && isParticipant && hasPending && !reportedByMine && pendingSignature !== acknowledgedReport; const report = useReportMatchScore(user?.id, { onSuccess: (data: any) => { if (data?.finalized) { toast.success(t`Score confirmed ๐Ÿป`); if (data.downstreamReset) { toast.error( t`Downstream matches were reset because the correction changed who advances.` ); } if (data.groupEditAfterKnockout) { toast.error( t`Group result changed after the bracket was seeded โ€” reseed the knockout stage if needed.` ); } } }, }); const confirm = useConfirmMatchScore({ onSuccess: (data: any) => { if (data?.downstreamReset) { toast.error( t`Downstream matches were reset because the correction changed who advances.` ); } if (data?.groupEditAfterKnockout) { toast.error( t`Group result changed after the bracket was seeded โ€” reseed the knockout stage if needed.` ); } }, }); const clear = useClearMatchReport(); const submitReport = useCallback( (data: { home_cups: number; away_cups: number; ot_count: number }) => { report.mutate({ data: { ...data, matchId: match.id } }); formSheet.close(); toast.success(t`Score submitted โ€” waiting for the other team`); }, [report, match.id, formSheet.close, t] ); const submitConfirm = useCallback(() => { confirm.mutate({ data: { matchId: match.id } }); formSheet.close(); toast.success(t`Score confirmed ๐Ÿป`); }, [confirm, match.id, formSheet.close, t]); const submitClear = useCallback(() => { clear.mutate({ data: { matchId: match.id } }); toast.success(t`Report cleared`); }, [clear, match.id, t]); const openForm = useCallback(() => { setAcknowledgedReport(pendingSignature); formSheet.open(); }, [pendingSignature, formSheet.open]); const dismissTakeover = useCallback(() => { setAcknowledgedReport(pendingSignature); }, [pendingSignature]); if (match.status !== "started") return null; const scoreText = hasPending ? `${match.reported_home_cups}โ€“${match.reported_away_cups}` : ""; const size = compact ? "compact-xs" : "sm"; const formSheetEl = ( {sheetTakenOver ? ( } title={t`${teamName(reportingTeam) ?? t`The other team`} just reported a score`} > Does this look right? {teamName(match.home) ?? t({ message: "Home", context: "match team" })} {match.reported_home_cups} โ€“ {teamName(match.away) ?? t({ message: "Away", context: "match team" })} {match.reported_away_cups} ) : ( )} ); if (isParticipant && !hasPending) { return ( <> {formSheetEl} ); } if (hasPending) { if (reportedByMine) { return ( Waiting for {teamName(opposingTeam) ?? t`the other team`} to confirm{" "} ยท{" "} {scoreText} ); } if (isParticipant) { return ( <> {teamName(reportingTeam) ?? t`Opponent`} reported{" "} {scoreText} {formSheetEl} ); } return ( Pending: {scoreText} ยท reported by {teamName(reportingTeam) ?? t`a team`} ); } return null; };