|
| 1 | +import { |
| 2 | + formatUserResult, |
| 3 | + getUserEventName, |
| 4 | + WcaPersonalRecords, |
| 5 | + WcaPersonalRecord, |
| 6 | +} from '../userProfileData'; |
| 7 | + |
| 8 | +interface RecordsTabProps { |
| 9 | + records: WcaPersonalRecords | undefined; |
| 10 | + isLoading: boolean; |
| 11 | + error: unknown; |
| 12 | +} |
| 13 | + |
| 14 | +type RankingType = 'single' | 'average'; |
| 15 | + |
| 16 | +const rankValue = (rank: number | undefined) => (rank ? rank.toLocaleString() : '-'); |
| 17 | + |
| 18 | +const resultValue = ( |
| 19 | + eventId: string, |
| 20 | + rankingType: RankingType, |
| 21 | + record: WcaPersonalRecord | undefined, |
| 22 | +) => formatUserResult(eventId, rankingType, record?.best); |
| 23 | + |
| 24 | +export function RecordsTab({ records, isLoading, error }: RecordsTabProps) { |
| 25 | + if (isLoading) { |
| 26 | + return <p className="type-body-sm text-muted">Loading records...</p>; |
| 27 | + } |
| 28 | + |
| 29 | + if (error) { |
| 30 | + return <p className="type-body-sm text-red-600">Unable to load records.</p>; |
| 31 | + } |
| 32 | + |
| 33 | + const entries = Object.entries(records || {}).sort(([a], [b]) => |
| 34 | + getUserEventName(a).localeCompare(getUserEventName(b)), |
| 35 | + ); |
| 36 | + |
| 37 | + if (entries.length === 0) { |
| 38 | + return <p className="type-body-sm text-muted">No records available.</p>; |
| 39 | + } |
| 40 | + |
| 41 | + return ( |
| 42 | + <div> |
| 43 | + <div className="hidden overflow-hidden rounded-md border border-tertiary-weak bg-panel shadow-sm md:block"> |
| 44 | + <table className="w-full table-auto whitespace-nowrap type-body-sm"> |
| 45 | + <thead> |
| 46 | + <tr className="border-b border-tertiary-weak text-left"> |
| 47 | + <th className="px-3 py-2 font-semibold">Event</th> |
| 48 | + <th className="px-3 py-2 text-right font-semibold">Single</th> |
| 49 | + <th className="px-3 py-2 text-right font-semibold text-muted">NR</th> |
| 50 | + <th className="px-3 py-2 text-right font-semibold text-muted">CR</th> |
| 51 | + <th className="px-3 py-2 text-right font-semibold text-muted">WR</th> |
| 52 | + <th className="px-3 py-2 text-right font-semibold">Average</th> |
| 53 | + <th className="px-3 py-2 text-right font-semibold text-muted">WR</th> |
| 54 | + <th className="px-3 py-2 text-right font-semibold text-muted">CR</th> |
| 55 | + <th className="px-3 py-2 text-right font-semibold text-muted">NR</th> |
| 56 | + </tr> |
| 57 | + </thead> |
| 58 | + <tbody> |
| 59 | + {entries.map(([eventId, record]) => ( |
| 60 | + <tr |
| 61 | + key={eventId} |
| 62 | + className="border-b border-tertiary-weak odd:bg-tertiary last:border-b-0"> |
| 63 | + <td className="px-3 py-2 font-medium"> |
| 64 | + <span className={`cubing-icon event-${eventId} mr-2`} aria-hidden="true" /> |
| 65 | + {getUserEventName(eventId)} |
| 66 | + </td> |
| 67 | + <td className="px-3 py-2 text-right font-semibold"> |
| 68 | + {resultValue(eventId, 'single', record.single)} |
| 69 | + </td> |
| 70 | + <td className="px-3 py-2 text-right text-muted"> |
| 71 | + {rankValue(record.single?.country_rank)} |
| 72 | + </td> |
| 73 | + <td className="px-3 py-2 text-right text-muted"> |
| 74 | + {rankValue(record.single?.continent_rank)} |
| 75 | + </td> |
| 76 | + <td className="px-3 py-2 text-right text-muted"> |
| 77 | + {rankValue(record.single?.world_rank)} |
| 78 | + </td> |
| 79 | + <td className="px-3 py-2 text-right font-semibold"> |
| 80 | + {resultValue(eventId, 'average', record.average)} |
| 81 | + </td> |
| 82 | + <td className="px-3 py-2 text-right text-muted"> |
| 83 | + {rankValue(record.average?.world_rank)} |
| 84 | + </td> |
| 85 | + <td className="px-3 py-2 text-right text-muted"> |
| 86 | + {rankValue(record.average?.continent_rank)} |
| 87 | + </td> |
| 88 | + <td className="px-3 py-2 text-right text-muted"> |
| 89 | + {rankValue(record.average?.country_rank)} |
| 90 | + </td> |
| 91 | + </tr> |
| 92 | + ))} |
| 93 | + </tbody> |
| 94 | + </table> |
| 95 | + </div> |
| 96 | + |
| 97 | + <div className="space-y-2 md:hidden"> |
| 98 | + {entries.map(([eventId, record]) => ( |
| 99 | + <div |
| 100 | + key={eventId} |
| 101 | + className="rounded-md border border-tertiary-weak bg-panel px-3 py-2 shadow-sm"> |
| 102 | + <div className="space-y-2"> |
| 103 | + <div className="type-label text-default"> |
| 104 | + <span className={`cubing-icon event-${eventId} mr-2`} aria-hidden="true" /> |
| 105 | + {getUserEventName(eventId)} |
| 106 | + </div> |
| 107 | + |
| 108 | + <div className="grid grid-cols-[auto_1fr] gap-x-3 gap-y-2 type-body-sm"> |
| 109 | + <div className="font-semibold text-muted">Single</div> |
| 110 | + <div className="min-w-0"> |
| 111 | + <div className="font-semibold text-default"> |
| 112 | + {resultValue(eventId, 'single', record.single)} |
| 113 | + </div> |
| 114 | + <div className="text-muted"> |
| 115 | + NR {rankValue(record.single?.country_rank)} / CR{' '} |
| 116 | + {rankValue(record.single?.continent_rank)} / WR{' '} |
| 117 | + {rankValue(record.single?.world_rank)} |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + |
| 121 | + <div className="font-semibold text-muted">Average</div> |
| 122 | + <div className="min-w-0"> |
| 123 | + <div className="font-semibold text-default"> |
| 124 | + {resultValue(eventId, 'average', record.average)} |
| 125 | + </div> |
| 126 | + <div className="text-muted"> |
| 127 | + NR {rankValue(record.average?.country_rank)} / CR{' '} |
| 128 | + {rankValue(record.average?.continent_rank)} / WR{' '} |
| 129 | + {rankValue(record.average?.world_rank)} |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + </div> |
| 135 | + ))} |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + ); |
| 139 | +} |
0 commit comments