@quantaroute/checkout 1.7.1 → 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +18 -0
- package/README.md +1 -1
- package/dist/lib/index.d.ts +100 -2
- package/dist/lib/quantaroute-checkout.es.js +207 -193
- package/dist/lib/quantaroute-checkout.umd.js +137 -123
- package/dist/style.css +1 -1
- package/package.json +7 -3
- package/src/components/CheckoutWidget.tsx +9 -0
- package/src/components/MapPinSelector.native.tsx +2 -8
- package/src/components/MapPinSelector.tsx +257 -175
- package/src/core/map-runtime.ts +173 -0
- package/src/core/types.ts +36 -2
- package/src/index.ts +22 -0
- package/src/styles/checkout.css +19 -0
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import React, { useEffect, useRef, useState, useCallback, useId } from 'react';
|
|
2
2
|
import maplibregl from 'maplibre-gl';
|
|
3
3
|
import 'maplibre-gl/dist/maplibre-gl.css';
|
|
4
|
+
import type { MapMouseEvent, Map as MapLibreMap, Marker as MapLibreMarker } from 'maplibre-gl';
|
|
4
5
|
import { getDigiPin, getLatLngFromDigiPin, formatDigiPin, isWithinIndia } from '../core/digipin';
|
|
5
6
|
import { autocompleteAddress, looksLikeDigiPin, couldBeDigiPinInput } from '../core/api';
|
|
6
7
|
import type { AutocompleteSuggestion } from '../core/api';
|
|
7
8
|
import { useGeolocation } from '../hooks/useGeolocation';
|
|
8
9
|
import type { MapPinSelectorProps } from '../core/types';
|
|
10
|
+
import {
|
|
11
|
+
addQuantaRouteWatermark,
|
|
12
|
+
resolveQuantaRouteMapStyle,
|
|
13
|
+
waitForExternalMapRuntime,
|
|
14
|
+
type MapRuntime,
|
|
15
|
+
} from '../core/map-runtime';
|
|
9
16
|
|
|
10
17
|
// ─── Constants ───────────────────────────────────────────────────────────────
|
|
11
18
|
|
|
@@ -15,13 +22,7 @@ const MIN_ZOOM = 6; // users cannot zoom out beyond a useful India-level v
|
|
|
15
22
|
const OVERVIEW_ZOOM = 9;
|
|
16
23
|
const STREET_ZOOM = 18;
|
|
17
24
|
|
|
18
|
-
|
|
19
|
-
* Carto Positron – free vector basemap, no API key required.
|
|
20
|
-
* Attribution: © OpenStreetMap contributors © CARTO
|
|
21
|
-
* Terms: https://carto.com/legal/
|
|
22
|
-
*/
|
|
23
|
-
const CARTO_STYLE_URL =
|
|
24
|
-
'https://basemaps.cartocdn.com/gl/positron-gl-style/style.json';
|
|
25
|
+
type MapLoadState = 'loading' | 'ready' | 'unavailable' | 'error';
|
|
25
26
|
|
|
26
27
|
// ─── Pin dimensions ───────────────────────────────────────────────────────────
|
|
27
28
|
// MUST match the SVG width/height exactly.
|
|
@@ -99,6 +100,153 @@ function createPinElement(brandColor: string = DEFAULT_BRAND): HTMLDivElement {
|
|
|
99
100
|
return el;
|
|
100
101
|
}
|
|
101
102
|
|
|
103
|
+
function collapseCompactAttribution(container: HTMLElement | null): void {
|
|
104
|
+
const attribEl = container?.querySelector('.maplibregl-ctrl-attrib');
|
|
105
|
+
if (attribEl instanceof HTMLElement) {
|
|
106
|
+
attribEl.classList.add('maplibregl-compact');
|
|
107
|
+
attribEl.classList.remove('maplibregl-compact-show');
|
|
108
|
+
attribEl.removeAttribute('open');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function initMapWithRuntime(
|
|
113
|
+
runtime: MapRuntime,
|
|
114
|
+
container: HTMLDivElement,
|
|
115
|
+
markerRef: React.MutableRefObject<MapLibreMarker | null>,
|
|
116
|
+
options: {
|
|
117
|
+
styleUrl: string;
|
|
118
|
+
initLat: number;
|
|
119
|
+
initLng: number;
|
|
120
|
+
initZoom: number;
|
|
121
|
+
brandColor: string;
|
|
122
|
+
indiaBoundaryUrl?: string;
|
|
123
|
+
refreshDigipin: (la: number, lo: number) => void;
|
|
124
|
+
onReady: () => void;
|
|
125
|
+
onError: (message: string) => void;
|
|
126
|
+
}
|
|
127
|
+
): { map: MapLibreMap; cleanup: () => void } {
|
|
128
|
+
const {
|
|
129
|
+
styleUrl,
|
|
130
|
+
initLat,
|
|
131
|
+
initLng,
|
|
132
|
+
initZoom,
|
|
133
|
+
brandColor,
|
|
134
|
+
indiaBoundaryUrl,
|
|
135
|
+
refreshDigipin,
|
|
136
|
+
onReady,
|
|
137
|
+
onError,
|
|
138
|
+
} = options;
|
|
139
|
+
|
|
140
|
+
const boundaryPromise: Promise<unknown> = indiaBoundaryUrl
|
|
141
|
+
? fetch(indiaBoundaryUrl)
|
|
142
|
+
.then((r) => (r.ok ? r.json() : null))
|
|
143
|
+
.catch(() => null)
|
|
144
|
+
: Promise.resolve(null);
|
|
145
|
+
|
|
146
|
+
const map = new runtime.Map({
|
|
147
|
+
container,
|
|
148
|
+
style: styleUrl,
|
|
149
|
+
center: [initLng, initLat],
|
|
150
|
+
zoom: initZoom,
|
|
151
|
+
minZoom: MIN_ZOOM,
|
|
152
|
+
attributionControl: false,
|
|
153
|
+
maplibreLogo: false,
|
|
154
|
+
touchZoomRotate: true,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
if (typeof runtime.AttributionControl === 'function') {
|
|
158
|
+
map.addControl(
|
|
159
|
+
new runtime.AttributionControl({ compact: true }),
|
|
160
|
+
'bottom-right'
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
addQuantaRouteWatermark(map);
|
|
165
|
+
|
|
166
|
+
if (typeof runtime.NavigationControl === 'function') {
|
|
167
|
+
map.addControl(
|
|
168
|
+
new runtime.NavigationControl({ showCompass: false }),
|
|
169
|
+
'top-right'
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
let marker: MapLibreMarker | null = null;
|
|
174
|
+
|
|
175
|
+
map.on('style.load', () => {
|
|
176
|
+
collapseCompactAttribution(container);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
map.on('load', () => {
|
|
180
|
+
collapseCompactAttribution(container);
|
|
181
|
+
onReady();
|
|
182
|
+
|
|
183
|
+
boundaryPromise.then((geoJson) => {
|
|
184
|
+
// Our hosted style already draws the official outline; skip a second line.
|
|
185
|
+
if (styleUrl.includes('maps.quantaroute.com')) return;
|
|
186
|
+
if (!geoJson || !map.isStyleLoaded()) return;
|
|
187
|
+
try {
|
|
188
|
+
map.addSource('india-boundary', {
|
|
189
|
+
type: 'geojson',
|
|
190
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
191
|
+
data: geoJson as any,
|
|
192
|
+
});
|
|
193
|
+
map.addLayer({
|
|
194
|
+
id: 'india-boundary-line',
|
|
195
|
+
type: 'line',
|
|
196
|
+
source: 'india-boundary',
|
|
197
|
+
paint: {
|
|
198
|
+
'line-color': '#94a3b8',
|
|
199
|
+
'line-width': 1.5,
|
|
200
|
+
'line-opacity': 0.65,
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
} catch {
|
|
204
|
+
// Silent: duplicate source/layer on hot-reload is not an error
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
marker = new runtime.Marker({
|
|
209
|
+
element: createPinElement(brandColor),
|
|
210
|
+
draggable: true,
|
|
211
|
+
anchor: 'bottom',
|
|
212
|
+
})
|
|
213
|
+
.setLngLat([initLng, initLat])
|
|
214
|
+
.addTo(map);
|
|
215
|
+
|
|
216
|
+
markerRef.current = marker;
|
|
217
|
+
|
|
218
|
+
marker.on('drag', () => {
|
|
219
|
+
const pos = marker!.getLngLat();
|
|
220
|
+
refreshDigipin(pos.lat, pos.lng);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
marker.on('dragend', () => {
|
|
224
|
+
const pos = marker!.getLngLat();
|
|
225
|
+
map.easeTo({ center: [pos.lng, pos.lat], duration: 250 });
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
map.on('error', () => {
|
|
230
|
+
onError('Map tiles could not load. Please try again.');
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
map.on('click', (e: MapMouseEvent) => {
|
|
234
|
+
const pos = e.lngLat;
|
|
235
|
+
marker?.setLngLat([pos.lng, pos.lat]);
|
|
236
|
+
refreshDigipin(pos.lat, pos.lng);
|
|
237
|
+
map.easeTo({ center: [pos.lng, pos.lat], duration: 200 });
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
map,
|
|
242
|
+
cleanup: () => {
|
|
243
|
+
markerRef.current = null;
|
|
244
|
+
marker = null;
|
|
245
|
+
map.remove();
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
102
250
|
// ─── Component ───────────────────────────────────────────────────────────────
|
|
103
251
|
|
|
104
252
|
const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
@@ -112,10 +260,14 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
112
260
|
apiKey,
|
|
113
261
|
apiBaseUrl,
|
|
114
262
|
brandColor = DEFAULT_BRAND,
|
|
263
|
+
mapStyle,
|
|
264
|
+
mapTheme,
|
|
265
|
+
embedMode = 'bundled',
|
|
115
266
|
}) => {
|
|
267
|
+
const resolvedMapStyle = resolveQuantaRouteMapStyle(mapStyle, mapTheme);
|
|
116
268
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
117
|
-
const mapRef = useRef<
|
|
118
|
-
const markerRef = useRef<
|
|
269
|
+
const mapRef = useRef<MapLibreMap | null>(null);
|
|
270
|
+
const markerRef = useRef<MapLibreMarker | null>(null);
|
|
119
271
|
|
|
120
272
|
const initLat = defaultLat ?? INDIA_CENTER.lat;
|
|
121
273
|
const initLng = defaultLng ?? INDIA_CENTER.lng;
|
|
@@ -127,6 +279,8 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
127
279
|
computeDigiPinSafe(initLat, initLng)
|
|
128
280
|
);
|
|
129
281
|
const [mapLoaded, setMapLoaded] = useState(false);
|
|
282
|
+
const [mapState, setMapState] = useState<MapLoadState>('loading');
|
|
283
|
+
const [mapError, setMapError] = useState<string | null>(null);
|
|
130
284
|
|
|
131
285
|
// ── Search state ─────────────────────────────────────────────────────────
|
|
132
286
|
const [searchQuery, setSearchQuery] = useState('');
|
|
@@ -141,17 +295,15 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
141
295
|
const { loading: geoLoading, error: geoError, locate, clearError } = useGeolocation();
|
|
142
296
|
|
|
143
297
|
const refreshDigipin = useCallback((la: number, lo: number) => {
|
|
298
|
+
setLat(la);
|
|
299
|
+
setLng(lo);
|
|
144
300
|
setDigipin(computeDigiPinSafe(la, lo));
|
|
145
301
|
}, []);
|
|
146
302
|
|
|
147
303
|
// ── Fly map + move pin to a coord ────────────────────────────────────────
|
|
148
304
|
const flyToCoord = useCallback((la: number, lo: number) => {
|
|
149
|
-
setLat(la);
|
|
150
|
-
setLng(lo);
|
|
151
305
|
refreshDigipin(la, lo);
|
|
152
|
-
|
|
153
|
-
mapRef.current.flyTo({ center: [lo, la], zoom: STREET_ZOOM, duration: 900, essential: true });
|
|
154
|
-
}
|
|
306
|
+
mapRef.current?.flyTo({ center: [lo, la], zoom: STREET_ZOOM, duration: 900, essential: true });
|
|
155
307
|
markerRef.current?.setLngLat([lo, la]);
|
|
156
308
|
}, [refreshDigipin]);
|
|
157
309
|
|
|
@@ -165,7 +317,6 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
165
317
|
|
|
166
318
|
const trimmed = value.trim();
|
|
167
319
|
|
|
168
|
-
// Empty — close dropdown
|
|
169
320
|
if (!trimmed) {
|
|
170
321
|
setSuggestions([]);
|
|
171
322
|
setDropdownOpen(false);
|
|
@@ -173,8 +324,6 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
173
324
|
return;
|
|
174
325
|
}
|
|
175
326
|
|
|
176
|
-
// DigiPin input — resolve 100% offline, instant, zero API cost
|
|
177
|
-
// Keep the text in the input so the user can see what resolved.
|
|
178
327
|
if (looksLikeDigiPin(trimmed)) {
|
|
179
328
|
try {
|
|
180
329
|
const coords = getLatLngFromDigiPin(trimmed);
|
|
@@ -190,7 +339,6 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
190
339
|
return;
|
|
191
340
|
}
|
|
192
341
|
|
|
193
|
-
// Partial DigiPin (compact/spaced/dashed) — wait for full code, skip autocomplete
|
|
194
342
|
if (couldBeDigiPinInput(trimmed)) {
|
|
195
343
|
setSuggestions([]);
|
|
196
344
|
setDropdownOpen(false);
|
|
@@ -198,7 +346,6 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
198
346
|
return;
|
|
199
347
|
}
|
|
200
348
|
|
|
201
|
-
// Address search — debounce 400 ms, need ≥ 3 chars
|
|
202
349
|
if (trimmed.length < 3) {
|
|
203
350
|
setSuggestions([]);
|
|
204
351
|
setDropdownOpen(false);
|
|
@@ -209,7 +356,6 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
209
356
|
debounceRef.current = setTimeout(async () => {
|
|
210
357
|
try {
|
|
211
358
|
const key = apiKey ?? '';
|
|
212
|
-
// Forward current pin position so the API can bias/bound results nearby
|
|
213
359
|
const results = await autocompleteAddress(trimmed, key, apiBaseUrl, 5, lat, lng);
|
|
214
360
|
setSuggestions(results);
|
|
215
361
|
setDropdownOpen(results.length > 0);
|
|
@@ -221,9 +367,8 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
221
367
|
setSearchLoading(false);
|
|
222
368
|
}
|
|
223
369
|
}, 400);
|
|
224
|
-
}, [apiKey, apiBaseUrl, flyToCoord]);
|
|
370
|
+
}, [apiKey, apiBaseUrl, flyToCoord, lat, lng]);
|
|
225
371
|
|
|
226
|
-
// ── Pick a suggestion from the dropdown ──────────────────────────────────
|
|
227
372
|
const handleSuggestionSelect = useCallback((s: AutocompleteSuggestion) => {
|
|
228
373
|
flyToCoord(s.coordinates.latitude, s.coordinates.longitude);
|
|
229
374
|
setSearchQuery('');
|
|
@@ -240,159 +385,96 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
240
385
|
}
|
|
241
386
|
}, []);
|
|
242
387
|
|
|
243
|
-
// ── Map initialization
|
|
388
|
+
// ── Map initialization ────────────────────────────────────────────────────
|
|
244
389
|
useEffect(() => {
|
|
245
390
|
if (!containerRef.current) return;
|
|
246
391
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
// Silent failure: the boundary is a legal safeguard, not critical UI.
|
|
251
|
-
const boundaryPromise: Promise<unknown> = indiaBoundaryUrl
|
|
252
|
-
? fetch(indiaBoundaryUrl)
|
|
253
|
-
.then((r) => (r.ok ? r.json() : null))
|
|
254
|
-
.catch(() => null)
|
|
255
|
-
: Promise.resolve(null);
|
|
392
|
+
let cancelled = false;
|
|
393
|
+
let cancelWait: (() => void) | null = null;
|
|
394
|
+
let mapCleanup: (() => void) | null = null;
|
|
256
395
|
|
|
257
396
|
const initZoom = hasDefault ? STREET_ZOOM : OVERVIEW_ZOOM;
|
|
397
|
+
const container = containerRef.current;
|
|
258
398
|
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
style: CARTO_STYLE_URL,
|
|
262
|
-
center: [initLng, initLat],
|
|
263
|
-
zoom: initZoom,
|
|
264
|
-
minZoom: MIN_ZOOM, // lock: cannot zoom out past level 7
|
|
265
|
-
attributionControl: false,
|
|
266
|
-
// Smooth touch gestures on mobile (tap center for zoom)
|
|
267
|
-
touchZoomRotate: true,
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
// Attribution (required by Carto & OSM terms)
|
|
271
|
-
map.addControl(
|
|
272
|
-
new maplibregl.AttributionControl({
|
|
273
|
-
compact: true,
|
|
274
|
-
customAttribution:
|
|
275
|
-
'© <a href="https://www.openstreetmap.org/copyright" target="_blank" rel="noopener">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions" target="_blank" rel="noopener">CARTO</a>',
|
|
276
|
-
}),
|
|
277
|
-
'bottom-right'
|
|
278
|
-
);
|
|
279
|
-
|
|
280
|
-
// Zoom +/− (hidden on small screens via CSS)
|
|
281
|
-
map.addControl(
|
|
282
|
-
new maplibregl.NavigationControl({ showCompass: false }),
|
|
283
|
-
'top-right'
|
|
284
|
-
);
|
|
399
|
+
const mountMap = (runtime: MapRuntime) => {
|
|
400
|
+
if (cancelled || !containerRef.current) return;
|
|
285
401
|
|
|
286
|
-
|
|
287
|
-
|
|
402
|
+
try {
|
|
403
|
+
const { map, cleanup } = initMapWithRuntime(runtime, container, markerRef, {
|
|
404
|
+
styleUrl: resolvedMapStyle,
|
|
405
|
+
initLat,
|
|
406
|
+
initLng,
|
|
407
|
+
initZoom,
|
|
408
|
+
brandColor,
|
|
409
|
+
indiaBoundaryUrl,
|
|
410
|
+
refreshDigipin,
|
|
411
|
+
onReady: () => {
|
|
412
|
+
if (cancelled) return;
|
|
413
|
+
setMapLoaded(true);
|
|
414
|
+
setMapState('ready');
|
|
415
|
+
setMapError(null);
|
|
416
|
+
},
|
|
417
|
+
onError: (message) => {
|
|
418
|
+
if (cancelled) return;
|
|
419
|
+
setMapState((state) => (state === 'ready' ? state : 'error'));
|
|
420
|
+
setMapError(message);
|
|
421
|
+
},
|
|
422
|
+
});
|
|
288
423
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
attribEl.classList.remove('maplibregl-compact-show');
|
|
295
|
-
attribEl.setAttribute('open', '');
|
|
424
|
+
mapRef.current = map;
|
|
425
|
+
mapCleanup = cleanup;
|
|
426
|
+
} catch (err) {
|
|
427
|
+
setMapState('error');
|
|
428
|
+
setMapError(err instanceof Error ? err.message : 'Map failed to initialize.');
|
|
296
429
|
}
|
|
430
|
+
};
|
|
297
431
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
try {
|
|
304
|
-
map.addSource('india-boundary', {
|
|
305
|
-
type: 'geojson',
|
|
306
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
307
|
-
data: geoJson as any,
|
|
308
|
-
});
|
|
309
|
-
map.addLayer({
|
|
310
|
-
id: 'india-boundary-line',
|
|
311
|
-
type: 'line',
|
|
312
|
-
source: 'india-boundary',
|
|
313
|
-
paint: {
|
|
314
|
-
'line-color': '#94a3b8', // slate-400 — subtle grey
|
|
315
|
-
'line-width': 1.5,
|
|
316
|
-
'line-opacity': 0.65,
|
|
317
|
-
},
|
|
318
|
-
});
|
|
319
|
-
} catch {
|
|
320
|
-
// Silent: duplicate source/layer on hot-reload is not an error
|
|
321
|
-
}
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
// Create draggable pin marker.
|
|
325
|
-
// anchor='bottom' → MapLibre aligns element's bottom-center with the coord.
|
|
326
|
-
// Our SVG tip is at (20, 52) = exactly the bottom-center of the 40×52 element.
|
|
327
|
-
// No offset needed — any offset shifts the tip away from the coordinate.
|
|
328
|
-
const pinEl = createPinElement(brandColor);
|
|
329
|
-
const marker = new maplibregl.Marker({
|
|
330
|
-
element: pinEl,
|
|
331
|
-
draggable: true,
|
|
332
|
-
anchor: 'bottom',
|
|
333
|
-
})
|
|
334
|
-
.setLngLat([initLng, initLat])
|
|
335
|
-
.addTo(map);
|
|
336
|
-
|
|
337
|
-
// Live DigiPin update while dragging
|
|
338
|
-
marker.on('drag', () => {
|
|
339
|
-
const pos = marker.getLngLat();
|
|
340
|
-
setLat(pos.lat);
|
|
341
|
-
setLng(pos.lng);
|
|
342
|
-
refreshDigipin(pos.lat, pos.lng);
|
|
343
|
-
});
|
|
344
|
-
|
|
345
|
-
// Re-center map after drag ends
|
|
346
|
-
marker.on('dragend', () => {
|
|
347
|
-
const pos = marker.getLngLat();
|
|
348
|
-
map.easeTo({ center: [pos.lng, pos.lat], duration: 250 });
|
|
349
|
-
});
|
|
350
|
-
|
|
351
|
-
markerRef.current = marker;
|
|
352
|
-
// DigiPin is already computed in useState() init; no need to recompute here.
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
// Tap/click map to reposition pin
|
|
356
|
-
map.on('click', (e) => {
|
|
357
|
-
const pos = e.lngLat;
|
|
358
|
-
markerRef.current?.setLngLat([pos.lng, pos.lat]);
|
|
359
|
-
setLat(pos.lat);
|
|
360
|
-
setLng(pos.lng);
|
|
361
|
-
refreshDigipin(pos.lat, pos.lng);
|
|
362
|
-
map.easeTo({ center: [pos.lng, pos.lat], duration: 200 });
|
|
363
|
-
});
|
|
432
|
+
const handleUnavailable = () => {
|
|
433
|
+
if (cancelled) return;
|
|
434
|
+
setMapState('unavailable');
|
|
435
|
+
setMapError('Map could not load. Check your connection and try again.');
|
|
436
|
+
};
|
|
364
437
|
|
|
365
|
-
|
|
438
|
+
if (embedMode === 'external') {
|
|
439
|
+
cancelWait = waitForExternalMapRuntime(mountMap, handleUnavailable);
|
|
440
|
+
} else {
|
|
441
|
+
mountMap(maplibregl as MapRuntime);
|
|
442
|
+
}
|
|
366
443
|
|
|
367
444
|
return () => {
|
|
368
|
-
|
|
369
|
-
|
|
445
|
+
cancelled = true;
|
|
446
|
+
cancelWait?.();
|
|
447
|
+
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
370
448
|
markerRef.current = null;
|
|
449
|
+
mapRef.current = null;
|
|
450
|
+
mapCleanup?.();
|
|
371
451
|
};
|
|
372
|
-
|
|
373
|
-
|
|
452
|
+
}, [
|
|
453
|
+
brandColor,
|
|
454
|
+
embedMode,
|
|
455
|
+
hasDefault,
|
|
456
|
+
indiaBoundaryUrl,
|
|
457
|
+
initLat,
|
|
458
|
+
initLng,
|
|
459
|
+
resolvedMapStyle,
|
|
460
|
+
refreshDigipin,
|
|
461
|
+
]);
|
|
374
462
|
|
|
375
463
|
// ── Geolocation handler ───────────────────────────────────────────────────
|
|
376
464
|
const handleLocateMe = useCallback(() => {
|
|
377
465
|
clearError();
|
|
378
466
|
locate((userLat, userLng) => {
|
|
379
|
-
setLat(userLat);
|
|
380
|
-
setLng(userLng);
|
|
381
467
|
refreshDigipin(userLat, userLng);
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
essential: true,
|
|
389
|
-
});
|
|
390
|
-
}
|
|
468
|
+
mapRef.current?.flyTo({
|
|
469
|
+
center: [userLng, userLat],
|
|
470
|
+
zoom: STREET_ZOOM,
|
|
471
|
+
duration: 1400,
|
|
472
|
+
essential: true,
|
|
473
|
+
});
|
|
391
474
|
markerRef.current?.setLngLat([userLng, userLat]);
|
|
392
475
|
});
|
|
393
476
|
}, [locate, clearError, refreshDigipin]);
|
|
394
477
|
|
|
395
|
-
// ── Confirm handler ───────────────────────────────────────────────────────
|
|
396
478
|
const handleConfirm = useCallback(() => {
|
|
397
479
|
if (digipin) {
|
|
398
480
|
onLocationConfirm(lat, lng, digipin);
|
|
@@ -400,11 +482,11 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
400
482
|
}, [lat, lng, digipin, onLocationConfirm]);
|
|
401
483
|
|
|
402
484
|
const isInIndia = digipin !== null;
|
|
403
|
-
const canConfirm = isInIndia && mapLoaded;
|
|
485
|
+
const canConfirm = isInIndia && mapLoaded && mapState === 'ready';
|
|
486
|
+
const showMapStatus = mapState !== 'ready';
|
|
404
487
|
|
|
405
488
|
return (
|
|
406
489
|
<div className="qr-map-wrapper">
|
|
407
|
-
{/* ── Step header ── */}
|
|
408
490
|
<div className="qr-step-header">
|
|
409
491
|
<div className="qr-step-badge">1</div>
|
|
410
492
|
<div className="qr-step-text">
|
|
@@ -415,11 +497,9 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
415
497
|
</div>
|
|
416
498
|
</div>
|
|
417
499
|
|
|
418
|
-
{/* ── Search bar ── */}
|
|
419
500
|
{enableSearch && (
|
|
420
501
|
<div className="qr-search-bar" role="search">
|
|
421
502
|
<div className="qr-search-bar__input-wrap">
|
|
422
|
-
{/* Search icon */}
|
|
423
503
|
<svg
|
|
424
504
|
className="qr-search-bar__icon"
|
|
425
505
|
aria-hidden="true"
|
|
@@ -452,12 +532,10 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
452
532
|
aria-expanded={dropdownOpen}
|
|
453
533
|
/>
|
|
454
534
|
|
|
455
|
-
{/* Inline loading spinner */}
|
|
456
535
|
{searchLoading && (
|
|
457
536
|
<span className="qr-search-bar__spinner qr-spinner qr-spinner--sm" aria-hidden="true" />
|
|
458
537
|
)}
|
|
459
538
|
|
|
460
|
-
{/* Clear button */}
|
|
461
539
|
{searchQuery && !searchLoading && (
|
|
462
540
|
<button
|
|
463
541
|
type="button"
|
|
@@ -471,12 +549,10 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
471
549
|
)}
|
|
472
550
|
</div>
|
|
473
551
|
|
|
474
|
-
{/* Inline error */}
|
|
475
552
|
{searchError && (
|
|
476
553
|
<p className="qr-search-bar__error" role="alert">{searchError}</p>
|
|
477
554
|
)}
|
|
478
555
|
|
|
479
|
-
{/* Suggestions dropdown */}
|
|
480
556
|
{dropdownOpen && suggestions.length > 0 && (
|
|
481
557
|
<ul
|
|
482
558
|
id={listboxId}
|
|
@@ -491,7 +567,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
491
567
|
role="option"
|
|
492
568
|
aria-selected="false"
|
|
493
569
|
onMouseDown={(e) => {
|
|
494
|
-
e.preventDefault();
|
|
570
|
+
e.preventDefault();
|
|
495
571
|
handleSuggestionSelect(s);
|
|
496
572
|
}}
|
|
497
573
|
>
|
|
@@ -519,7 +595,6 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
519
595
|
</ul>
|
|
520
596
|
)}
|
|
521
597
|
|
|
522
|
-
{/* DigiPin hint chip */}
|
|
523
598
|
<p className="qr-search-bar__hint">
|
|
524
599
|
<span className="qr-search-bar__hint-chip">DigiPin</span>
|
|
525
600
|
Type your DigiPin code for instant offline lookup — no API call
|
|
@@ -527,12 +602,22 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
527
602
|
</div>
|
|
528
603
|
)}
|
|
529
604
|
|
|
530
|
-
{/* ── Map area ── */}
|
|
531
605
|
<div className="qr-map-outer" style={{ height: mapHeight }}>
|
|
532
|
-
{/* MapLibre GL container */}
|
|
533
606
|
<div ref={containerRef} className="qr-map-canvas" />
|
|
534
607
|
|
|
535
|
-
{
|
|
608
|
+
{showMapStatus && (
|
|
609
|
+
<div className="qr-map-notice" role={mapState === 'loading' ? 'status' : 'alert'}>
|
|
610
|
+
{mapState === 'loading' ? (
|
|
611
|
+
<>
|
|
612
|
+
<span className="qr-spinner qr-spinner--sm" aria-hidden="true" />
|
|
613
|
+
<span>Loading map…</span>
|
|
614
|
+
</>
|
|
615
|
+
) : (
|
|
616
|
+
mapError ?? 'Map is unavailable. Please try again.'
|
|
617
|
+
)}
|
|
618
|
+
</div>
|
|
619
|
+
)}
|
|
620
|
+
|
|
536
621
|
{digipin && (
|
|
537
622
|
<div className="qr-digipin-badge" aria-live="polite" aria-label={`DigiPin: ${digipin}`}>
|
|
538
623
|
<span className="qr-digipin-badge__label">DigiPin</span>
|
|
@@ -540,18 +625,16 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
540
625
|
</div>
|
|
541
626
|
)}
|
|
542
627
|
|
|
543
|
-
{/* Out-of-India notice */}
|
|
544
628
|
{!isInIndia && mapLoaded && (
|
|
545
629
|
<div className="qr-map-notice" role="status">
|
|
546
630
|
Move map to India to get a DigiPin
|
|
547
631
|
</div>
|
|
548
632
|
)}
|
|
549
633
|
|
|
550
|
-
{/* Locate-me button */}
|
|
551
634
|
<button
|
|
552
635
|
className={`qr-locate-btn${geoLoading ? ' qr-locate-btn--loading' : ''}`}
|
|
553
636
|
onClick={handleLocateMe}
|
|
554
|
-
disabled={geoLoading}
|
|
637
|
+
disabled={geoLoading || mapState !== 'ready'}
|
|
555
638
|
title="Use my current location"
|
|
556
639
|
aria-label="Use my current location"
|
|
557
640
|
type="button"
|
|
@@ -564,23 +647,17 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
564
647
|
viewBox="0 0 24 24"
|
|
565
648
|
fill="none"
|
|
566
649
|
stroke="currentColor"
|
|
567
|
-
strokeWidth="2"
|
|
650
|
+
strokeWidth="2"
|
|
568
651
|
strokeLinecap="round"
|
|
569
652
|
strokeLinejoin="round"
|
|
570
653
|
>
|
|
571
|
-
{/* 1. Inner solid dot */}
|
|
572
654
|
<circle cx="12" cy="12" r="3" fill="currentColor" stroke="none" />
|
|
573
|
-
|
|
574
|
-
{/* 2. Outer ring */}
|
|
575
655
|
<circle cx="12" cy="12" r="7" />
|
|
576
|
-
|
|
577
|
-
{/* 3. Crosshair bumps attached to the ring */}
|
|
578
656
|
<path d="M12 2v3M12 19v3M2 12h3M19 12h3" />
|
|
579
657
|
</svg>
|
|
580
658
|
)}
|
|
581
659
|
</button>
|
|
582
660
|
|
|
583
|
-
{/* Geo error toast */}
|
|
584
661
|
{geoError && (
|
|
585
662
|
<div className="qr-geo-error" role="alert">
|
|
586
663
|
<span>{geoError}</span>
|
|
@@ -596,7 +673,6 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
596
673
|
)}
|
|
597
674
|
</div>
|
|
598
675
|
|
|
599
|
-
{/* ── Coordinates strip ── */}
|
|
600
676
|
{mapLoaded && (
|
|
601
677
|
<div className="qr-coords-strip" aria-label="Current pin coordinates">
|
|
602
678
|
<span>{lat.toFixed(5)}° N</span>
|
|
@@ -605,7 +681,6 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
605
681
|
</div>
|
|
606
682
|
)}
|
|
607
683
|
|
|
608
|
-
{/* ── Confirm action bar ── */}
|
|
609
684
|
<div className="qr-map-actions">
|
|
610
685
|
<button
|
|
611
686
|
type="button"
|
|
@@ -629,6 +704,13 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
629
704
|
{!isInIndia && mapLoaded && (
|
|
630
705
|
<p className="qr-map-hint">Move the pin to a location in India to continue</p>
|
|
631
706
|
)}
|
|
707
|
+
{mapState !== 'ready' && (
|
|
708
|
+
<p className="qr-map-hint">
|
|
709
|
+
{mapState === 'loading'
|
|
710
|
+
? 'Loading the map before location confirmation.'
|
|
711
|
+
: 'Map is unavailable, so location confirmation is paused.'}
|
|
712
|
+
</p>
|
|
713
|
+
)}
|
|
632
714
|
</div>
|
|
633
715
|
</div>
|
|
634
716
|
);
|