-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
274 lines (241 loc) · 7.8 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import "./App.css";
import "leaflet/dist/leaflet.css";
import "leaflet/dist/leaflet";
import AddIcon from "@mui/icons-material/Add";
import DownloadIcon from "@mui/icons-material/Download";
import { MapContainer, TileLayer } from "react-leaflet";
import * as R from "ramda";
import * as turf from "@turf/turf";
import { useEffect, useState } from "react";
import { CurrentLocationMarker } from "./CurrentLocationMarker";
import {
CircularProgress,
Fab,
Tooltip,
MenuItem,
Select,
} from "@mui/material";
import osmtogeojson from "osmtogeojson";
import { useQuery } from "react-query";
import { DivIconMarker } from "./DivIconMarker";
import { IDMapMarker } from "./IDMapMarker";
import { AusPostData } from "./AusPostData";
import { AddNewMarker } from "./AddNewMarker";
import { EditModal } from "./EditModal";
import { Operation, applyPatch } from "fast-json-patch";
import { featureCollection } from "@turf/turf";
// TODO: fix this typing
import * as osmauth from "osm-auth";
const { osmAuth } = osmauth as any;
const DEV_MODE = true;
const PROD_API = "https://api.openstreetmap.org";
const DEV_API = "https://master.apis.dev.openstreetmap.org";
const DEV_OAUTH = {
client_id: "BVQH_lZQ1uEYlF6PplpVDmqVP0QD0RnbQdhR1ZkeFxY",
client_secret: "wS0o1OBKJ_EFmEN0HGdemTf-Vds-7aAjVKFPQgGAJGw",
};
const API_URL = DEV_MODE ? DEV_API : PROD_API;
const redirectPath = window.location.origin + window.location.pathname;
const auth = osmAuth({
...DEV_OAUTH,
redirect_uri: redirectPath,
scope: "read_prefs write_api write_notes",
auto: true,
url: DEV_MODE ? DEV_API : "https://openstreetmap.org",
});
const searchParams = new URLSearchParams(window.location.search);
if (searchParams.has("code")) {
// eslint-disable-next-line no-restricted-globals
opener && opener.authComplete(window.location.href);
window.close();
}
function App() {
const [display, setDisplay] = useState<"post_office" | "post_box">(
"post_office"
);
const [addingPoint, setAddingPoint] = useState<boolean>(false);
const [editId, setEditId] = useState<string>();
const [changes, setChanges] = useState<Record<string, Operation[]>>({});
useEffect(() => {
auth.authenticate((err: any, res: any) => {
if (err) {
// TODO: stylize
alert("Sorry we're having trouble logging you in");
}
console.log(res);
});
}, []);
const { data: osmQueryData, isLoading: isLoadingOsmApi } = useQuery(
["osmDataAgain"],
() => {
return fetch(
API_URL +
"/api/0.6/map?bbox=115.80551147460938,-32.053326175386076,115.80830097198486,-32.05200764122336",
{
headers: { Accept: "application/json" },
}
)
.then((res) => res.json())
.then((osmJson) => osmtogeojson(osmJson))
.then((osmGeoJson) => {
return turf.featureCollection(
osmGeoJson.features.filter(({ id }) =>
(typeof id === "string" ? id : "").startsWith("node/")
) as any
);
});
},
{
// the user should manually trigger this
staleTime: Infinity,
cacheTime: Infinity,
}
);
const osmDataWithChanges = featureCollection(
osmQueryData
? osmQueryData.features.map((f) => {
if (f.id && changes[f.id]) {
// do not mutate the document
return applyPatch(f, changes[f.id], undefined, false).newDocument;
}
return f;
})
: []
);
// While we send back the current changes, TODO: we also need to send the original
// state so that the patches are always derived from the OSM data.
const editFeature = osmDataWithChanges.features.find(
({ id }) => id === editId
);
const { data: presetDefaults } = useQuery(
["presetDefaults"],
() => {
return fetch(
"https://cdn.jsdelivr.net/npm/@openstreetmap/id-tagging-schema@3/dist/presets.min.json"
).then((res) => res.json());
},
{
// the user should manually trigger this
staleTime: Infinity,
cacheTime: Infinity,
}
);
return (
<div>
{false && (
<Select
sx={{ position: "absolute", top: 10, right: 10, zIndex: 1000 }}
value={display}
onChange={(e) => setDisplay((e as any).target.value)}
>
<MenuItem value="post_office">Post Office</MenuItem>
<MenuItem value="post_box">Post Box</MenuItem>
</Select>
)}
<Tooltip title="Download places for the current view" placement="right">
<Fab
color="primary"
size="small"
sx={{ position: "absolute", left: 10, bottom: 75 }}
>
{!isLoadingOsmApi ? (
<DownloadIcon />
) : (
<CircularProgress size="small" />
)}
</Fab>
</Tooltip>
{!addingPoint && (
<Tooltip title="Add a new place" placement="left">
<Fab
color="primary"
size="small"
sx={{ position: "absolute", right: 10, bottom: 25 }}
onClick={() => setAddingPoint(true)}
>
<AddIcon />
</Fab>
</Tooltip>
)}
{editId && editFeature && (
<EditModal
edit={editFeature}
onSave={(newChanges) => {
setChanges({
...changes,
[editId]: newChanges,
});
setEditId(undefined);
}}
onClose={() => setEditId(undefined)}
/>
)}
<MapContainer
center={[-32.0499, 115.8086]}
zoom={15}
maxZoom={25}
style={{ height: "100vh", width: "100wv" }}
zoomControl={false}
>
<>
{addingPoint && (
<AddNewMarker
onSave={(newLocation) => console.log({ newLocation })}
onClose={() => setAddingPoint(false)}
/>
)}
{osmDataWithChanges.features
.filter((f) => f?.properties?.highway !== "crossing")
.map((f, i) => {
const foundIcon = R.toPairs(f.properties || {})
.map(([key, value]) => `${key}/${value}`)
.map((k) => presetDefaults[k])
.filter(
(o) =>
typeof o === "object" &&
o !== null &&
typeof o.icon === "string" &&
(o.icon.startsWith("maki-") ||
o.icon.startsWith("temaki-") ||
o.icon.startsWith("fas-"))
)
.map((s) => s.icon)
.map((s) => (s.startsWith("maki-") ? s + "-15" : s));
const hasChanged =
changes[`${f.id}`] && changes[`${f.id}`].length > 0;
return (
<DivIconMarker
key={i}
marker={{
position: [...f.geometry.coordinates].reverse() as any,
eventHandlers: {
click: () => f.id && setEditId(`${f.id}`),
},
}}
container={{ tagName: "svg" }}
divIconProps={{
className: "leaflet-id-icon",
iconSize: [18, 24],
iconAnchor: [9, 25],
}}
>
<IDMapMarker iconId={foundIcon[0]} changed={hasChanged} />
</DivIconMarker>
);
})}
<CurrentLocationMarker />
{false && <AusPostData display={display} />}
<TileLayer
attribution={
'© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}
url="https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}@2x.png"
maxNativeZoom={18}
maxZoom={25}
/>
</>
</MapContainer>
</div>
);
}
export default App;