-
-
Notifications
You must be signed in to change notification settings - Fork 661
Expand file tree
/
Copy pathWmsSource.tsx
More file actions
421 lines (411 loc) · 16.5 KB
/
Copy pathWmsSource.tsx
File metadata and controls
421 lines (411 loc) · 16.5 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { Button, Input, Label, Select } from "@geolibre/ui";
import { ListTree, Loader2 } from "lucide-react";
import { useEffect, useId, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { buildWmsLayer } from "../apply-service";
import {
DEFAULT_WMS_ENDPOINT,
DEFAULT_WMS_LAYERS,
GEBCO_WMS_ENDPOINT,
GEBCO_WMS_LAYERS,
} from "../constants";
import {
fetchWmsCapabilities,
normalizeWmsVersion,
serviceRequestErrorMessage,
wmsVersionFromEndpoint,
type WmsLayerOption,
} from "../helpers";
import { ServiceLibrarySection } from "../ServiceLibrarySection";
import { serviceFieldBoolean, serviceFieldString, type ServiceFields } from "../service-library";
import { AddDataSourceForm, SampleDataSelect, useAddDataSource } from "../shared";
/**
* Retains the WMS form input across dialog close/reopen (in memory, for the
* session) so a user can add several layers from the same service without
* re-entering the URL or re-retrieving its layer list each time.
*/
interface WmsFormCache {
endpoint: string;
layers: string;
styles: string;
format: string;
transparent: boolean;
tileSize: string;
version: string;
versionTouched: boolean;
options: WmsLayerOption[];
}
let wmsFormCache: WmsFormCache | null = null;
export function WmsSource({
initialUrl = "",
initialLayers = "",
}: {
initialUrl?: string;
initialLayers?: string;
}) {
const { t } = useTranslation();
const source = useAddDataSource(t("addData.wms.defaultName"));
const [wmsEndpoint, setWmsEndpoint] = useState(initialUrl || wmsFormCache?.endpoint || "");
// A deep link brings its own service, so the layer and style left over from
// whichever service this dialog was last used for do not apply to it: pairing
// a fresh endpoint with a stale layer name would request a layer the service
// does not have.
const [wmsLayers, setWmsLayers] = useState(
initialLayers || (initialUrl ? "" : (wmsFormCache?.layers ?? "")),
);
const [wmsStyles, setWmsStyles] = useState(initialUrl ? "" : (wmsFormCache?.styles ?? ""));
const [wmsFormat, setWmsFormat] = useState(wmsFormCache?.format ?? "image/png");
const [wmsTransparent, setWmsTransparent] = useState(wmsFormCache?.transparent ?? true);
const [wmsTileSize, setWmsTileSize] = useState(wmsFormCache?.tileSize ?? "256");
const [wmsVersion, setWmsVersion] = useState(wmsFormCache?.version ?? "1.1.1");
// True while the version has an explicit source — the selector, a pasted
// URL's VERSION parameter, or a saved service entry. Capabilities
// auto-detection only fills the version in when no explicit source exists.
// Mirrored in a ref so the async retrieve handler reads the value current at
// response time, not the one captured when the button was clicked.
const [versionTouched, setVersionTouched] = useState(wmsFormCache?.versionTouched ?? false);
const versionTouchedRef = useRef(versionTouched);
const markVersionTouched = (touched: boolean) => {
versionTouchedRef.current = touched;
setVersionTouched(touched);
};
const [layerOptions, setLayerOptions] = useState<WmsLayerOption[]>(wmsFormCache?.options ?? []);
const [isRetrieving, setIsRetrieving] = useState(false);
const [retrieveError, setRetrieveError] = useState<string | null>(null);
const layerListId = useId();
// Persist the form input so reopening the dialog restores the URL, the fields,
// and the retrieved layer list.
useEffect(() => {
wmsFormCache = {
endpoint: wmsEndpoint,
layers: wmsLayers,
styles: wmsStyles,
format: wmsFormat,
transparent: wmsTransparent,
tileSize: wmsTileSize,
version: wmsVersion,
versionTouched,
options: layerOptions,
};
}, [
wmsEndpoint,
wmsLayers,
wmsStyles,
wmsFormat,
wmsTransparent,
wmsTileSize,
wmsVersion,
versionTouched,
layerOptions,
]);
// Guards against a stale in-flight retrieval overwriting the form after the
// user has moved on: a monotonic token identifies the latest request, and the
// AbortController cancels the previous one when a new request or an endpoint
// edit supersedes it.
const retrieveTokenRef = useRef(0);
const retrieveAbortRef = useRef<AbortController | null>(null);
const cancelRetrieve = () => {
retrieveAbortRef.current?.abort();
retrieveAbortRef.current = null;
};
// Abort an in-flight retrieval if the dialog closes mid-request, and advance
// the token so its finally block cannot set state after unmount.
useEffect(
() => () => {
retrieveTokenRef.current += 1;
retrieveAbortRef.current?.abort();
},
[],
);
const handleRetrieveLayers = async () => {
const endpoint = wmsEndpoint.trim();
if (!endpoint) {
setRetrieveError(t("addData.wms.errorUrl"));
return;
}
retrieveAbortRef.current?.abort();
const controller = new AbortController();
retrieveAbortRef.current = controller;
const token = ++retrieveTokenRef.current;
const isStale = () => token !== retrieveTokenRef.current || controller.signal.aborted;
setIsRetrieving(true);
setRetrieveError(null);
try {
const { layers: options, version } = await fetchWmsCapabilities(endpoint, {
signal: controller.signal,
});
if (isStale()) return;
if (options.length === 0) {
setLayerOptions([]);
setRetrieveError(t("addData.wms.noLayersFound"));
return;
}
setLayerOptions(options);
// Adopt the service's negotiated version so a 1.3.0-only server (e.g.
// the IGN Géoplateforme raster endpoint) gets a GetMap it accepts —
// unless the user already picked a version explicitly, since a server
// can negotiate GetCapabilities and GetMap differently. Read the ref,
// not the state: the user may have touched the selector while this
// request was in flight.
if (version && !versionTouchedRef.current) {
setWmsVersion(normalizeWmsVersion(version));
}
// Preselect the first layer when the field is empty so a single click
// leaves the form ready to submit.
if (!wmsLayers.trim()) setWmsLayers(options[0].name);
} catch (error) {
if (isStale()) return;
setLayerOptions([]);
setRetrieveError(serviceRequestErrorMessage(error, t, t("addData.wms.retrieveError")));
} finally {
if (token === retrieveTokenRef.current) setIsRetrieving(false);
}
};
const getFields = (): ServiceFields => ({
endpoint: wmsEndpoint,
layers: wmsLayers,
styles: wmsStyles,
format: wmsFormat,
transparent: wmsTransparent,
tileSize: wmsTileSize,
// Only persist the version when it has an explicit source; an untouched
// default stays eligible for URL/capabilities auto-detection on reload.
...(versionTouched ? { version: wmsVersion } : {}),
});
const applyFields = (fields: ServiceFields) => {
const endpoint = serviceFieldString(fields, "endpoint");
setWmsEndpoint(endpoint);
setWmsLayers(serviceFieldString(fields, "layers"));
setWmsStyles(serviceFieldString(fields, "styles"));
setWmsFormat(serviceFieldString(fields, "format", "image/png"));
setWmsTransparent(serviceFieldBoolean(fields, "transparent", true));
setWmsTileSize(serviceFieldString(fields, "tileSize", "256"));
// A saved service predating the version field falls back to the endpoint's
// own VERSION parameter (if any) rather than silently resetting to 1.1.1.
// Normalize whatever was stored so a hand-edited value (e.g. "1.3") still
// matches the selector's option pair. Either source is an explicit prior
// choice, so capabilities auto-detection must not override it later.
const savedVersion = serviceFieldString(fields, "version");
const detectedVersion = wmsVersionFromEndpoint(endpoint);
setWmsVersion(normalizeWmsVersion(savedVersion || detectedVersion || "1.1.1"));
markVersionTouched(Boolean(savedVersion || detectedVersion));
// The new endpoint's layers must be re-retrieved, so drop the old list and
// cancel any retrieval still in flight for the previous endpoint.
cancelRetrieve();
setLayerOptions([]);
setRetrieveError(null);
};
const handleSubmit = source.runSubmit(() => {
const name = source.layerName.trim() || t("addData.wms.defaultName");
if (!wmsEndpoint.trim()) throw new Error(t("addData.wms.errorUrl"));
if (!wmsLayers.trim()) {
throw new Error(t("addData.wms.errorLayers"));
}
// buildWmsLayer strips any leftover operation params (a pasted
// GetCapabilities URL), normalizes the version, and credits known keyless
// services (e.g. GEBCO) in the map's attribution control.
source.addAndClose(
buildWmsLayer({
name,
endpoint: wmsEndpoint,
layers: wmsLayers,
styles: wmsStyles,
format: wmsFormat,
transparent: wmsTransparent,
tileSize: wmsTileSize,
version: wmsVersion,
}),
);
});
return (
<AddDataSourceForm
layerName={source.layerName}
onLayerNameChange={source.setLayerName}
beforeLayerId={source.beforeLayerId}
onBeforeLayerIdChange={source.setBeforeLayerId}
onSubmit={handleSubmit}
error={source.error}
submitDisabled={source.isSubmitting}
useServiceIcon
>
<div className="space-y-3">
<ServiceLibrarySection
kind="wms"
layerName={source.layerName}
getFields={getFields}
onApply={(entry) => {
source.setLayerName(entry.name);
applyFields(entry.fields);
}}
/>
<div className="space-y-1.5">
<Label htmlFor="wms-endpoint">{t("addData.common.serviceUrl")}</Label>
<div className="flex gap-2">
<Input
id="wms-endpoint"
placeholder={t("addData.wms.urlPlaceholder")}
value={wmsEndpoint}
onChange={(event) => {
const value = event.target.value;
const previous = wmsEndpoint;
setWmsEndpoint(value);
// A pasted URL often carries the service's VERSION (stripped
// before the GetMap is built); adopt it so a 1.3.0-only server
// works without a manual version change, and treat it as an
// explicit source that capabilities auto-detection must not
// override. A different service (origin + path changed) always
// re-derives the version from its own URL — or the default —
// so a choice made for the previous service cannot leak onto
// it. Within the same service, only an actual change to the
// URL's declared VERSION is adopted; fixing an unrelated typo
// must not clobber a manual selection.
const detected = wmsVersionFromEndpoint(value);
const serviceChanged =
value.trim().split(/[?#]/)[0] !== previous.trim().split(/[?#]/)[0];
if (serviceChanged) {
setWmsVersion(detected ?? "1.1.1");
markVersionTouched(detected != null);
} else if (detected && detected !== wmsVersionFromEndpoint(previous)) {
setWmsVersion(detected);
markVersionTouched(true);
}
// Layers belong to the previous endpoint; clear them (and cancel
// any in-flight retrieval) so the list never reflects a
// different service.
if (layerOptions.length > 0 || isRetrieving) {
cancelRetrieve();
setLayerOptions([]);
setIsRetrieving(false);
}
if (retrieveError) setRetrieveError(null);
}}
/>
<Button
type="button"
variant="outline"
onClick={handleRetrieveLayers}
disabled={isRetrieving || !wmsEndpoint.trim()}
className="shrink-0"
>
{isRetrieving ? (
<Loader2 className="me-2 h-3.5 w-3.5 animate-spin" />
) : (
<ListTree className="me-2 h-3.5 w-3.5" />
)}
{isRetrieving ? t("addData.wms.retrieving") : t("addData.wms.retrieveLayers")}
</Button>
</div>
{retrieveError ? <p className="text-xs text-destructive">{retrieveError}</p> : null}
{layerOptions.length > 0 ? (
<div className="space-y-1.5">
<Label htmlFor={layerListId}>{t("addData.wms.retrievedLayers")}</Label>
{/* A picker that lists every retrieved layer and fills the Layers
field below on select. Its own value stays empty (an action
menu, like Load sample data), so it always shows the full list
and can never mismatch the free-text field. */}
<Select
id={layerListId}
value=""
onChange={(event) => {
if (event.target.value) setWmsLayers(event.target.value);
}}
>
<option value="" disabled>
{t("addData.wms.selectLayer", { count: layerOptions.length })}
</option>
{layerOptions.map((option) => (
<option key={option.name} value={option.name}>
{option.title === option.name
? option.name
: `${option.title} (${option.name})`}
</option>
))}
</Select>
</div>
) : null}
</div>
<div className="grid gap-3 sm:grid-cols-2">
<div className="space-y-1.5">
<Label htmlFor="wms-layers">{t("addData.wms.layers")}</Label>
{/* Plain free-text field: holds the submitted LAYERS value and stays
editable for a comma-separated composite value or manual entry.
The retrieved-layers picker above fills it. */}
<Input
id="wms-layers"
placeholder={t("addData.common.workspaceLayerPlaceholder")}
value={wmsLayers}
onChange={(event) => setWmsLayers(event.target.value)}
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="wms-styles">{t("addData.wms.styles")}</Label>
<Input
id="wms-styles"
value={wmsStyles}
onChange={(event) => setWmsStyles(event.target.value)}
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="wms-format">{t("addData.common.format")}</Label>
<Select
id="wms-format"
value={wmsFormat}
onChange={(event) => setWmsFormat(event.target.value)}
>
<option value="image/png">PNG</option>
<option value="image/jpeg">JPEG</option>
</Select>
</div>
<div className="space-y-1.5">
<Label htmlFor="wms-tile-size">{t("addData.common.tileSize")}</Label>
<Input
id="wms-tile-size"
inputMode="numeric"
value={wmsTileSize}
onChange={(event) => setWmsTileSize(event.target.value)}
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="wms-version">{t("addData.wms.version")}</Label>
<Select
id="wms-version"
value={wmsVersion}
onChange={(event) => {
setWmsVersion(event.target.value);
markVersionTouched(true);
}}
>
<option value="1.1.1">1.1.1</option>
<option value="1.3.0">1.3.0</option>
</Select>
</div>
</div>
<label className="flex items-center gap-2 text-sm">
<input
type="checkbox"
checked={wmsTransparent}
onChange={(event) => setWmsTransparent(event.target.checked)}
/>
{t("addData.wms.transparent")}
</label>
<SampleDataSelect
samples={[
{
label: t("addData.wms.sampleLabel"),
value: { endpoint: DEFAULT_WMS_ENDPOINT, layers: DEFAULT_WMS_LAYERS },
},
{
label: t("addData.wms.sampleLabelGebco"),
value: {
endpoint: GEBCO_WMS_ENDPOINT,
layers: GEBCO_WMS_LAYERS,
version: "1.3.0",
},
},
]}
onSelect={applyFields}
/>
</div>
</AddDataSourceForm>
);
}