@@ -6,6 +6,7 @@ import Popup from './popup';
66import extent from 'turf-extent' ;
77import { useMapDataContext } from '../../context/MapDataContext' ;
88import useAPI from '../../components/ChatMap/useApi.js'
9+ import { MapTools , useMapTags } from '../Tags' ;
910import { BackgroundControl } from "./controls.jsx" ;
1011
1112/**
@@ -28,14 +29,31 @@ export default function Map({dataFiles, center, zoom, className, onInteract, sho
2829 } = useAPI ( ) ;
2930
3031 const { data, tags, mapDataDispatch} = useMapDataContext ( ) ;
32+ // The values of the keys of the map, when it has them; it changes every minute, as values expire
33+ const mapTags = useMapTags ( data ) ;
3134
3235 const dataRef = useRef ( data ) ;
3336 dataRef . current = data ;
37+ // True while a point is being added by hand
38+ const addingPoint = useRef ( false ) ;
3439
3540 const showFeature = ( feature ) => {
3641 if ( feature ) setActivePopupFeature ( feature ) ;
3742 } ;
3843
44+ // Color of a point: by kind of message, gray when removed
45+ const pointColor = [
46+ "case" ,
47+ [ "boolean" , [ "get" , "removed" ] , false ] ,
48+ '#9A969B' , // --hot-color-neutral-400
49+ [
50+ "case" ,
51+ [ "to-boolean" , [ "get" , "file" ] ] ,
52+ '#D73F3F' , // --hot-color-red-600
53+ '#2E4873' , // --hot-color-blue-600
54+ ]
55+ ] ;
56+
3957 useEffect ( ( ) => {
4058 if ( map . current ) return ;
4159
@@ -68,7 +86,7 @@ export default function Map({dataFiles, center, zoom, className, onInteract, sho
6886
6987 // Add geojson data source
7088 map . current . addSource ( 'locations' , {
71- data : data ,
89+ data : mapTags . shown ( data ) ,
7290 type : "geojson"
7391 } ) ;
7492
@@ -94,21 +112,13 @@ export default function Map({dataFiles, center, zoom, className, onInteract, sho
94112 'source' : 'locations' ,
95113 'layout' : { } ,
96114 'paint' : {
97- 'circle-color' : [
98- "case" ,
99- [ "boolean" , [ "get" , "removed" ] , false ] ,
100- '#9A969B' , // --hot-color-neutral-400
101- [
102- "case" ,
103- [ "to-boolean" , [ "get" , "file" ] ] ,
104- '#D73F3F' , // --hot-color-red-600
105- '#2E4873' , // --hot-color-blue-600
106- ]
107- ] ,
115+ 'circle-color' : pointColor ,
108116 'circle-radius' : 10
109117 }
110118 } ) ;
111119
120+ mapTags . paint ( map . current , 'pois' , pointColor ) ;
121+
112122 // Invisible layer for with a buffer for better click
113123 map . current . addLayer ( {
114124 'id' : 'pois-clickable' ,
@@ -123,6 +133,7 @@ export default function Map({dataFiles, center, zoom, className, onInteract, sho
123133
124134 // On feature click
125135 map . current . on ( "click" , "pois-clickable" , ( e ) => {
136+ if ( addingPoint . current ) return ;
126137 const clickedId = e . features [ 0 ] . properties . id ;
127138 showFeature ( dataRef . current . features . find ( ( feature ) => feature . properties . id === clickedId ) ) ;
128139 } ) ;
@@ -163,22 +174,27 @@ export default function Map({dataFiles, center, zoom, className, onInteract, sho
163174
164175 // Add geojson data source
165176 if ( data . filterTag !== null ) {
177+ // The filter can also be a value of a key of the map
178+ const byValue = mapTags . filter ( data . filterTag ) ;
166179 {
167- map . current . getSource ( 'locations' ) . setData ( {
180+ map . current . getSource ( 'locations' ) . setData ( mapTags . shown ( {
168181 ...data ,
169182 features : data . features . filter ( feature =>
170- data . filterTag !== undefined ?
183+ byValue ? byValue ( feature )
184+ : data . filterTag !== undefined ?
171185 feature . properties . tags && feature . properties . tags . split ( "," ) . indexOf ( data . filterTag ) > - 1
172186 : ! feature . properties . tags
173187 )
174- } ) ;
188+ } ) ) ;
175189 }
176190 } else {
177- map . current . getSource ( 'locations' ) . setData ( data ) ;
191+ map . current . getSource ( 'locations' ) . setData ( mapTags . shown ( data ) ) ;
178192 }
179193
194+ mapTags . paint ( map . current , 'pois' , pointColor ) ;
195+
180196 }
181- } , [ data ] ) ;
197+ } , [ data , mapTags ] ) ;
182198
183199 // Show popup
184200 useEffect ( ( ) => {
@@ -217,6 +233,14 @@ export default function Map({dataFiles, center, zoom, className, onInteract, sho
217233 setEditingTags ( false ) ;
218234 }
219235
236+ const handleAddPoint = ( feature ) => {
237+ mapDataDispatch ( {
238+ type : "set" ,
239+ payload : { features : [ ...dataRef . current . features , feature ] }
240+ } ) ;
241+ showFeature ( feature ) ;
242+ }
243+
220244 const handleRemoveMessage = async ( feature ) => {
221245 await removePoint ( feature . properties . id ) ;
222246 feature . properties . removed = ! feature . properties . removed ;
@@ -238,6 +262,7 @@ export default function Map({dataFiles, center, zoom, className, onInteract, sho
238262 return (
239263 < div className = "map-wrap" >
240264 < div ref = { mapContainer } className = { [ "map" , className ] . join ( " " ) } />
265+ < MapTools map = { map } adding = { addingPoint } show = { showMessageOptions } onAdd = { handleAddPoint } />
241266 { map . current && activePopupFeature &&
242267 < Popup
243268 popupRef = { popupRef }
@@ -247,6 +272,8 @@ export default function Map({dataFiles, center, zoom, className, onInteract, sho
247272 onRemoveTag = { handleRemoveTag }
248273 onRemoveMessage = { handleRemoveMessage }
249274 allTags = { Object . keys ( tags ) }
275+ pointTags = { mapTags . point }
276+ onChange = { handleChange }
250277 showMessageOptions = { showMessageOptions }
251278 />
252279 }
0 commit comments