@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.
- package/README.md +47 -47
- package/dist/index.d.mts +103 -2
- package/dist/index.d.ts +103 -2
- package/dist/index.js +2861 -2442
- package/dist/index.mjs +3651 -3227
- package/package.json +2 -2
- package/src/components/announcement-modal/AnnouncementModal.tsx +183 -183
- package/src/components/article/Article.tsx +21 -21
- package/src/components/article/ArticleCard.tsx +81 -81
- package/src/components/article/ArticleExcerpts.tsx +80 -80
- package/src/components/article/ArticleList.tsx +114 -114
- package/src/components/article/ArticleMarkdown.tsx +170 -170
- package/src/components/article/ArticleReaderModal.tsx +93 -93
- package/src/components/article/ArticleSingle.tsx +57 -57
- package/src/components/bottom-tab/BottomTab.tsx +392 -392
- package/src/components/button/Button.tsx +154 -154
- package/src/components/challenge/Badges.tsx +84 -84
- package/src/components/challenge/BadgesModal.tsx +166 -166
- package/src/components/challenge/Challenge.tsx +380 -380
- package/src/components/challenge/ChallengeModal.tsx +231 -231
- package/src/components/challenge/ChallengeVignette.tsx +249 -249
- package/src/components/challenge/ChallengesModal.tsx +243 -243
- package/src/components/challenge/QuizModal.tsx +381 -381
- package/src/components/challenge/ResultModal.tsx +144 -144
- package/src/components/chat/Chat.tsx +928 -928
- package/src/components/collection-calendar/CollectionCalendar.tsx +274 -274
- package/src/components/columns/Columns.tsx +33 -33
- package/src/components/consignes-modal/ConsignesModal.tsx +267 -267
- package/src/components/content-inset/ContentInset.tsx +33 -33
- package/src/components/header/Header.tsx +391 -391
- package/src/components/image/Image.tsx +15 -15
- package/src/components/image/ImageFrame.tsx +102 -102
- package/src/components/image/ImageMedia.tsx +31 -31
- package/src/components/image/ImageUrl.tsx +30 -30
- package/src/components/locations-map/LocationSheet.tsx +417 -417
- package/src/components/locations-map/LocationsMap.native.tsx +260 -260
- package/src/components/locations-map/LocationsMap.tsx +13 -13
- package/src/components/locations-map/LocationsMap.web.tsx +10 -10
- package/src/components/locations-map/LocationsMapShell.tsx +527 -527
- package/src/components/locations-map/LocationsMapView.tsx +108 -108
- package/src/components/locations-map/LocationsMapView.web.tsx +331 -331
- package/src/components/nav-button/NavButton.tsx +59 -59
- package/src/components/next-collection/NextCollection.tsx +636 -621
- package/src/components/notifications/NotificationsScreen.tsx +247 -247
- package/src/components/page/Page.tsx +102 -64
- package/src/components/parapraph/Paragraph.tsx +67 -67
- package/src/components/profile/ProfileScreen.tsx +236 -236
- package/src/components/recycling-info/RecyclingInfo.tsx +189 -189
- package/src/components/safe-area/OptionalSafeAreaProvider.tsx +17 -17
- package/src/components/search/Search.tsx +320 -320
- package/src/components/select-street-modal/SelectStreetModal.tsx +475 -475
- package/src/components/title/Title.tsx +100 -100
- package/src/components/vision/Vision.tsx +447 -447
- package/src/components/vision/VisionActionCard.tsx +117 -117
- package/src/components/vision/VisionCapture.tsx +30 -30
- package/src/components/vision/VisionTapOverlay.tsx +122 -122
- package/src/contexts/AnnouncementsContext.tsx +171 -171
- package/src/contexts/AppDataContext.tsx +458 -0
- package/src/contexts/ChallengesContext.tsx +182 -182
- package/src/contexts/LocalContext.tsx +227 -227
- package/src/contexts/ThemeContext.tsx +37 -37
- package/src/contexts/WastoContext.tsx +130 -127
- package/src/helpers/blogPost.ts +72 -72
- package/src/helpers/generateQuizQuestions.ts +247 -247
- package/src/helpers/getContrastColor.ts +18 -18
- package/src/helpers/haptics.ts +20 -20
- package/src/helpers/hoursSchedule.ts +230 -230
- package/src/helpers/htmlContentToMarkdown.ts +114 -114
- package/src/helpers/layout.ts +54 -54
- package/src/helpers/loadLinearGradient.ts +22 -22
- package/src/helpers/mapLocations.ts +170 -0
- package/src/helpers/maquetteTheme.ts +76 -76
- package/src/helpers/markdownUnderlinePlugin.ts +32 -32
- package/src/helpers/nextCollection.ts +122 -0
- package/src/helpers/normalizeWasteTypeRecyclingInfo.ts +80 -80
- package/src/helpers/publicMediaUrl.ts +8 -8
- package/src/helpers/resolveApiAssetUrl.ts +21 -21
- package/src/helpers/systemRoutes.ts +7 -7
- package/src/helpers/themeStyles.ts +114 -114
- package/src/helpers/ttlCache.ts +41 -0
- package/src/helpers/useSafeAreaPadding.ts +14 -14
- package/src/hooks/useBadges.ts +100 -100
- package/src/hooks/useBlogPosts.ts +109 -105
- package/src/hooks/useChallenges.ts +110 -110
- package/src/hooks/useCities.ts +56 -56
- package/src/hooks/useCityAnnouncements.ts +118 -118
- package/src/hooks/useCollectionCalendar.ts +421 -421
- package/src/hooks/useColumns.ts +19 -19
- package/src/hooks/useConsignes.ts +62 -62
- package/src/hooks/useLocations.ts +120 -248
- package/src/hooks/useNextCollection.ts +163 -238
- package/src/hooks/useRecyclingInfo.ts +90 -90
- package/src/hooks/useWastoClient.ts +16 -16
- package/src/index.ts +149 -142
- package/src/navigation/NavigationContext.tsx +56 -56
- package/src/sentry/initSentry.ts +120 -120
- package/src/types/Theme.ts +72 -72
- package/src/types/css-modules.d.ts +1 -1
package/src/hooks/useColumns.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
|
|
3
|
-
export function useColumns<T = any>(
|
|
4
|
-
data: T[],
|
|
5
|
-
itemsPerRow: number = 4,
|
|
6
|
-
): (T | null)[][] {
|
|
7
|
-
return useMemo(() => {
|
|
8
|
-
const rows: (T | null)[][] = [];
|
|
9
|
-
|
|
10
|
-
for (let i = 0; i < data.length; i += itemsPerRow) {
|
|
11
|
-
const row: (T | null)[] = data.slice(i, i + itemsPerRow);
|
|
12
|
-
while (row.length < itemsPerRow) {
|
|
13
|
-
row.push(null);
|
|
14
|
-
}
|
|
15
|
-
rows.push(row);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return rows;
|
|
19
|
-
}, [data]);
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
|
|
3
|
+
export function useColumns<T = any>(
|
|
4
|
+
data: T[],
|
|
5
|
+
itemsPerRow: number = 4,
|
|
6
|
+
): (T | null)[][] {
|
|
7
|
+
return useMemo(() => {
|
|
8
|
+
const rows: (T | null)[][] = [];
|
|
9
|
+
|
|
10
|
+
for (let i = 0; i < data.length; i += itemsPerRow) {
|
|
11
|
+
const row: (T | null)[] = data.slice(i, i + itemsPerRow);
|
|
12
|
+
while (row.length < itemsPerRow) {
|
|
13
|
+
row.push(null);
|
|
14
|
+
}
|
|
15
|
+
rows.push(row);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return rows;
|
|
19
|
+
}, [data]);
|
|
20
20
|
}
|
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import { useMemo } from "react";
|
|
2
|
-
import { normalizeWasteTypeToRecyclingInfo } from "../helpers/normalizeWasteTypeRecyclingInfo";
|
|
3
|
-
import { useNextCollection } from "./useNextCollection";
|
|
4
|
-
|
|
5
|
-
export type WasteTypeForConsignes = {
|
|
6
|
-
name: string;
|
|
7
|
-
imageUrl?: string;
|
|
8
|
-
rules?: string[];
|
|
9
|
-
color?: string;
|
|
10
|
-
details?: Array<{
|
|
11
|
-
id: string | number;
|
|
12
|
-
material?: string;
|
|
13
|
-
description?: string;
|
|
14
|
-
imageUrl?: string;
|
|
15
|
-
}>;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type CityForConsignes = {
|
|
19
|
-
general_collection_rules?: Record<string, string>;
|
|
20
|
-
[key: string]: unknown;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type UseConsignesResult = {
|
|
24
|
-
currentCity: CityForConsignes | null;
|
|
25
|
-
wasteType: WasteTypeForConsignes | null;
|
|
26
|
-
isLoading: boolean;
|
|
27
|
-
error: string | null;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Prépare les données pour la modale de consignes à partir de:
|
|
32
|
-
* - la ville courante (useNextCollection.city)
|
|
33
|
-
* - la liste des wasteTypes (useNextCollection.wasteTypes)
|
|
34
|
-
*/
|
|
35
|
-
export function useConsignes(wasteTypeId: string | null | undefined): UseConsignesResult {
|
|
36
|
-
const { city, wasteTypes, isLoading, error } = useNextCollection();
|
|
37
|
-
|
|
38
|
-
const wasteType = useMemo<WasteTypeForConsignes | null>(() => {
|
|
39
|
-
if (!wasteTypeId) return null;
|
|
40
|
-
|
|
41
|
-
const wt = (wasteTypes ?? []).find((w) => w.id === wasteTypeId);
|
|
42
|
-
if (!wt) return null;
|
|
43
|
-
|
|
44
|
-
const info = normalizeWasteTypeToRecyclingInfo(wt);
|
|
45
|
-
if (!info) return null;
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
name: info.name,
|
|
49
|
-
imageUrl: info.imageUrl,
|
|
50
|
-
rules: info.rules,
|
|
51
|
-
details: info.details,
|
|
52
|
-
color: info.color
|
|
53
|
-
};
|
|
54
|
-
}, [wasteTypeId, wasteTypes]);
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
currentCity: (city as any) ?? null,
|
|
58
|
-
wasteType,
|
|
59
|
-
isLoading,
|
|
60
|
-
error,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { normalizeWasteTypeToRecyclingInfo } from "../helpers/normalizeWasteTypeRecyclingInfo";
|
|
3
|
+
import { useNextCollection } from "./useNextCollection";
|
|
4
|
+
|
|
5
|
+
export type WasteTypeForConsignes = {
|
|
6
|
+
name: string;
|
|
7
|
+
imageUrl?: string;
|
|
8
|
+
rules?: string[];
|
|
9
|
+
color?: string;
|
|
10
|
+
details?: Array<{
|
|
11
|
+
id: string | number;
|
|
12
|
+
material?: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
imageUrl?: string;
|
|
15
|
+
}>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type CityForConsignes = {
|
|
19
|
+
general_collection_rules?: Record<string, string>;
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type UseConsignesResult = {
|
|
24
|
+
currentCity: CityForConsignes | null;
|
|
25
|
+
wasteType: WasteTypeForConsignes | null;
|
|
26
|
+
isLoading: boolean;
|
|
27
|
+
error: string | null;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Prépare les données pour la modale de consignes à partir de:
|
|
32
|
+
* - la ville courante (useNextCollection.city)
|
|
33
|
+
* - la liste des wasteTypes (useNextCollection.wasteTypes)
|
|
34
|
+
*/
|
|
35
|
+
export function useConsignes(wasteTypeId: string | null | undefined): UseConsignesResult {
|
|
36
|
+
const { city, wasteTypes, isLoading, error } = useNextCollection();
|
|
37
|
+
|
|
38
|
+
const wasteType = useMemo<WasteTypeForConsignes | null>(() => {
|
|
39
|
+
if (!wasteTypeId) return null;
|
|
40
|
+
|
|
41
|
+
const wt = (wasteTypes ?? []).find((w) => w.id === wasteTypeId);
|
|
42
|
+
if (!wt) return null;
|
|
43
|
+
|
|
44
|
+
const info = normalizeWasteTypeToRecyclingInfo(wt);
|
|
45
|
+
if (!info) return null;
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
name: info.name,
|
|
49
|
+
imageUrl: info.imageUrl,
|
|
50
|
+
rules: info.rules,
|
|
51
|
+
details: info.details,
|
|
52
|
+
color: info.color
|
|
53
|
+
};
|
|
54
|
+
}, [wasteTypeId, wasteTypes]);
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
currentCity: (city as any) ?? null,
|
|
58
|
+
wasteType,
|
|
59
|
+
isLoading,
|
|
60
|
+
error,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
@@ -1,248 +1,120 @@
|
|
|
1
|
-
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
-
import { MAX_PAGE_SIZE } from "@wasto/sdk";
|
|
3
|
-
import { useLocal } from "../contexts/LocalContext";
|
|
4
|
-
import {
|
|
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
|
-
const
|
|
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
|
-
const
|
|
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
|
-
for (let page = 2; page <= totalPages; page++) {
|
|
123
|
-
const res = await fetchPage({ page, pageSize: PAGE_SIZE });
|
|
124
|
-
items.push(...asArray(res));
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return items;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export type UseLocationsResult = {
|
|
131
|
-
locations: MapLocation[];
|
|
132
|
-
isLoading: boolean;
|
|
133
|
-
error: string | null;
|
|
134
|
-
center: { lat: number; lon: number };
|
|
135
|
-
refetch: () => Promise<void>;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
export function useLocations(): UseLocationsResult {
|
|
139
|
-
const { selectedCityId } = useLocal();
|
|
140
|
-
const client = useWastoClient();
|
|
141
|
-
|
|
142
|
-
const [locations, setLocations] = useState<MapLocation[]>([]);
|
|
143
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
144
|
-
const [error, setError] = useState<string | null>(null);
|
|
145
|
-
|
|
146
|
-
const fetchLocations = useCallback(async () => {
|
|
147
|
-
if (!selectedCityId) {
|
|
148
|
-
setLocations([]);
|
|
149
|
-
setIsLoading(false);
|
|
150
|
-
setError(null);
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
try {
|
|
155
|
-
setIsLoading(true);
|
|
156
|
-
setError(null);
|
|
157
|
-
const [points, places] = await Promise.all([
|
|
158
|
-
fetchAllPages((params) =>
|
|
159
|
-
client.public.cities.getRecyclingPoints(selectedCityId, params),
|
|
160
|
-
),
|
|
161
|
-
fetchAllPages((params) =>
|
|
162
|
-
client.public.cities.getWasteCollectionPlaces(selectedCityId, params),
|
|
163
|
-
),
|
|
164
|
-
]);
|
|
165
|
-
|
|
166
|
-
const out: MapLocation[] = [];
|
|
167
|
-
|
|
168
|
-
for (const raw of points) {
|
|
169
|
-
const p = raw as Record<string, unknown>;
|
|
170
|
-
const coords = parseRecyclingPointCoords(p);
|
|
171
|
-
if (!coords) continue;
|
|
172
|
-
const id = String(p.id ?? "");
|
|
173
|
-
if (!id) continue;
|
|
174
|
-
const hoursSource = {
|
|
175
|
-
hours: typeof p.hours === "string" ? p.hours : null,
|
|
176
|
-
details:
|
|
177
|
-
p.details != null && typeof p.details === "object"
|
|
178
|
-
? (p.details as Record<string, unknown>)
|
|
179
|
-
: null,
|
|
180
|
-
};
|
|
181
|
-
const schedule = buildHoursSchedule(hoursSource);
|
|
182
|
-
out.push({
|
|
183
|
-
id: `rp-${id}`,
|
|
184
|
-
name: String(p.name ?? "Point de collecte"),
|
|
185
|
-
category: categorizeRecyclingType(typeof p.type === "string" ? p.type : undefined),
|
|
186
|
-
address: typeof p.address === "string" ? p.address : undefined,
|
|
187
|
-
phone: typeof p.phone === "string" ? p.phone : undefined,
|
|
188
|
-
hoursDisplay: schedule.summary ?? undefined,
|
|
189
|
-
hoursRows: schedule.rows.length ? schedule.rows : undefined,
|
|
190
|
-
hoursStatus: schedule.status,
|
|
191
|
-
lat: coords.lat,
|
|
192
|
-
lon: coords.lon,
|
|
193
|
-
badges: schedule.status ? [schedule.status.text] : schedule.summary ? [schedule.summary] : undefined,
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
for (const raw of places) {
|
|
198
|
-
const place = raw as Record<string, unknown>;
|
|
199
|
-
const locs = Array.isArray(place.attachedLocations) ? place.attachedLocations : [];
|
|
200
|
-
const loc = locs.find((item) => {
|
|
201
|
-
const l = item as Record<string, unknown>;
|
|
202
|
-
return l.latitude != null && l.longitude != null;
|
|
203
|
-
}) as Record<string, unknown> | undefined;
|
|
204
|
-
if (!loc) continue;
|
|
205
|
-
const coords = parseLngLat(loc.longitude, loc.latitude);
|
|
206
|
-
if (!coords) continue;
|
|
207
|
-
const id = String(place.id ?? loc.id ?? "");
|
|
208
|
-
if (!id) continue;
|
|
209
|
-
out.push({
|
|
210
|
-
id: `cp-${id}`,
|
|
211
|
-
name: String(place.name ?? loc.name ?? "Lieu de ramassage"),
|
|
212
|
-
category: "collection_place",
|
|
213
|
-
address: typeof loc.reference === "string" ? loc.reference : undefined,
|
|
214
|
-
lat: coords.lat,
|
|
215
|
-
lon: coords.lon,
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
setLocations(out);
|
|
220
|
-
} catch (err) {
|
|
221
|
-
setError(err instanceof Error ? err.message : "Impossible de charger les lieux");
|
|
222
|
-
setLocations([]);
|
|
223
|
-
} finally {
|
|
224
|
-
setIsLoading(false);
|
|
225
|
-
}
|
|
226
|
-
}, [client.public.cities, selectedCityId]);
|
|
227
|
-
|
|
228
|
-
useEffect(() => {
|
|
229
|
-
void fetchLocations();
|
|
230
|
-
}, [fetchLocations]);
|
|
231
|
-
|
|
232
|
-
const center = useMemo(() => {
|
|
233
|
-
const valid = locations.filter((l) => isValidLatLon(l.lat, l.lon));
|
|
234
|
-
if (valid.length === 0) return REUNION_CENTER;
|
|
235
|
-
const lat = valid.reduce((sum, l) => sum + l.lat, 0) / valid.length;
|
|
236
|
-
const lon = valid.reduce((sum, l) => sum + l.lon, 0) / valid.length;
|
|
237
|
-
if (!isValidLatLon(lat, lon)) return REUNION_CENTER;
|
|
238
|
-
return { lat, lon };
|
|
239
|
-
}, [locations]);
|
|
240
|
-
|
|
241
|
-
return {
|
|
242
|
-
locations,
|
|
243
|
-
isLoading,
|
|
244
|
-
error,
|
|
245
|
-
center,
|
|
246
|
-
refetch: fetchLocations,
|
|
247
|
-
};
|
|
248
|
-
}
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { MAX_PAGE_SIZE } from "@wasto/sdk";
|
|
3
|
+
import { useLocal } from "../contexts/LocalContext";
|
|
4
|
+
import { useOptionalAppData } from "../contexts/AppDataContext";
|
|
5
|
+
import {
|
|
6
|
+
computeMapCenter,
|
|
7
|
+
isValidLatLon,
|
|
8
|
+
LOCATION_CATEGORY_META,
|
|
9
|
+
normalizeMapLocations,
|
|
10
|
+
REUNION_CENTER,
|
|
11
|
+
type LocationCategory,
|
|
12
|
+
type MapLocation,
|
|
13
|
+
} from "../helpers/mapLocations";
|
|
14
|
+
import { useWastoClient } from "./useWastoClient";
|
|
15
|
+
|
|
16
|
+
export type { LocationCategory, MapLocation };
|
|
17
|
+
export { LOCATION_CATEGORY_META, REUNION_CENTER, isValidLatLon };
|
|
18
|
+
|
|
19
|
+
const PAGE_SIZE = MAX_PAGE_SIZE;
|
|
20
|
+
|
|
21
|
+
function asArray(res: unknown): unknown[] {
|
|
22
|
+
if (Array.isArray(res)) return res;
|
|
23
|
+
const data = (res as { data?: unknown })?.data;
|
|
24
|
+
return Array.isArray(data) ? data : [];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function totalPagesFrom(res: unknown): number {
|
|
28
|
+
const meta = (res as { meta?: { totalPages?: number } })?.meta;
|
|
29
|
+
const totalPages = meta?.totalPages;
|
|
30
|
+
return typeof totalPages === "number" && totalPages >= 1 ? totalPages : 1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type PaginatedFetcher = (params: { page: number; pageSize: number }) => Promise<unknown>;
|
|
34
|
+
|
|
35
|
+
async function fetchAllPages(fetchPage: PaginatedFetcher): Promise<unknown[]> {
|
|
36
|
+
const first = await fetchPage({ page: 1, pageSize: PAGE_SIZE });
|
|
37
|
+
const items = [...asArray(first)];
|
|
38
|
+
const totalPages = totalPagesFrom(first);
|
|
39
|
+
|
|
40
|
+
for (let page = 2; page <= totalPages; page++) {
|
|
41
|
+
const res = await fetchPage({ page, pageSize: PAGE_SIZE });
|
|
42
|
+
items.push(...asArray(res));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return items;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type UseLocationsResult = {
|
|
49
|
+
locations: MapLocation[];
|
|
50
|
+
isLoading: boolean;
|
|
51
|
+
error: string | null;
|
|
52
|
+
center: { lat: number; lon: number };
|
|
53
|
+
refetch: () => Promise<void>;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export function useLocations(): UseLocationsResult {
|
|
57
|
+
const { selectedCityId } = useLocal();
|
|
58
|
+
const appData = useOptionalAppData();
|
|
59
|
+
const client = useWastoClient();
|
|
60
|
+
|
|
61
|
+
const [locations, setLocations] = useState<MapLocation[]>([]);
|
|
62
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
63
|
+
const [error, setError] = useState<string | null>(null);
|
|
64
|
+
|
|
65
|
+
const useShared = Boolean(appData);
|
|
66
|
+
|
|
67
|
+
const fetchLocations = useCallback(async () => {
|
|
68
|
+
if (useShared && appData) {
|
|
69
|
+
await appData.refreshCachedData();
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!selectedCityId) {
|
|
74
|
+
setLocations([]);
|
|
75
|
+
setIsLoading(false);
|
|
76
|
+
setError(null);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
setIsLoading(true);
|
|
82
|
+
setError(null);
|
|
83
|
+
const [points, places] = await Promise.all([
|
|
84
|
+
fetchAllPages((params) =>
|
|
85
|
+
client.public.cities.getRecyclingPoints(selectedCityId, params),
|
|
86
|
+
),
|
|
87
|
+
fetchAllPages((params) =>
|
|
88
|
+
client.public.cities.getWasteCollectionPlaces(selectedCityId, params),
|
|
89
|
+
),
|
|
90
|
+
]);
|
|
91
|
+
|
|
92
|
+
setLocations(normalizeMapLocations(points, places));
|
|
93
|
+
} catch (err) {
|
|
94
|
+
setError(err instanceof Error ? err.message : "Impossible de charger les lieux");
|
|
95
|
+
setLocations([]);
|
|
96
|
+
} finally {
|
|
97
|
+
setIsLoading(false);
|
|
98
|
+
}
|
|
99
|
+
}, [appData, client.public.cities, selectedCityId, useShared]);
|
|
100
|
+
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (useShared) return;
|
|
103
|
+
void fetchLocations();
|
|
104
|
+
}, [fetchLocations, useShared]);
|
|
105
|
+
|
|
106
|
+
const sharedLocations = useShared && appData ? appData.locations : locations;
|
|
107
|
+
const center = useMemo(
|
|
108
|
+
() =>
|
|
109
|
+
useShared && appData ? appData.mapCenter : computeMapCenter(sharedLocations),
|
|
110
|
+
[appData, sharedLocations, useShared],
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
locations: sharedLocations,
|
|
115
|
+
isLoading: useShared && appData ? appData.isMapLoading : isLoading,
|
|
116
|
+
error: useShared && appData ? appData.mapError : error,
|
|
117
|
+
center,
|
|
118
|
+
refetch: fetchLocations,
|
|
119
|
+
};
|
|
120
|
+
}
|