11import { Activity , AssignmentCode , EventId , Person } from '@wca/helpers' ;
22import classNames from 'classnames' ;
3- import { useEffect , useMemo } from 'react' ;
3+ import { useCallback , useEffect , useMemo } from 'react' ;
44import { Link } from 'react-router-dom' ;
55import tw from 'tailwind-styled-components/dist/tailwind' ;
66import {
@@ -47,9 +47,11 @@ interface EventGroupProps {
4747export default function EventGroup ( { competitionId, activity, persons } : EventGroupProps ) {
4848 const { setTitle, wcif } = useWCIF ( ) ;
4949 const { eventId, roundNumber } = parseActivityCode ( activity ?. activityCode || '' ) ;
50- const event = wcif ?. events . find ( ( e ) => e . id === eventId ) ;
51- const prevRound =
52- roundNumber && event ?. rounds ?. find ( ( r ) => r . id === `${ eventId } ,r${ roundNumber - 1 } ` ) ;
50+ const event = useMemo ( ( ) => wcif ?. events . find ( ( e ) => e . id === eventId ) , [ wcif ] ) ;
51+ const prevRound = useMemo (
52+ ( ) => roundNumber && event ?. rounds ?. find ( ( r ) => r . id === `${ eventId } -r${ roundNumber - 1 } ` ) ,
53+ [ event ]
54+ ) ;
5355
5456 useEffect ( ( ) => {
5557 if ( activity ) {
@@ -85,9 +87,7 @@ export default function EventGroup({ competitionId, activity, persons }: EventGr
8587 [ persons , eventId ]
8688 ) ;
8789
88- const competitors = everyoneInActivity
89- . filter ( isAssignment ( 'competitor' ) )
90- . sort ( byWorldRanking ( eventId as EventId ) ) ;
90+ const competitors = everyoneInActivity . filter ( isAssignment ( 'competitor' ) ) ;
9191
9292 const assignments = new Set (
9393 everyoneInActivity . map ( ( person ) => person . assignments ?. map ( ( a ) => a . assignmentCode ) ) . flat ( )
@@ -100,34 +100,67 @@ export default function EventGroup({ competitionId, activity, persons }: EventGr
100100 return acc ;
101101 } , { } ) as Record < AssignmentCode , Person [ ] > ;
102102
103- // TODO: Calculate seed result from previous round results when available.
104- const seedResult = ( person ) => {
105- if ( prevRound ) {
106- const prevRoundResults = prevRound . results ?. find ( ( r ) => r . personId === person . registrantId ) ;
107- if ( ! prevRoundResults ) {
103+ const seedResult = useCallback (
104+ ( person ) => {
105+ if ( prevRound ) {
106+ const prevRoundResults = prevRound . results ?. find (
107+ ( r ) => r . personId ?. toString ( ) === person . registrantId ?. toString ( )
108+ ) ;
109+ if ( ! prevRoundResults ) {
110+ return '' ;
111+ }
112+
113+ if ( [ 'a' || 'm' ] . includes ( prevRound . format ) ) {
114+ return renderResultByEventId ( eventId , 'average' , prevRoundResults . average ) ;
115+ }
116+
117+ return renderResultByEventId ( eventId , 'single' , prevRoundResults . best ) ;
118+ }
119+
120+ const averagePr = person . prAverage ?. best ;
121+ const singlePr = person . prSingle ?. best ;
122+ const shouldShowAveragePr = ! isRankedBySingle ( eventId ) ;
123+ if ( ( shouldShowAveragePr && ! averagePr ) || ! singlePr ) {
108124 return '' ;
109125 }
110126
111- if ( prevRound . format === 'a' || 'm' ) {
112- return renderResultByEventId ( eventId , 'average' , prevRoundResults . average ) ;
127+ return renderResultByEventId (
128+ eventId ,
129+ shouldShowAveragePr ? 'average' : 'single' ,
130+ shouldShowAveragePr ? averagePr : singlePr
131+ ) ;
132+ } ,
133+ [ prevRound ]
134+ ) ;
135+
136+ const seedRank = useCallback (
137+ ( person ) => {
138+ if ( prevRound ) {
139+ const prevRoundResults = prevRound . results ?. find (
140+ ( r ) => r . personId ?. toString ( ) === person . registrantId ?. toString ( )
141+ ) ;
142+ if ( ! prevRoundResults ) {
143+ return '' ;
144+ }
145+
146+ return prevRoundResults . ranking ;
113147 }
114148
115- return renderResultByEventId ( eventId , 'single' , prevRoundResults . average ) ;
116- }
149+ const averagePr = person . prAverage ;
150+ const singlePr = person . prSingle ;
151+ const shouldShowAveragePr = ! isRankedBySingle ( eventId ) ;
152+ if ( ( shouldShowAveragePr && ! averagePr ) || ! singlePr ) {
153+ return '' ;
154+ }
117155
118- const averagePr = person . prAverage ?. best ;
119- const singlePr = person . prSingle ?. best ;
120- const shouldShowAveragePr = ! isRankedBySingle ( eventId ) ;
121- if ( ( shouldShowAveragePr && ! averagePr ) || ! singlePr ) {
122- return '' ;
123- }
156+ if ( averagePr ) {
157+ return averagePr . worldRanking ;
158+ }
124159
125- return renderResultByEventId (
126- eventId ,
127- shouldShowAveragePr ? 'average' : 'single' ,
128- shouldShowAveragePr ? averagePr : singlePr
129- ) ;
130- } ;
160+ return singlePr . worldRanking ;
161+ } ,
162+ [ prevRound ]
163+ ) ;
131164
132165 const stationNumber = ( assignmentCode ) => ( person ) => {
133166 const assignment = person . assignments . find (
@@ -140,21 +173,24 @@ export default function EventGroup({ competitionId, activity, persons }: EventGr
140173
141174 return (
142175 < >
143- < div className = "p-2" >
144- < h3 className = "font-bold" style = { { lineHeight : 2 } } >
145- < span
146- className = "px-3 py-2 rounded mr-2"
147- style = { {
148- backgroundColor : `${ room ?. color } 70` ,
149- } } >
150- { room ?. name }
151- </ span >
152- < span > { activityCodeToName ( activity ?. activityCode ) } </ span >
153- </ h3 >
154- < p className = "p-2" >
155- { formatDateTimeRange ( activity . startTime , activity . endTime , 5 , timeZone ) }
156- </ p >
157- </ div >
176+ { wcif && (
177+ < div className = "p-2" >
178+ < h3 className = "font-bold" style = { { lineHeight : 2 } } >
179+ < Link
180+ className = "px-3 py-2 rounded mr-2"
181+ style = { {
182+ backgroundColor : `${ room ?. color } 70` ,
183+ } }
184+ to = { `/competitions/${ wcif . id } /rooms/${ room ?. id } ` } >
185+ { room ?. name }
186+ </ Link >
187+ < span > { activityCodeToName ( activity ?. activityCode ) } </ span >
188+ </ h3 >
189+ < p className = "p-2" >
190+ { formatDateTimeRange ( activity . startTime , activity . endTime , 5 , timeZone ) }
191+ </ p >
192+ </ div >
193+ ) }
158194 < hr className = "mb-2" />
159195 < div >
160196 < AssignmentCategoryHeader className = "bg-green-200 pb-1" >
@@ -169,17 +205,24 @@ export default function EventGroup({ competitionId, activity, persons }: EventGr
169205 </ tr >
170206 </ thead >
171207 < tbody >
172- { competitors . map ( ( person ) => (
173- < Link
174- className = "table-row even:bg-green-50 hover:opacity-80"
175- to = { `/competitions/${ competitionId } /persons/${ person . registrantId } ` } >
176- < td className = "py-3 px-6" > { person . name } </ td >
177- < td className = "py-3 px-6" > { seedResult ( person ) } </ td >
178- { anyCompetitorHasStationNumber && (
179- < td className = "py-3 px-6" > { stationNumber ( 'competitor' ) ( person ) } </ td >
180- ) }
181- </ Link >
182- ) ) }
208+ { competitors
209+ . map ( ( person ) => ( {
210+ ...person ,
211+ seedResult : seedResult ( person ) ,
212+ seedRank : seedRank ( person ) ,
213+ } ) )
214+ . sort ( ( a , b ) => a . seedRank - b . seedRank )
215+ . map ( ( person ) => (
216+ < Link
217+ className = "table-row even:bg-green-50 hover:opacity-80"
218+ to = { `/competitions/${ competitionId } /persons/${ person . registrantId } ` } >
219+ < td className = "py-3 px-6" > { person . name } </ td >
220+ < td className = "py-3 px-6" > { person . seedResult } </ td >
221+ { anyCompetitorHasStationNumber && (
222+ < td className = "py-3 px-6" > { stationNumber ( 'competitor' ) ( person ) } </ td >
223+ ) }
224+ </ Link >
225+ ) ) }
183226 </ tbody >
184227 </ table >
185228 </ div >
0 commit comments