forked from coder13/Competitor-groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventActivity.tsx
More file actions
368 lines (322 loc) · 11.6 KB
/
EventActivity.tsx
File metadata and controls
368 lines (322 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import { Activity, AssignmentCode, Person } from '@wca/helpers';
import classNames from 'classnames';
import { useCallback, useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { Link, Navigate, useNavigate } from 'react-router-dom';
import { Breadcrumbs } from '@/components/Breadcrumbs/Breadcrumbs';
import { CutoffTimeLimitPanel } from '@/components/CutoffTimeLimitPanel';
import { getRoomData, getRooms } from '@/lib/activities';
import { activityCodeToName, parseActivityCodeFlexible } from '@/lib/activityCodes';
import { getAllEvents, isOfficialEventId, isRankedBySingle } from '@/lib/events';
import { renderResultByEventId } from '@/lib/results';
import { formatDateTimeRange } from '@/lib/time';
import { useWCIF } from '@/providers/WCIFProvider';
import { PeopleList } from './PeopleList';
import { niceActivityName } from './Activity';
const isAssignment = (assignment) => (a) =>
a.assignments.some(({ assignmentCode }) => assignmentCode === assignment);
interface EventGroupProps {
competitionId: string;
activity: Activity;
persons: Person[];
}
export function EventActivity({ competitionId, activity, persons }: EventGroupProps) {
const { t } = useTranslation();
const navigate = useNavigate();
const { setTitle, wcif } = useWCIF();
const { eventId, roundNumber } = parseActivityCodeFlexible(activity?.activityCode || '');
const event = useMemo(
() => wcif && getAllEvents(wcif).find((e) => e.id === eventId),
[eventId, wcif],
);
const round = useMemo(() => {
if (!event) {
return null;
}
return event.rounds?.find((r) => r.id === `${eventId}-r${roundNumber}`);
}, [event, eventId, roundNumber]);
const prevRound = useMemo(
() => roundNumber && event?.rounds?.find((r) => r.id === `${eventId}-r${roundNumber - 1}`),
[event?.rounds, eventId, roundNumber],
);
useEffect(() => {
if (activity) {
setTitle(niceActivityName(activity));
}
}, [activity, setTitle]);
const room = useMemo(
() =>
wcif &&
getRooms(wcif).find((r) =>
r.activities.some(
(a) => a.id === activity.id || a?.childActivities?.some((ca) => ca.id === activity.id),
),
),
[activity.id, wcif],
);
const roomData = useMemo(() => {
if (!room) {
return undefined;
}
return getRoomData(room, activity);
}, [activity, room]);
const venue = wcif?.schedule.venues?.find((v) => v.rooms.some((r) => r.id === room?.id));
const timeZone = venue?.timezone;
const personsWithPRs = useMemo(
() =>
persons.map((person) => ({
...person,
prSingle: person.personalBests?.find(
(pb) => pb.eventId === eventId && pb.type === 'single',
),
prAverage: person.personalBests?.find(
(pb) => pb.eventId === eventId && pb.type === 'average',
),
})),
[persons, eventId],
);
const competitors = personsWithPRs.filter(isAssignment('competitor'));
const assignments = new Set(
personsWithPRs.map((person) => person.assignments?.map((a) => a.assignmentCode)).flat(),
);
const peopleByAssignmentCode = (Array.from(assignments.values()) as AssignmentCode[])
.filter((assignmentCode) => assignmentCode !== 'competitor')
.reduce((acc, assignmentCode) => {
acc[assignmentCode] = personsWithPRs.filter(isAssignment(assignmentCode));
return acc;
}, {}) as Record<AssignmentCode, Person[]>;
const seedResult = useCallback(
(person) => {
if (!isOfficialEventId(eventId)) {
return '';
}
if (prevRound) {
const prevRoundResults = prevRound.results?.find(
(r) => r.personId?.toString() === person.registrantId?.toString(),
);
if (!prevRoundResults) {
return '';
}
if (['a', 'm'].includes(prevRound.format)) {
return renderResultByEventId(eventId, 'average', prevRoundResults.average);
}
return renderResultByEventId(eventId, 'single', prevRoundResults.best);
}
const averagePr = person.prAverage?.best;
const singlePr = person.prSingle?.best;
const shouldShowAveragePr = !isRankedBySingle(eventId);
if ((shouldShowAveragePr && !averagePr) || !singlePr) {
return '';
}
return renderResultByEventId(
eventId,
shouldShowAveragePr ? 'average' : 'single',
shouldShowAveragePr ? averagePr : singlePr,
);
},
[eventId, prevRound],
);
const seedRank = useCallback(
(person) => {
if (!isOfficialEventId(eventId)) {
return '';
}
if (prevRound) {
const prevRoundResults = prevRound.results?.find(
(r) => r.personId?.toString() === person.registrantId?.toString(),
);
if (!prevRoundResults) {
return '';
}
return prevRoundResults.ranking;
}
const averagePr = person.prAverage;
const singlePr = person.prSingle;
const shouldShowAveragePr = !isRankedBySingle(eventId);
if ((shouldShowAveragePr && !averagePr) || !singlePr) {
return '';
}
if (averagePr) {
return averagePr.worldRanking;
}
return singlePr.worldRanking;
},
[eventId, prevRound],
);
const stationNumber = (assignmentCode) => (person) => {
const assignment = person.assignments.find(
(a) => a.assignmentCode === assignmentCode && a.activityId === activity.id,
);
return assignment?.stationNumber;
};
const anyCompetitorHasStationNumber = competitors.some(stationNumber('competitor'));
const activityName = niceActivityName(activity);
const groupNumber = parseActivityCodeFlexible(activity.activityCode).groupNumber;
const allActivitiesInStage = useMemo(
() =>
room?.activities
.flatMap((i) => i.childActivities)
.filter((i) => {
const stage = getRoomData(room, i);
if (stage && roomData) {
return stage?.name === roomData?.name;
}
return true;
})
.sort((a, b) => new Date(a.startTime).getTime() - new Date(b.startTime).getTime()),
[room, roomData],
);
const currentIndex = useMemo(() => {
return allActivitiesInStage?.findIndex((i) => i.id === activity.id);
}, [activity.id, allActivitiesInStage]);
const prev = useMemo(
() =>
allActivitiesInStage && currentIndex !== undefined
? allActivitiesInStage[currentIndex - 1]
: undefined,
[allActivitiesInStage, currentIndex],
);
const next = useMemo(
() =>
allActivitiesInStage && currentIndex !== undefined
? allActivitiesInStage[currentIndex + 1]
: undefined,
[allActivitiesInStage, currentIndex],
);
const prevUrl = prev && `/competitions/${competitionId}/activities/${prev?.id}`;
const nextUrl = next && `/competitions/${competitionId}/activities/${next?.id}`;
const goToPrev = useCallback(() => {
if (prevUrl) {
navigate(prevUrl);
}
}, [navigate, prevUrl]);
const goToNext = useCallback(() => {
if (nextUrl) {
navigate(nextUrl);
}
}, [navigate, nextUrl]);
useEffect(() => {
const handleKeydown = (event: KeyboardEvent) => {
if (event.key === 'ArrowLeft') {
goToPrev();
}
if (event.key === 'ArrowRight') {
goToNext();
}
};
document.addEventListener('keydown', handleKeydown);
return () => {
document.removeEventListener('keydown', handleKeydown);
};
}, [wcif, activity, goToPrev, goToNext]);
console.log({ prev, next });
return (
<>
{wcif && (
<div className="p-2 space-y-2 text-sm">
<Breadcrumbs
breadcrumbs={[
{
href: `/competitions/${wcif.id}/rooms/${room?.id}`,
label: roomData?.name || '',
pillProps: { style: { backgroundColor: `${roomData?.color}70` } },
},
...(round
? [
{
href: `/competitions/${wcif.id}/events/${round.id}/${groupNumber}`,
label: activityName,
},
]
: [
{
label: activityName,
},
]),
]}
/>
<div className="flex space-x-2">
<Link
to={prevUrl || ''}
className={classNames(
'w-full border rounded-md p-2 px-2 flex cursor-pointer transition-colors my-1 justify-end',
{
'pointer-events-none opacity-25': !prev,
'hover:bg-slate-100 group cursor-pointer': prev,
},
)}>
<span className="fa fa-arrow-left self-center mr-2 group-hover:-translate-x-2 transition-all" />
{t('competition.groups.previousGroup')}
</Link>
<Link
to={nextUrl || ''}
className={classNames(
'w-full border rounded-md p-2 px-2 flex cursor-pointer group hover:bg-slate-100 transition-colors my-1',
{
'pointer-events-none opacity-25': !next,
'hover:bg-slate-100 group': next,
},
)}>
{t('competition.groups.nextGroup')}
<span className="fa fa-arrow-right self-center ml-2 group-hover:translate-x-2 transition-all" />
</Link>
</div>
<div className="space-y-1">
<span className="px-2">
{formatDateTimeRange(activity.startTime, activity.endTime, 5, timeZone)}
</span>
{round && <CutoffTimeLimitPanel round={round} className="w-full" />}
</div>
</div>
)}
<hr className="mb-2" />
{competitors.length > 0 && (
<div>
<h4 className="bg-green-200 pb-1 text-lg font-bold text-center shadow-md py-3 px-6">
{t('common.assignments.competitor.noun')}{' '}
<span className="text-sm">({competitors.length})</span>
</h4>
<table className="w-full text-left">
<thead>
<tr className="text-xs lg:text-sm bg-green-200 shadow-md">
<th className="pt-1 pb-3 px-6">{t('common.name')}</th>
<th className="pt-1 pb-3 px-6">{t('competition.eventActivity.seedResult')}</th>
{anyCompetitorHasStationNumber && (
<th className="pt-1 pb-3 px-6">{t('competition.eventActivity.stationNumber')}</th>
)}
</tr>
</thead>
<tbody>
{competitors
.map((person) => ({
...person,
seedResult: seedResult(person),
seedRank: seedRank(person),
}))
.sort((a, b) => {
return (a.seedRank || 999999999) - (b.seedRank || 999999999);
})
.map((person) => (
<Link
key={person.registrantId}
className="table-row even:bg-green-50 hover:opacity-80"
to={`/competitions/${competitionId}/persons/${person.registrantId}`}>
<td className="py-3 px-6">{person.name}</td>
<td className="py-3 px-6">{person.seedResult}</td>
{anyCompetitorHasStationNumber && (
<td className="py-3 px-6">{stationNumber('competitor')(person)}</td>
)}
</Link>
))}
</tbody>
</table>
</div>
)}
<PeopleList
competitionId={competitionId}
activity={activity}
peopleByAssignmentCode={peopleByAssignmentCode}
/>
</>
);
}