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