@wasto/cms-components 1.5.10 → 1.5.11

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.
Files changed (98) hide show
  1. package/README.md +47 -47
  2. package/dist/index.d.mts +103 -2
  3. package/dist/index.d.ts +103 -2
  4. package/dist/index.js +2861 -2442
  5. package/dist/index.mjs +3651 -3227
  6. package/package.json +2 -2
  7. package/src/components/announcement-modal/AnnouncementModal.tsx +183 -183
  8. package/src/components/article/Article.tsx +21 -21
  9. package/src/components/article/ArticleCard.tsx +81 -81
  10. package/src/components/article/ArticleExcerpts.tsx +80 -80
  11. package/src/components/article/ArticleList.tsx +114 -114
  12. package/src/components/article/ArticleMarkdown.tsx +170 -170
  13. package/src/components/article/ArticleReaderModal.tsx +93 -93
  14. package/src/components/article/ArticleSingle.tsx +57 -57
  15. package/src/components/bottom-tab/BottomTab.tsx +392 -392
  16. package/src/components/button/Button.tsx +154 -154
  17. package/src/components/challenge/Badges.tsx +84 -84
  18. package/src/components/challenge/BadgesModal.tsx +166 -166
  19. package/src/components/challenge/Challenge.tsx +380 -380
  20. package/src/components/challenge/ChallengeModal.tsx +231 -231
  21. package/src/components/challenge/ChallengeVignette.tsx +249 -249
  22. package/src/components/challenge/ChallengesModal.tsx +243 -243
  23. package/src/components/challenge/QuizModal.tsx +381 -381
  24. package/src/components/challenge/ResultModal.tsx +144 -144
  25. package/src/components/chat/Chat.tsx +928 -928
  26. package/src/components/collection-calendar/CollectionCalendar.tsx +274 -274
  27. package/src/components/columns/Columns.tsx +33 -33
  28. package/src/components/consignes-modal/ConsignesModal.tsx +267 -267
  29. package/src/components/content-inset/ContentInset.tsx +33 -33
  30. package/src/components/header/Header.tsx +391 -391
  31. package/src/components/image/Image.tsx +15 -15
  32. package/src/components/image/ImageFrame.tsx +102 -102
  33. package/src/components/image/ImageMedia.tsx +31 -31
  34. package/src/components/image/ImageUrl.tsx +30 -30
  35. package/src/components/locations-map/LocationSheet.tsx +417 -417
  36. package/src/components/locations-map/LocationsMap.native.tsx +260 -260
  37. package/src/components/locations-map/LocationsMap.tsx +13 -13
  38. package/src/components/locations-map/LocationsMap.web.tsx +10 -10
  39. package/src/components/locations-map/LocationsMapShell.tsx +527 -527
  40. package/src/components/locations-map/LocationsMapView.tsx +108 -108
  41. package/src/components/locations-map/LocationsMapView.web.tsx +331 -331
  42. package/src/components/nav-button/NavButton.tsx +59 -59
  43. package/src/components/next-collection/NextCollection.tsx +636 -621
  44. package/src/components/notifications/NotificationsScreen.tsx +247 -247
  45. package/src/components/page/Page.tsx +102 -64
  46. package/src/components/parapraph/Paragraph.tsx +67 -67
  47. package/src/components/profile/ProfileScreen.tsx +236 -236
  48. package/src/components/recycling-info/RecyclingInfo.tsx +189 -189
  49. package/src/components/safe-area/OptionalSafeAreaProvider.tsx +17 -17
  50. package/src/components/search/Search.tsx +320 -320
  51. package/src/components/select-street-modal/SelectStreetModal.tsx +475 -475
  52. package/src/components/title/Title.tsx +100 -100
  53. package/src/components/vision/Vision.tsx +447 -447
  54. package/src/components/vision/VisionActionCard.tsx +117 -117
  55. package/src/components/vision/VisionCapture.tsx +30 -30
  56. package/src/components/vision/VisionTapOverlay.tsx +122 -122
  57. package/src/contexts/AnnouncementsContext.tsx +171 -171
  58. package/src/contexts/AppDataContext.tsx +458 -0
  59. package/src/contexts/ChallengesContext.tsx +182 -182
  60. package/src/contexts/LocalContext.tsx +227 -227
  61. package/src/contexts/ThemeContext.tsx +37 -37
  62. package/src/contexts/WastoContext.tsx +130 -127
  63. package/src/helpers/blogPost.ts +72 -72
  64. package/src/helpers/generateQuizQuestions.ts +247 -247
  65. package/src/helpers/getContrastColor.ts +18 -18
  66. package/src/helpers/haptics.ts +20 -20
  67. package/src/helpers/hoursSchedule.ts +230 -230
  68. package/src/helpers/htmlContentToMarkdown.ts +114 -114
  69. package/src/helpers/layout.ts +54 -54
  70. package/src/helpers/loadLinearGradient.ts +22 -22
  71. package/src/helpers/mapLocations.ts +170 -0
  72. package/src/helpers/maquetteTheme.ts +76 -76
  73. package/src/helpers/markdownUnderlinePlugin.ts +32 -32
  74. package/src/helpers/nextCollection.ts +122 -0
  75. package/src/helpers/normalizeWasteTypeRecyclingInfo.ts +80 -80
  76. package/src/helpers/publicMediaUrl.ts +8 -8
  77. package/src/helpers/resolveApiAssetUrl.ts +21 -21
  78. package/src/helpers/systemRoutes.ts +7 -7
  79. package/src/helpers/themeStyles.ts +114 -114
  80. package/src/helpers/ttlCache.ts +41 -0
  81. package/src/helpers/useSafeAreaPadding.ts +14 -14
  82. package/src/hooks/useBadges.ts +100 -100
  83. package/src/hooks/useBlogPosts.ts +109 -105
  84. package/src/hooks/useChallenges.ts +110 -110
  85. package/src/hooks/useCities.ts +56 -56
  86. package/src/hooks/useCityAnnouncements.ts +118 -118
  87. package/src/hooks/useCollectionCalendar.ts +421 -421
  88. package/src/hooks/useColumns.ts +19 -19
  89. package/src/hooks/useConsignes.ts +62 -62
  90. package/src/hooks/useLocations.ts +120 -248
  91. package/src/hooks/useNextCollection.ts +163 -238
  92. package/src/hooks/useRecyclingInfo.ts +90 -90
  93. package/src/hooks/useWastoClient.ts +16 -16
  94. package/src/index.ts +149 -142
  95. package/src/navigation/NavigationContext.tsx +56 -56
  96. package/src/sentry/initSentry.ts +120 -120
  97. package/src/types/Theme.ts +72 -72
  98. package/src/types/css-modules.d.ts +1 -1
@@ -1,331 +1,331 @@
1
- import {
2
- createElement,
3
- useEffect,
4
- useMemo,
5
- useRef,
6
- useState,
7
- } from "react";
8
- import { StyleSheet, Text, View } from "react-native";
9
- import type { Map as MapLibreMap, Marker } from "maplibre-gl";
10
- import {
11
- LOCATION_CATEGORY_META,
12
- REUNION_CENTER,
13
- isValidLatLon,
14
- type LocationCategory,
15
- } from "../../hooks/useLocations";
16
- import { getContrastColor } from "../../helpers/getContrastColor";
17
- import type { LocationsMapViewProps } from "./LocationsMapView";
18
-
19
- import "maplibre-gl/dist/maplibre-gl.css";
20
-
21
- /** Vector style (parity with native). Raster fallback if OpenFreeMap fails (CSP/iframe). */
22
- const OPENFREEMAP_STYLE = "https://tiles.openfreemap.org/styles/liberty";
23
- const OSM_RASTER_STYLE = {
24
- version: 8 as const,
25
- sources: {
26
- osm: {
27
- type: "raster" as const,
28
- tiles: ["https://tile.openstreetmap.org/{z}/{x}/{y}.png"],
29
- tileSize: 256,
30
- attribution: "© OpenStreetMap contributors",
31
- },
32
- },
33
- layers: [{ id: "osm", type: "raster" as const, source: "osm" }],
34
- };
35
- const MAX_MARKERS = 80;
36
-
37
- /** maplibre-gl@4 dynamic import may be `{ default: ns }` under Metro/webpack. */
38
- type MapLibreGL = {
39
- Map: new (options: {
40
- container: HTMLElement;
41
- style: string | typeof OSM_RASTER_STYLE;
42
- center: [number, number];
43
- zoom: number;
44
- }) => MapLibreMap;
45
- Marker: new (options?: { element?: HTMLElement; anchor?: string }) => Marker;
46
- LngLatBounds: new (
47
- sw: [number, number],
48
- ne: [number, number],
49
- ) => { extend: (coord: [number, number]) => unknown };
50
- };
51
-
52
- async function loadMapLibre(): Promise<MapLibreGL> {
53
- const mod = await import("maplibre-gl");
54
- return ((mod as { default?: MapLibreGL }).default ?? mod) as MapLibreGL;
55
- }
56
-
57
- function buildMarkerElement(category: LocationCategory, selected: boolean) {
58
- const color =
59
- LOCATION_CATEGORY_META[category]?.color ??
60
- LOCATION_CATEGORY_META.recycling.color;
61
- const iconColor = getContrastColor(color);
62
- const el = document.createElement("button");
63
- el.type = "button";
64
- el.setAttribute("aria-label", LOCATION_CATEGORY_META[category]?.label ?? "Lieu");
65
- el.style.cssText = [
66
- "display:flex",
67
- "align-items:center",
68
- "justify-content:center",
69
- "width:32px",
70
- "height:32px",
71
- "padding:0",
72
- "border:none",
73
- "background:transparent",
74
- "cursor:pointer",
75
- ].join(";");
76
- el.innerHTML = `
77
- <span style="
78
- display:flex;align-items:center;justify-content:center;
79
- width:32px;height:32px;border-radius:9999px;
80
- background:${color};
81
- border:3px solid ${selected ? "#111827" : "#ffffff"};
82
- box-shadow:0 1px 3px rgba(0,0,0,0.25);
83
- transform:scale(${selected ? 1.12 : 1});
84
- ">
85
- <svg width="16" height="16" viewBox="0 0 24 24" fill="none"
86
- stroke="${iconColor}" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
87
- <path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z"/>
88
- <circle cx="12" cy="10" r="3"/>
89
- </svg>
90
- </span>
91
- `;
92
- return el;
93
- }
94
-
95
- /**
96
- * Web MapLibre GL JS canvas (Metro resolves `.web.tsx` over the pin fallback).
97
- * Same OpenFreeMap style as native + Studio PWA preview.
98
- */
99
- export function LocationsMapView({
100
- locations,
101
- center,
102
- selectedId,
103
- onSelect,
104
- }: LocationsMapViewProps) {
105
- const containerRef = useRef<HTMLDivElement | null>(null);
106
- const mapRef = useRef<MapLibreMap | null>(null);
107
- const maplibreRef = useRef<MapLibreGL | null>(null);
108
- const markersRef = useRef<Array<{ id: string; marker: Marker }>>([]);
109
- const onSelectRef = useRef(onSelect);
110
- const [mapReady, setMapReady] = useState(false);
111
- const [mapError, setMapError] = useState<string | null>(null);
112
-
113
- const safeCenter = useMemo(() => {
114
- if (isValidLatLon(center.lat, center.lon)) return center;
115
- return REUNION_CENTER;
116
- }, [center]);
117
-
118
- const validLocations = useMemo(
119
- () => locations.filter((loc) => isValidLatLon(loc.lat, loc.lon)),
120
- [locations],
121
- );
122
-
123
- useEffect(() => {
124
- onSelectRef.current = onSelect;
125
- }, [onSelect]);
126
-
127
- useEffect(() => {
128
- let cancelled = false;
129
- let resizeObserver: ResizeObserver | null = null;
130
-
131
- async function init() {
132
- if (!containerRef.current || mapRef.current) return;
133
- try {
134
- const maplibregl = await loadMapLibre();
135
- if (cancelled || !containerRef.current) return;
136
- maplibreRef.current = maplibregl;
137
-
138
- const map = new maplibregl.Map({
139
- container: containerRef.current,
140
- style: OPENFREEMAP_STYLE,
141
- center: [safeCenter.lon, safeCenter.lat],
142
- zoom: 10,
143
- });
144
- mapRef.current = map;
145
-
146
- let usedRasterFallback = false;
147
- const doResize = () => {
148
- try {
149
- map.resize();
150
- } catch {
151
- /* ignore */
152
- }
153
- };
154
-
155
- map.on("load", () => {
156
- doResize();
157
- requestAnimationFrame(doResize);
158
- if (!cancelled) {
159
- setMapError(null);
160
- setMapReady(true);
161
- }
162
- });
163
-
164
- map.on("error", (e: { error?: { message?: string } }) => {
165
- const msg =
166
- typeof e?.error?.message === "string"
167
- ? e.error.message
168
- : "Erreur chargement carte";
169
- // OpenFreeMap / vector tiles often blocked in iframe → OSM raster
170
- if (!usedRasterFallback && !cancelled) {
171
- usedRasterFallback = true;
172
- try {
173
- map.setStyle(OSM_RASTER_STYLE as never);
174
- return;
175
- } catch {
176
- /* fall through */
177
- }
178
- }
179
- if (!cancelled) setMapError(msg);
180
- });
181
-
182
- if (typeof ResizeObserver !== "undefined" && containerRef.current) {
183
- resizeObserver = new ResizeObserver(() => doResize());
184
- resizeObserver.observe(containerRef.current);
185
- }
186
-
187
- requestAnimationFrame(() => {
188
- doResize();
189
- setTimeout(doResize, 50);
190
- setTimeout(doResize, 200);
191
- });
192
- } catch (err) {
193
- if (!cancelled) {
194
- setMapError(
195
- err instanceof Error
196
- ? err.message
197
- : "Impossible de charger MapLibre",
198
- );
199
- }
200
- }
201
- }
202
-
203
- void init();
204
-
205
- return () => {
206
- cancelled = true;
207
- resizeObserver?.disconnect();
208
- for (const m of markersRef.current) m.marker.remove();
209
- markersRef.current = [];
210
- mapRef.current?.remove();
211
- mapRef.current = null;
212
- maplibreRef.current = null;
213
- setMapReady(false);
214
- };
215
- // Init once; center used only as initial camera
216
- // eslint-disable-next-line react-hooks/exhaustive-deps
217
- }, []);
218
-
219
- useEffect(() => {
220
- if (!mapReady) return;
221
- const map = mapRef.current;
222
- const maplibregl = maplibreRef.current;
223
- if (!map || !maplibregl) return;
224
-
225
- for (const m of markersRef.current) m.marker.remove();
226
- markersRef.current = [];
227
-
228
- const visible = validLocations.slice(0, MAX_MARKERS);
229
- for (const loc of visible) {
230
- const el = buildMarkerElement(
231
- loc.category as LocationCategory,
232
- selectedId === loc.id,
233
- );
234
- el.addEventListener("click", (e) => {
235
- e.stopPropagation();
236
- onSelectRef.current(loc);
237
- });
238
- const marker = new maplibregl.Marker({ element: el, anchor: "center" })
239
- .setLngLat([loc.lon, loc.lat])
240
- .addTo(map);
241
- markersRef.current.push({ id: loc.id, marker });
242
- }
243
- }, [validLocations, selectedId, mapReady]);
244
-
245
- const locationsKey = useMemo(
246
- () => validLocations.map((l) => l.id).join("|"),
247
- [validLocations],
248
- );
249
-
250
- useEffect(() => {
251
- if (!mapReady) return;
252
- const map = mapRef.current;
253
- const maplibregl = maplibreRef.current;
254
- if (!map || !maplibregl) return;
255
-
256
- const visible = validLocations.slice(0, MAX_MARKERS);
257
- if (visible.length === 0) return;
258
-
259
- if (visible.length === 1) {
260
- map.easeTo({
261
- center: [visible[0]!.lon, visible[0]!.lat],
262
- zoom: 13,
263
- duration: 350,
264
- });
265
- return;
266
- }
267
-
268
- const bounds = new maplibregl.LngLatBounds(
269
- [visible[0]!.lon, visible[0]!.lat],
270
- [visible[0]!.lon, visible[0]!.lat],
271
- );
272
- for (const loc of visible) bounds.extend([loc.lon, loc.lat]);
273
- map.fitBounds(bounds, { padding: 56, maxZoom: 13, duration: 350 });
274
- // eslint-disable-next-line react-hooks/exhaustive-deps -- locationsKey captures set
275
- }, [locationsKey, mapReady]);
276
-
277
- useEffect(() => {
278
- if (!mapReady) return;
279
- const map = mapRef.current;
280
- if (!map || !selectedId) return;
281
- const loc = validLocations.find((l) => l.id === selectedId);
282
- if (!loc) return;
283
- map.easeTo({
284
- center: [loc.lon, loc.lat],
285
- zoom: Math.max(map.getZoom(), 13),
286
- duration: 350,
287
- });
288
- }, [selectedId, validLocations, mapReady]);
289
-
290
- return (
291
- <View style={styles.wrap}>
292
- {createElement("div", {
293
- ref: containerRef,
294
- className: "wasto-locations-map",
295
- style: {
296
- position: "absolute",
297
- inset: 0,
298
- width: "100%",
299
- height: "100%",
300
- minHeight: 220,
301
- },
302
- })}
303
- {mapError ? (
304
- <View style={styles.errorOverlay} pointerEvents="none">
305
- <Text style={styles.errorText}>Carte indisponible : {mapError}</Text>
306
- </View>
307
- ) : null}
308
- </View>
309
- );
310
- }
311
-
312
- const styles = StyleSheet.create({
313
- wrap: {
314
- flex: 1,
315
- minHeight: 220,
316
- position: "relative",
317
- overflow: "hidden",
318
- },
319
- errorOverlay: {
320
- ...StyleSheet.absoluteFillObject,
321
- alignItems: "center",
322
- justifyContent: "center",
323
- backgroundColor: "rgba(245,245,245,0.92)",
324
- paddingHorizontal: 16,
325
- },
326
- errorText: {
327
- fontSize: 12,
328
- textAlign: "center",
329
- opacity: 0.7,
330
- },
331
- });
1
+ import {
2
+ createElement,
3
+ useEffect,
4
+ useMemo,
5
+ useRef,
6
+ useState,
7
+ } from "react";
8
+ import { StyleSheet, Text, View } from "react-native";
9
+ import type { Map as MapLibreMap, Marker } from "maplibre-gl";
10
+ import {
11
+ LOCATION_CATEGORY_META,
12
+ REUNION_CENTER,
13
+ isValidLatLon,
14
+ type LocationCategory,
15
+ } from "../../hooks/useLocations";
16
+ import { getContrastColor } from "../../helpers/getContrastColor";
17
+ import type { LocationsMapViewProps } from "./LocationsMapView";
18
+
19
+ import "maplibre-gl/dist/maplibre-gl.css";
20
+
21
+ /** Vector style (parity with native). Raster fallback if OpenFreeMap fails (CSP/iframe). */
22
+ const OPENFREEMAP_STYLE = "https://tiles.openfreemap.org/styles/liberty";
23
+ const OSM_RASTER_STYLE = {
24
+ version: 8 as const,
25
+ sources: {
26
+ osm: {
27
+ type: "raster" as const,
28
+ tiles: ["https://tile.openstreetmap.org/{z}/{x}/{y}.png"],
29
+ tileSize: 256,
30
+ attribution: "© OpenStreetMap contributors",
31
+ },
32
+ },
33
+ layers: [{ id: "osm", type: "raster" as const, source: "osm" }],
34
+ };
35
+ const MAX_MARKERS = 80;
36
+
37
+ /** maplibre-gl@4 dynamic import may be `{ default: ns }` under Metro/webpack. */
38
+ type MapLibreGL = {
39
+ Map: new (options: {
40
+ container: HTMLElement;
41
+ style: string | typeof OSM_RASTER_STYLE;
42
+ center: [number, number];
43
+ zoom: number;
44
+ }) => MapLibreMap;
45
+ Marker: new (options?: { element?: HTMLElement; anchor?: string }) => Marker;
46
+ LngLatBounds: new (
47
+ sw: [number, number],
48
+ ne: [number, number],
49
+ ) => { extend: (coord: [number, number]) => unknown };
50
+ };
51
+
52
+ async function loadMapLibre(): Promise<MapLibreGL> {
53
+ const mod = await import("maplibre-gl");
54
+ return ((mod as { default?: MapLibreGL }).default ?? mod) as MapLibreGL;
55
+ }
56
+
57
+ function buildMarkerElement(category: LocationCategory, selected: boolean) {
58
+ const color =
59
+ LOCATION_CATEGORY_META[category]?.color ??
60
+ LOCATION_CATEGORY_META.recycling.color;
61
+ const iconColor = getContrastColor(color);
62
+ const el = document.createElement("button");
63
+ el.type = "button";
64
+ el.setAttribute("aria-label", LOCATION_CATEGORY_META[category]?.label ?? "Lieu");
65
+ el.style.cssText = [
66
+ "display:flex",
67
+ "align-items:center",
68
+ "justify-content:center",
69
+ "width:32px",
70
+ "height:32px",
71
+ "padding:0",
72
+ "border:none",
73
+ "background:transparent",
74
+ "cursor:pointer",
75
+ ].join(";");
76
+ el.innerHTML = `
77
+ <span style="
78
+ display:flex;align-items:center;justify-content:center;
79
+ width:32px;height:32px;border-radius:9999px;
80
+ background:${color};
81
+ border:3px solid ${selected ? "#111827" : "#ffffff"};
82
+ box-shadow:0 1px 3px rgba(0,0,0,0.25);
83
+ transform:scale(${selected ? 1.12 : 1});
84
+ ">
85
+ <svg width="16" height="16" viewBox="0 0 24 24" fill="none"
86
+ stroke="${iconColor}" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
87
+ <path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z"/>
88
+ <circle cx="12" cy="10" r="3"/>
89
+ </svg>
90
+ </span>
91
+ `;
92
+ return el;
93
+ }
94
+
95
+ /**
96
+ * Web MapLibre GL JS canvas (Metro resolves `.web.tsx` over the pin fallback).
97
+ * Same OpenFreeMap style as native + Studio PWA preview.
98
+ */
99
+ export function LocationsMapView({
100
+ locations,
101
+ center,
102
+ selectedId,
103
+ onSelect,
104
+ }: LocationsMapViewProps) {
105
+ const containerRef = useRef<HTMLDivElement | null>(null);
106
+ const mapRef = useRef<MapLibreMap | null>(null);
107
+ const maplibreRef = useRef<MapLibreGL | null>(null);
108
+ const markersRef = useRef<Array<{ id: string; marker: Marker }>>([]);
109
+ const onSelectRef = useRef(onSelect);
110
+ const [mapReady, setMapReady] = useState(false);
111
+ const [mapError, setMapError] = useState<string | null>(null);
112
+
113
+ const safeCenter = useMemo(() => {
114
+ if (isValidLatLon(center.lat, center.lon)) return center;
115
+ return REUNION_CENTER;
116
+ }, [center]);
117
+
118
+ const validLocations = useMemo(
119
+ () => locations.filter((loc) => isValidLatLon(loc.lat, loc.lon)),
120
+ [locations],
121
+ );
122
+
123
+ useEffect(() => {
124
+ onSelectRef.current = onSelect;
125
+ }, [onSelect]);
126
+
127
+ useEffect(() => {
128
+ let cancelled = false;
129
+ let resizeObserver: ResizeObserver | null = null;
130
+
131
+ async function init() {
132
+ if (!containerRef.current || mapRef.current) return;
133
+ try {
134
+ const maplibregl = await loadMapLibre();
135
+ if (cancelled || !containerRef.current) return;
136
+ maplibreRef.current = maplibregl;
137
+
138
+ const map = new maplibregl.Map({
139
+ container: containerRef.current,
140
+ style: OPENFREEMAP_STYLE,
141
+ center: [safeCenter.lon, safeCenter.lat],
142
+ zoom: 10,
143
+ });
144
+ mapRef.current = map;
145
+
146
+ let usedRasterFallback = false;
147
+ const doResize = () => {
148
+ try {
149
+ map.resize();
150
+ } catch {
151
+ /* ignore */
152
+ }
153
+ };
154
+
155
+ map.on("load", () => {
156
+ doResize();
157
+ requestAnimationFrame(doResize);
158
+ if (!cancelled) {
159
+ setMapError(null);
160
+ setMapReady(true);
161
+ }
162
+ });
163
+
164
+ map.on("error", (e: { error?: { message?: string } }) => {
165
+ const msg =
166
+ typeof e?.error?.message === "string"
167
+ ? e.error.message
168
+ : "Erreur chargement carte";
169
+ // OpenFreeMap / vector tiles often blocked in iframe → OSM raster
170
+ if (!usedRasterFallback && !cancelled) {
171
+ usedRasterFallback = true;
172
+ try {
173
+ map.setStyle(OSM_RASTER_STYLE as never);
174
+ return;
175
+ } catch {
176
+ /* fall through */
177
+ }
178
+ }
179
+ if (!cancelled) setMapError(msg);
180
+ });
181
+
182
+ if (typeof ResizeObserver !== "undefined" && containerRef.current) {
183
+ resizeObserver = new ResizeObserver(() => doResize());
184
+ resizeObserver.observe(containerRef.current);
185
+ }
186
+
187
+ requestAnimationFrame(() => {
188
+ doResize();
189
+ setTimeout(doResize, 50);
190
+ setTimeout(doResize, 200);
191
+ });
192
+ } catch (err) {
193
+ if (!cancelled) {
194
+ setMapError(
195
+ err instanceof Error
196
+ ? err.message
197
+ : "Impossible de charger MapLibre",
198
+ );
199
+ }
200
+ }
201
+ }
202
+
203
+ void init();
204
+
205
+ return () => {
206
+ cancelled = true;
207
+ resizeObserver?.disconnect();
208
+ for (const m of markersRef.current) m.marker.remove();
209
+ markersRef.current = [];
210
+ mapRef.current?.remove();
211
+ mapRef.current = null;
212
+ maplibreRef.current = null;
213
+ setMapReady(false);
214
+ };
215
+ // Init once; center used only as initial camera
216
+ // eslint-disable-next-line react-hooks/exhaustive-deps
217
+ }, []);
218
+
219
+ useEffect(() => {
220
+ if (!mapReady) return;
221
+ const map = mapRef.current;
222
+ const maplibregl = maplibreRef.current;
223
+ if (!map || !maplibregl) return;
224
+
225
+ for (const m of markersRef.current) m.marker.remove();
226
+ markersRef.current = [];
227
+
228
+ const visible = validLocations.slice(0, MAX_MARKERS);
229
+ for (const loc of visible) {
230
+ const el = buildMarkerElement(
231
+ loc.category as LocationCategory,
232
+ selectedId === loc.id,
233
+ );
234
+ el.addEventListener("click", (e) => {
235
+ e.stopPropagation();
236
+ onSelectRef.current(loc);
237
+ });
238
+ const marker = new maplibregl.Marker({ element: el, anchor: "center" })
239
+ .setLngLat([loc.lon, loc.lat])
240
+ .addTo(map);
241
+ markersRef.current.push({ id: loc.id, marker });
242
+ }
243
+ }, [validLocations, selectedId, mapReady]);
244
+
245
+ const locationsKey = useMemo(
246
+ () => validLocations.map((l) => l.id).join("|"),
247
+ [validLocations],
248
+ );
249
+
250
+ useEffect(() => {
251
+ if (!mapReady) return;
252
+ const map = mapRef.current;
253
+ const maplibregl = maplibreRef.current;
254
+ if (!map || !maplibregl) return;
255
+
256
+ const visible = validLocations.slice(0, MAX_MARKERS);
257
+ if (visible.length === 0) return;
258
+
259
+ if (visible.length === 1) {
260
+ map.easeTo({
261
+ center: [visible[0]!.lon, visible[0]!.lat],
262
+ zoom: 13,
263
+ duration: 350,
264
+ });
265
+ return;
266
+ }
267
+
268
+ const bounds = new maplibregl.LngLatBounds(
269
+ [visible[0]!.lon, visible[0]!.lat],
270
+ [visible[0]!.lon, visible[0]!.lat],
271
+ );
272
+ for (const loc of visible) bounds.extend([loc.lon, loc.lat]);
273
+ map.fitBounds(bounds, { padding: 56, maxZoom: 13, duration: 350 });
274
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- locationsKey captures set
275
+ }, [locationsKey, mapReady]);
276
+
277
+ useEffect(() => {
278
+ if (!mapReady) return;
279
+ const map = mapRef.current;
280
+ if (!map || !selectedId) return;
281
+ const loc = validLocations.find((l) => l.id === selectedId);
282
+ if (!loc) return;
283
+ map.easeTo({
284
+ center: [loc.lon, loc.lat],
285
+ zoom: Math.max(map.getZoom(), 13),
286
+ duration: 350,
287
+ });
288
+ }, [selectedId, validLocations, mapReady]);
289
+
290
+ return (
291
+ <View style={styles.wrap}>
292
+ {createElement("div", {
293
+ ref: containerRef,
294
+ className: "wasto-locations-map",
295
+ style: {
296
+ position: "absolute",
297
+ inset: 0,
298
+ width: "100%",
299
+ height: "100%",
300
+ minHeight: 220,
301
+ },
302
+ })}
303
+ {mapError ? (
304
+ <View style={styles.errorOverlay} pointerEvents="none">
305
+ <Text style={styles.errorText}>Carte indisponible : {mapError}</Text>
306
+ </View>
307
+ ) : null}
308
+ </View>
309
+ );
310
+ }
311
+
312
+ const styles = StyleSheet.create({
313
+ wrap: {
314
+ flex: 1,
315
+ minHeight: 220,
316
+ position: "relative",
317
+ overflow: "hidden",
318
+ },
319
+ errorOverlay: {
320
+ ...StyleSheet.absoluteFillObject,
321
+ alignItems: "center",
322
+ justifyContent: "center",
323
+ backgroundColor: "rgba(245,245,245,0.92)",
324
+ paddingHorizontal: 16,
325
+ },
326
+ errorText: {
327
+ fontSize: 12,
328
+ textAlign: "center",
329
+ opacity: 0.7,
330
+ },
331
+ });