@@ -5,7 +5,13 @@ import CreateIcon from '@material-ui/icons/Create';
55import FullscreenIcon from '@material-ui/icons/Fullscreen' ;
66import 'mapbox-gl/dist/mapbox-gl.css' ;
77import React from 'react' ;
8- import ReactMapGL , { MapContext , MapEvent , NavigationControl , Popup , Source } from 'react-map-gl' ;
8+ import ReactMapGL , {
9+ MapContext ,
10+ MapEvent ,
11+ NavigationControl ,
12+ Popup ,
13+ Source ,
14+ } from 'react-map-gl' ;
915import { useRecoilValue , useSetRecoilState } from 'recoil' ;
1016import { Feature } from '../../../api/types/feature' ;
1117import snackbarAtom from '../../../state/atoms/snackbar' ;
@@ -73,8 +79,12 @@ interface MapboxFeaturesProps {
7379 color : string ;
7480}
7581
76- const navControlStyle = { right : 10 , bottom : 10 , zIndex : 10 , } ;
77- const DEFAULT_VIEWPORT : ViewportProps = { zoom : 10 , width : 'fit' , height : '100%' } ;
82+ const navControlStyle = { right : 10 , bottom : 10 , zIndex : 10 } ;
83+ const DEFAULT_VIEWPORT : ViewportProps = {
84+ zoom : 10 ,
85+ width : 'fit' ,
86+ height : '100%' ,
87+ } ;
7888
7989interface Props {
8090 onFullscreen : ( ) => void ;
@@ -86,7 +96,8 @@ function Map({ onFullscreen, isFullscreen }: Props): JSX.Element {
8696 const [ selectedFeature , setSelectedFeature ] = React . useState < Feature > ( ) ;
8797 const [ editPlantModalOpen , setEditPlantModalOpen ] = React . useState ( false ) ;
8898 const [ viewport , setViewport ] = React . useState ( DEFAULT_VIEWPORT ) ;
89- const [ selectedCoordinates , setSelectedCoordinates ] = React . useState < [ number , number ] > ( ) ;
99+ const [ selectedCoordinates , setSelectedCoordinates ] =
100+ React . useState < [ number , number ] > ( ) ;
90101
91102 const features = useRecoilValue ( plantsFeaturesWithGeolocationSelector ) ;
92103 const speciesForChart = useRecoilValue ( speciesForChartSelector ) ;
@@ -99,49 +110,65 @@ function Map({ onFullscreen, isFullscreen }: Props): JSX.Element {
99110 setViewport ( { ...DEFAULT_VIEWPORT , height : '60vh' } ) ;
100111 } , [ isFullscreen ] ) ;
101112
102- useCenterMap ( center , setViewport ) ;
103- const geojson : GeoJSON . FeatureCollection < GeoJSON . Geometry > = React . useMemo ( ( ) => (
104- ( plantsByFeatureId && features ) ? features : [ ] ) . reduce ( ( acum , feature ) => {
105- const coordinates = getCoordinates ( feature ) ;
106- const plant = plantsByFeatureId ! [ feature . id ! ] ;
107- if ( plant ) {
108- const color = ( speciesForChart [ plant . speciesId ?? 0 ] ?? { color : 'black' } ) . color ;
109- const properties : MapboxFeaturesProps = { feature : JSON . stringify ( feature ) , color } ;
113+ const geojson : GeoJSON . FeatureCollection < GeoJSON . Geometry > = React . useMemo (
114+ ( ) =>
115+ ( plantsByFeatureId && features ? features : [ ] ) . reduce (
116+ ( acum , feature ) => {
117+ const coordinates = getCoordinates ( feature ) ;
118+ const plant = plantsByFeatureId ! [ feature . id ! ] ;
119+ if ( plant ) {
120+ const color = (
121+ speciesForChart [ plant . speciesId ?? 0 ] ?? { color : 'black' }
122+ ) . color ;
123+ const properties : MapboxFeaturesProps = {
124+ feature : JSON . stringify ( feature ) ,
125+ color,
126+ } ;
110127
111- acum . features . push ( {
112- type : 'Feature' ,
113- properties,
114- geometry : {
115- type : 'Point' ,
116- coordinates : [ coordinates . longitude , coordinates . latitude ]
128+ acum . features . push ( {
129+ type : 'Feature' ,
130+ properties,
131+ geometry : {
132+ type : 'Point' ,
133+ coordinates : [ coordinates . longitude , coordinates . latitude ] ,
134+ } ,
135+ } ) ;
117136 }
118- } ) ;
119- }
120137
121- return acum ;
122- } , { type : 'FeatureCollection' , features : [ ] } as GeoJSON . FeatureCollection < GeoJSON . Geometry > )
123- , [ features , plantsByFeatureId , speciesForChart ] ) ;
138+ return acum ;
139+ } ,
140+ {
141+ type : 'FeatureCollection' ,
142+ features : [ ] ,
143+ } as GeoJSON . FeatureCollection < GeoJSON . Geometry >
144+ ) ,
145+ [ features , plantsByFeatureId , speciesForChart ]
146+ ) ;
124147
125- const selectedPlant = selectedFeature && plantsByFeatureId
126- ? plantsByFeatureId [ selectedFeature . id ! ]
127- : undefined ;
148+ const selectedPlant =
149+ selectedFeature && plantsByFeatureId
150+ ? plantsByFeatureId [ selectedFeature . id ! ]
151+ : undefined ;
128152
129- const selectedPlantForTable = selectedPlant && selectedFeature ? {
130- date : selectedPlant . datePlanted ,
131- species : speciesForChart [ selectedPlant . speciesId ! ]
132- ? speciesForChart [ selectedPlant . speciesId ! ] . speciesName . name
133- : undefined ,
134- geolocation :
135- selectedFeature . geom &&
136- Array . isArray ( selectedFeature ?. geom . coordinates )
137- ? `${ selectedFeature . geom . coordinates [ 1 ] . toFixed (
138- 6
139- ) } , ${ selectedFeature . geom . coordinates [ 0 ] . toFixed ( 6 ) } `
140- : undefined ,
141- notes : selectedFeature . notes ,
142- featureId : selectedFeature . id ,
143- speciesId : selectedPlant . speciesId ,
144- } : undefined ;
153+ const selectedPlantForTable =
154+ selectedPlant && selectedFeature
155+ ? {
156+ date : selectedPlant . datePlanted ,
157+ species : speciesForChart [ selectedPlant . speciesId ! ]
158+ ? speciesForChart [ selectedPlant . speciesId ! ] . speciesName . name
159+ : undefined ,
160+ geolocation :
161+ selectedFeature . geom &&
162+ Array . isArray ( selectedFeature ?. geom . coordinates )
163+ ? `${ selectedFeature . geom . coordinates [ 1 ] . toFixed (
164+ 6
165+ ) } , ${ selectedFeature . geom . coordinates [ 0 ] . toFixed ( 6 ) } `
166+ : undefined ,
167+ notes : selectedFeature . notes ,
168+ featureId : selectedFeature . id ,
169+ speciesId : selectedPlant . speciesId ,
170+ }
171+ : undefined ;
145172
146173 const onCloseEditPlantModal = ( snackbarMessage ?: string ) => {
147174 setEditPlantModalOpen ( false ) ;
@@ -162,7 +189,9 @@ function Map({ onFullscreen, isFullscreen }: Props): JSX.Element {
162189 } ;
163190
164191 const onClick = ( event : MapEvent ) => {
165- if ( ! event . features ) { return ; }
192+ if ( ! event . features ) {
193+ return ;
194+ }
166195
167196 if ( event . features && event . features . length > 0 ) {
168197 const eventFeature = event . features [ 0 ] ;
@@ -171,15 +200,13 @@ function Map({ onFullscreen, isFullscreen }: Props): JSX.Element {
171200 if ( layerId === 'unclustered-point' ) {
172201 const lngLat = event . lngLat ;
173202
174-
175203 if ( eventFeature . properties ) {
176204 const properties : MapboxFeaturesProps = eventFeature . properties ;
177205 const feature = JSON . parse ( properties . feature ) ;
178206
179207 setSelectedCoordinates ( lngLat ) ;
180208 setSelectedFeature ( feature ) ;
181209 }
182-
183210 }
184211 }
185212 } ;
@@ -212,39 +239,63 @@ function Map({ onFullscreen, isFullscreen }: Props): JSX.Element {
212239 < MapLayers />
213240 </ Source >
214241 < NavigationControl showCompass = { false } style = { navControlStyle } />
242+ < CenterMap center = { center } setViewport = { setViewport } />
215243 < div style = { { position : 'absolute' , right : 0 , bottom : 80 } } >
216- < IconButton id = 'full-screen' onClick = { onFullscreen } className = { classes . fullscreen } >
244+ < IconButton
245+ id = 'full-screen'
246+ onClick = { onFullscreen }
247+ className = { classes . fullscreen }
248+ >
217249 < FullscreenIcon />
218250 </ IconButton >
219251 </ div >
220252 { selectedFeature && selectedPlant && selectedCoordinates && (
221253 < Popup
222- onClose = { ( ) => { setSelectedFeature ( undefined ) ; setSelectedCoordinates ( undefined ) ; } }
254+ onClose = { ( ) => {
255+ setSelectedFeature ( undefined ) ;
256+ setSelectedCoordinates ( undefined ) ;
257+ } }
223258 latitude = { selectedCoordinates [ 1 ] }
224259 longitude = { selectedCoordinates [ 0 ] }
225260 captureClick = { false }
226261 closeOnClick = { false }
227262 anchor = 'top'
228263 >
229264 < div >
230- < Typography component = 'p' variant = 'subtitle2' id = 'feature-species-name' >
265+ < Typography
266+ component = 'p'
267+ variant = 'subtitle2'
268+ id = 'feature-species-name'
269+ >
231270 { selectedPlant . speciesId
232271 ? speciesForChart [ selectedPlant . speciesId ] . speciesName . name
233272 : strings . OTHER }
234273 </ Typography >
235- < Typography component = 'p' variant = 'body2' className = { classes . spacing } >
236- { strings . AS_OF } { ' ' }
237- { cellDateFormatter ( selectedFeature . enteredTime ) }
274+ < Typography
275+ component = 'p'
276+ variant = 'body2'
277+ className = { classes . spacing }
278+ >
279+ { strings . AS_OF } { cellDateFormatter ( selectedFeature . enteredTime ) }
238280 </ Typography >
239- < Typography component = 'p' variant = 'body2' className = { classes . spacing } id = 'feature-coordinates' >
281+ < Typography
282+ component = 'p'
283+ variant = 'body2'
284+ className = { classes . spacing }
285+ id = 'feature-coordinates'
286+ >
240287 { selectedCoordinates ? selectedCoordinates [ 1 ] . toFixed ( 6 ) : 0 } ,
241288 { selectedCoordinates ? selectedCoordinates [ 0 ] . toFixed ( 6 ) : 0 }
242289 </ Typography >
243290 < PlantPhoto featureId = { selectedFeature ?. id } />
244291 < Chip
245292 id = 'new-species'
246293 size = 'medium'
247- label = { selectedPlant . speciesId ? strings . EDIT_SPECIES : strings . ADD_SPECIES }
294+ label = {
295+ selectedPlant . speciesId
296+ ? strings . EDIT_SPECIES
297+ : strings . ADD_SPECIES
298+ }
248299 onClick = { onNewSpecies }
249300 className = { classes . newSpecies }
250301 icon = { selectedPlant . speciesId ? < CreateIcon /> : < AddIcon /> }
@@ -259,13 +310,25 @@ function Map({ onFullscreen, isFullscreen }: Props): JSX.Element {
259310
260311export default React . memo ( Map ) ;
261312
262- function useCenterMap ( center : { latitude : number ; longitude : number } , setViewport : ( viewport : ViewportProps ) => void ) {
313+ interface CenterMapProps {
314+ center : { latitude : number ; longitude : number } ;
315+ setViewport : React . Dispatch <
316+ React . SetStateAction < {
317+ zoom : number ;
318+ width : string ;
319+ height : string ;
320+ } >
321+ > ;
322+ }
323+
324+ function CenterMap ( { center, setViewport } : CenterMapProps ) {
263325 const { map } = React . useContext ( MapContext ) ;
264326 const [ lat , setLat ] = React . useState ( center . latitude ) ;
265327 const [ long , setLong ] = React . useState ( center . longitude ) ;
266328
267329 React . useEffect ( ( ) => {
268- if ( map && center && ( center . longitude !== long || center . latitude !== lat ) ) {
330+ console . log ( map ) ;
331+ if ( center && ( center . longitude !== long || center . latitude !== lat ) ) {
269332 map . jumpTo ( {
270333 center : [ center . longitude , center . latitude ] ,
271334 essential : true ,
@@ -275,4 +338,6 @@ function useCenterMap(center: { latitude: number; longitude: number }, setViewpo
275338 setLong ( center . longitude ) ;
276339 }
277340 } , [ center , lat , long , map , setViewport ] ) ;
341+
342+ return null ;
278343}
0 commit comments