66 useContext , useEffect , useState , useRef , useReducer ,
77} from 'preact/hooks' ;
88
9- import { AppMode , QualMatch } from '@shared/DbTypes' ;
9+ import { AppMode , QualBreak , QualMatch } from '@shared/DbTypes' ;
1010import { TeamRanking } from '@/types' ;
1111import AppContext from '@/AppContext' ;
1212import styles from './styles.module.scss' ;
@@ -25,9 +25,9 @@ const Queueing = () => {
2525 const dbEventRef = useRef < DatabaseReference > ( ) ;
2626 const [ qualMatches , setQualMatches ] = useState < QualMatch [ ] > ( [ ] ) ;
2727 const [ displayMatches , setDisplayMatches ] = useState < {
28- currentMatch : QualMatch | null ,
29- nextMatch : QualMatch | null ,
30- queueingMatches : QualMatch [ ]
28+ currentMatch : QualMatch | QualBreak | null ,
29+ nextMatch : QualMatch | QualBreak | null ,
30+ queueingMatches : QualMatch [ ] | QualBreak [ ]
3131 } > ( { currentMatch : null , nextMatch : null , queueingMatches : [ ] } ) ;
3232 const [ rankings , setRankings ] = useState < TeamRanking [ ] > ( [ ] ) ;
3333
@@ -46,9 +46,13 @@ const Queueing = () => {
4646 } ;
4747 } , [ event . eventCode , season , token ] ) ;
4848
49- const getMatchByNumber = ( matchNumber : number ) : QualMatch | null => qualMatches ?. find (
49+ const getMatchIdxByNumber = ( matchNumber : number ) : number | null => qualMatches ?. findIndex (
5050 ( x ) => x . number === matchNumber ,
5151 ) ?? null ;
52+ const getMatchByIndex = ( index : number | null ) : QualMatch | QualBreak | null => (
53+ index && qualMatches
54+ ? ( qualMatches [ index ] ?? null )
55+ : null ) ;
5256
5357 const updateMatches = ( ) : void => {
5458 const matchNumber = event . currentMatchNumber ;
@@ -62,13 +66,23 @@ const Queueing = () => {
6266 }
6367
6468 try {
65- setDisplayMatches ( {
66- currentMatch : getMatchByNumber ( matchNumber ) ,
67- nextMatch : getMatchByNumber ( matchNumber + 1 ) ,
68- // By default, we'll take the three matches after the one on deck
69- queueingMatches : [ 2 , 3 , 4 ] . map ( ( x ) => getMatchByNumber ( matchNumber + x ) )
70- . filter ( ( x ) => x !== null ) as QualMatch [ ] ,
71- } ) ;
69+ const currentIdx = getMatchIdxByNumber ( matchNumber ) ;
70+ if ( currentIdx !== null ) {
71+ setDisplayMatches ( {
72+ currentMatch : getMatchByIndex ( currentIdx ) ,
73+ nextMatch : getMatchByIndex ( currentIdx + 1 ) ,
74+ // By default, we'll take the three matches after the one on deck
75+ queueingMatches : [ 2 , 3 , 4 ] . map ( ( x ) => getMatchByIndex ( currentIdx + x ) )
76+ . filter ( ( x ) => x !== null ) as QualMatch [ ] ,
77+ } ) ;
78+ } else {
79+ setDisplayMatches ( {
80+ currentMatch : null ,
81+ nextMatch : null ,
82+ queueingMatches : [ ] ,
83+ } ) ;
84+ }
85+
7286 setLoadingState ( 'ready' ) ;
7387 } catch ( e ) {
7488 setLoadingState ( 'error' ) ;
0 commit comments