|
| 1 | +import classNames from 'classnames'; |
| 2 | +import { Link } from 'react-router-dom'; |
| 3 | +import { WcaCompetitionResult } from '@/lib/api'; |
| 4 | +import { |
| 5 | + formatUserResult, |
| 6 | + getCompetitionResultSummaries, |
| 7 | + getEventResultSummaries, |
| 8 | + getUserEventName, |
| 9 | + ResultsMode, |
| 10 | +} from '../userProfileData'; |
| 11 | + |
| 12 | +interface ResultsTabProps { |
| 13 | + results: WcaCompetitionResult[] | undefined; |
| 14 | + isLoading: boolean; |
| 15 | + error: unknown; |
| 16 | + mode: ResultsMode; |
| 17 | + onModeChange: (mode: ResultsMode) => void; |
| 18 | +} |
| 19 | + |
| 20 | +const modeOptions: { id: ResultsMode; label: string }[] = [ |
| 21 | + { id: 'competition', label: 'By competition' }, |
| 22 | + { id: 'event', label: 'By event' }, |
| 23 | +]; |
| 24 | + |
| 25 | +export function ResultsTab({ results, isLoading, error, mode, onModeChange }: ResultsTabProps) { |
| 26 | + if (isLoading) { |
| 27 | + return <p className="type-body-sm text-muted">Loading results...</p>; |
| 28 | + } |
| 29 | + |
| 30 | + if (error) { |
| 31 | + return <p className="type-body-sm text-red-600">Unable to load results.</p>; |
| 32 | + } |
| 33 | + |
| 34 | + if (!results || results.length === 0) { |
| 35 | + return <p className="type-body-sm text-muted">No results available.</p>; |
| 36 | + } |
| 37 | + |
| 38 | + const competitionSummaries = getCompetitionResultSummaries(results); |
| 39 | + const eventSummaries = getEventResultSummaries(results); |
| 40 | + |
| 41 | + return ( |
| 42 | + <div className="space-y-3"> |
| 43 | + <div className="grid grid-cols-2 rounded-md border border-tertiary-weak bg-panel p-1"> |
| 44 | + {modeOptions.map((option) => ( |
| 45 | + <button |
| 46 | + key={option.id} |
| 47 | + type="button" |
| 48 | + className={classNames('rounded px-2 py-2 text-center type-label hover-bg-tertiary', { |
| 49 | + 'bg-tertiary-strong text-blue-700 dark:text-blue-300': mode === option.id, |
| 50 | + })} |
| 51 | + onClick={() => onModeChange(option.id)}> |
| 52 | + {option.label} |
| 53 | + </button> |
| 54 | + ))} |
| 55 | + </div> |
| 56 | + |
| 57 | + {mode === 'competition' ? ( |
| 58 | + <div className="space-y-2"> |
| 59 | + {competitionSummaries.map((summary) => ( |
| 60 | + <Link |
| 61 | + key={summary.competitionId} |
| 62 | + to={`/competitions/${summary.competitionId}`} |
| 63 | + className="block rounded-md border border-tertiary-weak bg-panel px-3 py-2 shadow-sm hover-bg-tertiary"> |
| 64 | + <div className="flex items-start justify-between gap-3"> |
| 65 | + <div className="min-w-0 space-y-1"> |
| 66 | + <div className="type-label text-default">{summary.competitionId}</div> |
| 67 | + <div className="type-body-sm text-subtle"> |
| 68 | + {summary.roundCount} rounds across {summary.eventCount} events |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + <div className="shrink-0 text-right type-body-sm text-muted"> |
| 72 | + <div>{getUserEventName(summary.bestResult.event_id)}</div> |
| 73 | + <div> |
| 74 | + {formatUserResult( |
| 75 | + summary.bestResult.event_id, |
| 76 | + 'single', |
| 77 | + summary.bestResult.best, |
| 78 | + )} |
| 79 | + </div> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + </Link> |
| 83 | + ))} |
| 84 | + </div> |
| 85 | + ) : ( |
| 86 | + <div className="space-y-2"> |
| 87 | + {eventSummaries.map((summary) => ( |
| 88 | + <div |
| 89 | + key={summary.eventId} |
| 90 | + className="rounded-md border border-tertiary-weak bg-panel px-3 py-2 shadow-sm"> |
| 91 | + <div className="flex items-start justify-between gap-3"> |
| 92 | + <div className="min-w-0 space-y-1"> |
| 93 | + <div className="type-label text-default">{summary.eventName}</div> |
| 94 | + <div className="type-body-sm text-subtle"> |
| 95 | + {summary.roundCount} rounds at {summary.competitionCount} competitions |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + <div className="shrink-0 space-y-1 text-right type-body-sm text-muted"> |
| 99 | + <div> |
| 100 | + Single: {formatUserResult(summary.eventId, 'single', summary.bestSingle)} |
| 101 | + </div> |
| 102 | + <div> |
| 103 | + Average: {formatUserResult(summary.eventId, 'average', summary.bestAverage)} |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + ))} |
| 109 | + </div> |
| 110 | + )} |
| 111 | + </div> |
| 112 | + ); |
| 113 | +} |
0 commit comments