11import { Coord , featureCollection , point } from '@turf/helpers' ;
22import nearestPointOnLine from '@turf/nearest-point-on-line' ;
3- import type { FeatureCollection , Point } from 'geojson' ;
3+ import type { Feature , FeatureCollection , LineString , Point } from 'geojson' ;
44import { GameConfig , GameMode } from './game-modes' ;
55import { useAppSelector } from './store' ;
66import { distance } from './util' ;
77
8+ const isDegenerate = ( line : Feature < LineString > ) =>
9+ line . geometry . coordinates . length !== 2 ||
10+ line . geometry . coordinates [ 0 ] . join ( ',' ) ===
11+ line . geometry . coordinates [ 1 ] . join ( ',' ) ;
12+
813export const useGameConfig = ( ) : GameConfig | null => {
914 const { mode, lineTarget, basicTarget, multiTarget, error } = useAppSelector (
1015 ( { game } ) => game
@@ -13,7 +18,7 @@ export const useGameConfig = (): GameConfig | null => {
1318 return null ;
1419 } else if ( mode === GameMode . BASIC ) {
1520 return { mode, target : basicTarget } ;
16- } else if ( mode === GameMode . LINE ) {
21+ } else if ( mode === GameMode . LINE && ! isDegenerate ( lineTarget ) ) {
1722 return { mode, target : lineTarget } ;
1823 } else if ( mode === GameMode . MULTI && multiTarget . features . length > 0 ) {
1924 return { mode, target : multiTarget } ;
@@ -39,12 +44,17 @@ export const decorate = <P extends {}>(
3944 photos : FeatureCollection < Point , P >
4045) : FeatureCollection < Point , P & { distance : number } > => {
4146 const calculator = distanceCalc ( game ) ;
42- return featureCollection (
43- photos . features
44- . map ( ( photo ) => ( {
45- ...photo ,
46- properties : { ...photo . properties , distance : calculator ( photo ) } ,
47- } ) )
48- . sort ( ( a , b ) => a . properties . distance - b . properties . distance )
49- ) ;
47+ try {
48+ return featureCollection (
49+ photos . features
50+ . map ( ( photo ) => ( {
51+ ...photo ,
52+ properties : { ...photo . properties , distance : calculator ( photo ) } ,
53+ } ) )
54+ . sort ( ( a , b ) => a . properties . distance - b . properties . distance )
55+ ) ;
56+ } catch ( error ) {
57+ console . error ( error ) ;
58+ return featureCollection ( [ ] ) ;
59+ }
5060} ;
0 commit comments