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