|
| 1 | +import { useQueries } from '@tanstack/react-query'; |
| 2 | +import { Link } from 'react-router-dom'; |
| 3 | +import { fetchCompetition, WcaCompetitionResult } from '@/lib/api'; |
| 4 | +import { getCompetitionResultSummaries, getPersonResultsPath } from '../userProfileData'; |
| 5 | + |
| 6 | +interface CompetitionsTabProps { |
| 7 | + competitions: ApiCompetition[]; |
| 8 | + assignmentStatus: Record<string, boolean> | undefined; |
| 9 | + isCheckingAssignments: boolean; |
| 10 | + results: WcaCompetitionResult[] | undefined; |
| 11 | + isLoadingResults: boolean; |
| 12 | + wcaId?: string; |
| 13 | +} |
| 14 | + |
| 15 | +const formatCompetitionDates = (competition: ApiCompetition) => { |
| 16 | + const start = new Date(`${competition.start_date}T00:00:00`); |
| 17 | + const end = new Date(`${competition.end_date}T00:00:00`); |
| 18 | + |
| 19 | + if (competition.start_date === competition.end_date) { |
| 20 | + return start.toLocaleDateString(undefined, { |
| 21 | + month: 'short', |
| 22 | + day: 'numeric', |
| 23 | + year: 'numeric', |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + return `${start.toLocaleDateString(undefined, { |
| 28 | + month: 'short', |
| 29 | + day: 'numeric', |
| 30 | + })} - ${end.toLocaleDateString(undefined, { |
| 31 | + month: 'short', |
| 32 | + day: 'numeric', |
| 33 | + year: 'numeric', |
| 34 | + })}`; |
| 35 | +}; |
| 36 | + |
| 37 | +export function CompetitionsTab({ |
| 38 | + competitions, |
| 39 | + assignmentStatus, |
| 40 | + isCheckingAssignments, |
| 41 | + results, |
| 42 | + isLoadingResults, |
| 43 | + wcaId, |
| 44 | +}: CompetitionsTabProps) { |
| 45 | + const pastCompetitionSummaries = getCompetitionResultSummaries(results || []).sort( |
| 46 | + (a, b) => b.latestResultId - a.latestResultId, |
| 47 | + ); |
| 48 | + const pastCompetitionDetails = useQueries({ |
| 49 | + queries: pastCompetitionSummaries.map((summary) => ({ |
| 50 | + queryKey: ['competition', summary.competitionId], |
| 51 | + queryFn: () => fetchCompetition(summary.competitionId), |
| 52 | + enabled: !isLoadingResults, |
| 53 | + staleTime: 60 * 60 * 1000, |
| 54 | + gcTime: 24 * 60 * 60 * 1000, |
| 55 | + })), |
| 56 | + }); |
| 57 | + const pastCompetitionDetailsById = new Map( |
| 58 | + pastCompetitionDetails |
| 59 | + .map((query) => query.data) |
| 60 | + .filter((competition): competition is ApiCompetition => Boolean(competition)) |
| 61 | + .map((competition) => [competition.id, competition]), |
| 62 | + ); |
| 63 | + const sortedPastCompetitionSummaries = [...pastCompetitionSummaries].sort((a, b) => { |
| 64 | + const aCompetition = pastCompetitionDetailsById.get(a.competitionId); |
| 65 | + const bCompetition = pastCompetitionDetailsById.get(b.competitionId); |
| 66 | + |
| 67 | + if (aCompetition && bCompetition) { |
| 68 | + return ( |
| 69 | + new Date(bCompetition.start_date).getTime() - new Date(aCompetition.start_date).getTime() |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return b.latestResultId - a.latestResultId; |
| 74 | + }); |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="space-y-4"> |
| 78 | + <section className="space-y-2"> |
| 79 | + <h2 className="type-label text-default">Upcoming competitions</h2> |
| 80 | + {competitions.length === 0 ? ( |
| 81 | + <p className="type-body-sm text-muted">No upcoming competitions.</p> |
| 82 | + ) : ( |
| 83 | + competitions.map((competition) => { |
| 84 | + const hasAssignments = assignmentStatus?.[competition.id]; |
| 85 | + const statusText = |
| 86 | + hasAssignments == null && isCheckingAssignments |
| 87 | + ? 'Checking assignments' |
| 88 | + : hasAssignments |
| 89 | + ? 'Assignments generated' |
| 90 | + : 'No assignments yet'; |
| 91 | + |
| 92 | + return ( |
| 93 | + <Link |
| 94 | + key={competition.id} |
| 95 | + to={`/competitions/${competition.id}`} |
| 96 | + className="block rounded-md border border-tertiary-weak bg-panel px-3 py-2 shadow-sm hover-bg-tertiary"> |
| 97 | + <div className="flex items-start justify-between gap-3"> |
| 98 | + <div className="min-w-0 space-y-1"> |
| 99 | + <div className="type-label text-default">{competition.name}</div> |
| 100 | + <div className="type-body-sm text-subtle"> |
| 101 | + {competition.city}, {competition.country_iso2} -{' '} |
| 102 | + {formatCompetitionDates(competition)} |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + <span |
| 106 | + className={ |
| 107 | + hasAssignments |
| 108 | + ? 'shrink-0 text-right type-body-sm text-green-600' |
| 109 | + : 'shrink-0 text-right type-body-sm text-muted' |
| 110 | + }> |
| 111 | + {statusText} |
| 112 | + </span> |
| 113 | + </div> |
| 114 | + </Link> |
| 115 | + ); |
| 116 | + }) |
| 117 | + )} |
| 118 | + </section> |
| 119 | + |
| 120 | + <section className="space-y-2"> |
| 121 | + <h2 className="type-label text-default">Past competitions</h2> |
| 122 | + {isLoadingResults ? ( |
| 123 | + <p className="type-body-sm text-muted">Loading past competitions...</p> |
| 124 | + ) : sortedPastCompetitionSummaries.length === 0 ? ( |
| 125 | + <p className="type-body-sm text-muted">No past competitions.</p> |
| 126 | + ) : ( |
| 127 | + sortedPastCompetitionSummaries.map((summary) => { |
| 128 | + const to = getPersonResultsPath(summary.competitionId, wcaId); |
| 129 | + const competition = pastCompetitionDetailsById.get(summary.competitionId); |
| 130 | + const content = ( |
| 131 | + <div className="min-w-0 space-y-1"> |
| 132 | + <div className="type-label text-default"> |
| 133 | + {competition?.name || summary.competitionId} |
| 134 | + </div> |
| 135 | + <div className="type-body-sm text-subtle"> |
| 136 | + {competition ? formatCompetitionDates(competition) : 'Loading dates...'} |
| 137 | + </div> |
| 138 | + </div> |
| 139 | + ); |
| 140 | + |
| 141 | + if (!to) { |
| 142 | + return ( |
| 143 | + <div |
| 144 | + key={summary.competitionId} |
| 145 | + className="rounded-md border border-tertiary-weak bg-panel px-3 py-2 shadow-sm"> |
| 146 | + {content} |
| 147 | + </div> |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + return ( |
| 152 | + <Link |
| 153 | + key={summary.competitionId} |
| 154 | + to={to} |
| 155 | + className="block rounded-md border border-tertiary-weak bg-panel px-3 py-2 shadow-sm hover-bg-tertiary"> |
| 156 | + {content} |
| 157 | + </Link> |
| 158 | + ); |
| 159 | + }) |
| 160 | + )} |
| 161 | + </section> |
| 162 | + </div> |
| 163 | + ); |
| 164 | +} |
0 commit comments