Skip to content

Commit 7af5526

Browse files
committed
Fixed rankings
1 parent a19f231 commit 7af5526

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed

src/pages/Competition/PsychSheet/PsychSheetEvent.tsx

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Event, EventId } from '@wca/helpers';
1+
import { Event, EventId, Person, PersonalBest } from '@wca/helpers';
22
import classNames from 'classnames';
33
import getUnicodeFlagIcon from 'country-flag-icons/unicode';
44
import { useCallback, useMemo } from 'react';
55
import { useTranslation } from 'react-i18next';
66
import { Link, useNavigate, useParams, useSearchParams } from 'react-router-dom';
77
import { Container } from '@/components/Container';
8+
import { findPR } from '@/lib/activities';
89
import { activityCodeToName } from '@/lib/activityCodes';
910
import { acceptedRegistration, isRegisteredForEvent } from '@/lib/person';
1011
import { renderResultByEventId } from '@/lib/results';
1112
import { byWorldRanking } from '@/lib/sort';
12-
import { unique } from '@/lib/utils';
1313
import { useWCIF } from '@/providers/WCIFProvider';
1414

1515
export const PsychSheetEvent = () => {
@@ -43,30 +43,53 @@ export const PsychSheetEvent = () => {
4343
);
4444

4545
// Creates a proper psychsheet with support for tied rankings
46-
const sortedPersons = useMemo(
47-
() =>
48-
eventId &&
49-
persons.sort(byWorldRanking(eventId, resultType)).map((person) => {
50-
return {
51-
...person,
52-
pr: person.personalBests?.find(
53-
(pr) => pr.eventId === eventId && pr.type === (resultType || 'average'),
54-
),
55-
};
56-
}),
57-
[eventId, persons, resultType],
58-
);
59-
60-
const rankings = useMemo(
61-
() =>
62-
sortedPersons
63-
?.map((person) => person.pr?.worldRanking ?? 0)
64-
.filter((i) => i > 0)
65-
.filter(unique) ?? [],
66-
[sortedPersons],
67-
);
68-
69-
const gridCss = 'grid grid-cols-[3em_2em_1fr_min-content_7em]';
46+
const sortedPersons = useMemo(() => {
47+
if (!eventId || !persons.length) {
48+
return [];
49+
}
50+
51+
const _findPr = findPR(eventId);
52+
return persons.sort(byWorldRanking(eventId, resultType)).reduce(
53+
(
54+
persons: (Person & {
55+
rank: number;
56+
mainRank: number;
57+
subRank: number;
58+
pr?: PersonalBest;
59+
})[],
60+
person,
61+
index,
62+
) => {
63+
const lastPerson = index > 0 ? persons[index - 1] : undefined;
64+
65+
const avgPr = _findPr(person.personalBests || [], 'average');
66+
const singlePr = _findPr(person.personalBests || [], 'single');
67+
68+
const mainRank =
69+
(resultType === 'average' ? avgPr?.worldRanking : singlePr?.worldRanking) ?? 0;
70+
const subRank = singlePr?.worldRanking ?? 0;
71+
72+
const rank =
73+
lastPerson && mainRank === lastPerson.mainRank && subRank === lastPerson.subRank
74+
? lastPerson.rank
75+
: index + 1;
76+
77+
return [
78+
...persons,
79+
{
80+
...person,
81+
mainRank,
82+
subRank,
83+
rank: rank,
84+
pr: resultType === 'average' ? avgPr : singlePr,
85+
},
86+
];
87+
},
88+
[],
89+
);
90+
}, [eventId, persons, resultType]);
91+
92+
const gridCss = 'grid grid-cols-[3.5em_2em_1fr_min-content_7em]';
7093

7194
const handleEventChange = useCallback(
7295
(newEventId: EventId) => {
@@ -126,11 +149,6 @@ export const PsychSheetEvent = () => {
126149
</div>
127150
<div className="contents striped">
128151
{sortedPersons?.map((person) => {
129-
const rank =
130-
(rankings?.findIndex((i) => i === person.pr?.worldRanking) >= 0
131-
? rankings?.findIndex((i) => i === person.pr?.worldRanking)
132-
: rankings.length) + 1;
133-
134152
const prAverage = person.personalBests?.find(
135153
(pr) => pr.eventId === eventId && pr.type === resultType,
136154
);
@@ -141,7 +159,7 @@ export const PsychSheetEvent = () => {
141159
className="contents"
142160
to={`/competitions/${wcif?.id}/personal-bests/${person.wcaId}`}>
143161
<span className="px-3 py-2.5 text-right [font-variant-numeric:tabular-nums]">
144-
{rank}
162+
{person.rank}
145163
</span>
146164
<span className="px-3 py-2.5 text-left flex items-center w-full">
147165
{getUnicodeFlagIcon(person.countryIso2)}

0 commit comments

Comments
 (0)