import type { CompetitionDetailResponse, SeriesProjection, } from "@lolympus/contracts"; import { ChevronDown, UsersRound } from "lucide-react"; import { useEffect, useRef } from "react"; import { Link, useParams, useSearchParams } from "react-router"; import { useCompetition, useTeamCompetition } from "../api/hooks"; import { TeamSeriesCard } from "../components/competition/TeamSeriesCard"; import { TeamTournamentCodes } from "../components/competition/TeamTournamentCodes"; import { DEFAULT_COMMUNITY_ID } from "../config"; import { positionLabel } from "../lib/league"; import { getTeamProgressErrorContent, isConsistentTeamProgressSnapshot, shouldOfferTeamSelection, type TeamProgressErrorContent, } from "./match-team-progress-state"; import { createTeamSeriesGroups, getTeamSeriesDisplayLabel, sortCompletedTeamSeries, sortTeamMembersForRoster, } from "./match-team-progress-view"; import { PageBody, PageHero, PlainPage } from "../ui"; export default function MatchTeamProgressPage() { const { id } = useParams(); const [searchParams] = useSearchParams(); const selectedTeamId = searchParams.get("teamId") ?? undefined; const isSelectingTeam = !selectedTeamId && searchParams.get("select") === "1"; const competitionQuery = useCompetition(id, DEFAULT_COMMUNITY_ID); const teamQuery = useTeamCompetition( id, DEFAULT_COMMUNITY_ID, selectedTeamId, ); const refetchCompetition = competitionQuery.refetch; const refetchTeam = teamQuery.refetch; const lastAutoRefetchedMismatchRef = useRef(null); const contentHeadingRef = useRef(null); const competition = competitionQuery.data; const teamView = teamQuery.data; const teamViewId = teamView?.teamId; const hasQueryError = Boolean( competitionQuery.error || teamQuery.error, ); const isRefetching = competitionQuery.isFetching || teamQuery.isFetching; const revisionsMatch = isConsistentTeamProgressSnapshot( competition, teamView, ); const revisionMismatchKey = competition && teamView && !revisionsMatch ? `${competition.revision}:${teamView.revision}` : null; useEffect(() => { if (!revisionMismatchKey) { lastAutoRefetchedMismatchRef.current = null; return; } if ( hasQueryError || isRefetching || lastAutoRefetchedMismatchRef.current === revisionMismatchKey ) { return; } lastAutoRefetchedMismatchRef.current = revisionMismatchKey; void Promise.all([refetchCompetition(), refetchTeam()]).catch( () => undefined, ); }, [ hasQueryError, isRefetching, refetchCompetition, refetchTeam, revisionMismatchKey, ]); useEffect(() => { if (isSelectingTeam || !teamViewId || !revisionsMatch) return; contentHeadingRef.current?.focus({ preventScroll: true }); }, [isSelectingTeam, revisionsMatch, teamViewId]); function retryBoth() { void Promise.all([refetchCompetition(), refetchTeam()]).catch( () => undefined, ); } if (isSelectingTeam && competition) { return ; } if (competitionQuery.isLoading || teamQuery.isLoading) { return ; } if (hasQueryError) { // The caller could not be resolved to a team from their identity — let them // name the team instead, so the page stays usable without signing in. if ( competition && !competitionQuery.error && shouldOfferTeamSelection([teamQuery.error], selectedTeamId) ) { return ; } return ( ); } if (!competition || !teamView) { return ( ); } if (!revisionsMatch) { return ( ); } const teamById = new Map( competition.teams.map((team) => [team.teamId, team]), ); const team = teamById.get(teamView.teamId); const activeSeriesGroups = createTeamSeriesGroups( teamView.activeSeries, teamView.tournamentCodes, ); const completedSeries = sortCompletedTeamSeries( competition.projection.series.filter( ( series, ): series is Extract => series.status === "completed" && series.entries.some((entry) => entry.teamId === teamView.teamId), ), ); const recentSeries = completedSeries.slice(0, 2); const olderSeries = completedSeries.slice(2); const rounds = competition.projection.rounds; return ( <> {team?.name ?? (selectedTeamId ? "팀" : "내 팀")} } meta="다음 상대와 세트별 토너먼트 코드를 한곳에서 확인할 수 있습니다." flush actions={