Skip to content

Commit 67a426c

Browse files
authored
Merge pull request #236 from hotosm/feat/tags-ui
Configurable tags in the map UI, behind the dashboard parameter
2 parents fffab00 + 5cf6f77 commit 67a426c

30 files changed

Lines changed: 2809 additions & 25 deletions
Lines changed: 4 additions & 0 deletions
Loading

‎chatmap-ui/src/components/ChatMap/useApi.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useState, useCallback } from "react";
22
import { useConfigContext } from '../../context/ConfigContext.jsx'
3+
import { withMapTags } from '../Tags';
34

45
/**
56
* ChatMap API
@@ -73,7 +74,7 @@ const useApi = (params = {}) => {
7374
}
7475
if (!response.ok) throw new Error('Failed to fetch data');
7576
const result = await response.json();
76-
setMapData(result);
77+
setMapData(await withMapTags(config.API_URL, result));
7778
});
7879
}, [params]);
7980

‎chatmap-ui/src/components/Map/index.jsx‎

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Popup from './popup';
66
import extent from 'turf-extent';
77
import {useMapDataContext} from '../../context/MapDataContext';
88
import useAPI from '../../components/ChatMap/useApi.js'
9+
import {MapTools, useMapTags} from '../Tags';
910
import {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
}

‎chatmap-ui/src/components/Map/popup.jsx‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {createRoot} from "react-dom/client";
55
import {Popup as PopupGL} from "maplibre-gl";
66
import Tagger from '../Tagger';
77
import Message from '../Message';
8+
import { PointTags } from '../Tags';
89
import {useIntl} from 'react-intl';
910
import {useLanguage} from "../../context/LanguageContext";
1011

@@ -60,6 +61,8 @@ export default function Popup({
6061
onRemoveTag,
6162
onRemoveMessage,
6263
allTags,
64+
pointTags,
65+
onChange,
6366
showMessageOptions,
6467
}) {
6568
const intl = useIntl();
@@ -96,7 +99,7 @@ export default function Popup({
9699
closeButton={true}
97100
>
98101
<IntlProvider defaultLocale="en" locale={lang} messages={messages}>
99-
<div className="activePopupFeatureContent">
102+
<div className={`activePopupFeatureContent ${pointTags ? "activePopupFeatureContent--tags" : ""}`}>
100103
<Message
101104
coordinates={feature.geometry.coordinates}
102105
message={feature.properties}
@@ -105,6 +108,19 @@ export default function Popup({
105108
onRemove={tag => onRemoveMessage(feature)}
106109
showMessageOptions={showMessageOptions}
107110
/>
111+
{/* When the map has keys, their values and the free tags go in one row */}
112+
{pointTags && !feature.properties.removed &&
113+
<PointTags
114+
key={feature.properties.id}
115+
{...pointTags}
116+
properties={feature.properties}
117+
allTags={allTags}
118+
onChange={properties => onChange({...feature, properties})}
119+
onAddTag={tag => onAddTag(tag, feature)}
120+
onRemoveTag={tag => onRemoveTag(tag, feature)}
121+
showEditOptions={showMessageOptions}
122+
/>
123+
}
108124
{survey.length > 0 &&
109125
<dl className="surveyAnswers">
110126
{survey.map((entry, index) => (
@@ -115,7 +131,7 @@ export default function Popup({
115131
))}
116132
</dl>
117133
}
118-
{!feature.properties.removed ?
134+
{!feature.properties.removed && !pointTags ?
119135
<Tagger
120136
placeholder={
121137
intl.formatMessage({

0 commit comments

Comments
 (0)