@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
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
PropsWithChildren,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState,
|
|
10
|
+
type ReactNode,
|
|
11
|
+
} from "react";
|
|
12
|
+
import { ActivityIndicator, StyleSheet, View } from "react-native";
|
|
13
|
+
import { Image } from "expo-image";
|
|
14
|
+
import { createWastoClient, type CityAppPreload } from "@wasto/sdk";
|
|
15
|
+
import { useLocal } from "./LocalContext";
|
|
16
|
+
import { useTheme } from "./ThemeContext";
|
|
17
|
+
import { resolveApiAssetUrl } from "../helpers/resolveApiAssetUrl";
|
|
18
|
+
import {
|
|
19
|
+
computeMapCenter,
|
|
20
|
+
normalizeMapLocations,
|
|
21
|
+
type MapLocation,
|
|
22
|
+
} from "../helpers/mapLocations";
|
|
23
|
+
import {
|
|
24
|
+
APP_DATA_CACHE_TTL_MS,
|
|
25
|
+
readTtlCache,
|
|
26
|
+
writeTtlCache,
|
|
27
|
+
} from "../helpers/ttlCache";
|
|
28
|
+
import {
|
|
29
|
+
getCollectionsForDay,
|
|
30
|
+
type City,
|
|
31
|
+
type CollectionForDay,
|
|
32
|
+
type WasteCollectionPlace,
|
|
33
|
+
type WasteType,
|
|
34
|
+
} from "../helpers/nextCollection";
|
|
35
|
+
import { resolveTheme } from "../helpers/themeStyles";
|
|
36
|
+
|
|
37
|
+
/** Fenêtre préchargée pour le carrousel « prochaines collectes ». */
|
|
38
|
+
export const NEXT_EVENTS_DAY_RADIUS = 3;
|
|
39
|
+
|
|
40
|
+
const CACHE_KEY_MAP = (cityId: string) => `wasto_cache_map_v1_${cityId}`;
|
|
41
|
+
const CACHE_KEY_RECYCLING = (cityId: string) => `wasto_cache_recycling_v1_${cityId}`;
|
|
42
|
+
|
|
43
|
+
export type AppDataContextType = {
|
|
44
|
+
/** True tant que events ±3j + mois calendrier ne sont pas prêts (loader post-splash). */
|
|
45
|
+
isBootstrapping: boolean;
|
|
46
|
+
city: City | null;
|
|
47
|
+
wasteTypes: WasteType[];
|
|
48
|
+
wasteCollectionPlaces: WasteCollectionPlace[];
|
|
49
|
+
/** Collectes précalculées pour offsets -3 … +3. */
|
|
50
|
+
nextEventsByOffset: Record<number, CollectionForDay[]>;
|
|
51
|
+
locations: MapLocation[];
|
|
52
|
+
mapCenter: { lat: number; lon: number };
|
|
53
|
+
isMapLoading: boolean;
|
|
54
|
+
isRecyclingLoading: boolean;
|
|
55
|
+
scheduleError: string | null;
|
|
56
|
+
mapError: string | null;
|
|
57
|
+
/**
|
|
58
|
+
* Force-refresh tri + carte (invalide le TTL).
|
|
59
|
+
* Utilisé par le pull-to-refresh.
|
|
60
|
+
*/
|
|
61
|
+
refreshCachedData: () => Promise<void>;
|
|
62
|
+
/** Recharge events + calendrier (critique). */
|
|
63
|
+
refreshSchedule: () => Promise<void>;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const AppDataContext = createContext<AppDataContextType | undefined>(undefined);
|
|
67
|
+
|
|
68
|
+
function asArray(value: unknown): unknown[] {
|
|
69
|
+
return Array.isArray(value) ? value : [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function cacheRecyclingPayload(wasteTypes: WasteType[]) {
|
|
73
|
+
return wasteTypes;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function buildNextEventsByOffset(
|
|
77
|
+
wasteTypes: WasteType[],
|
|
78
|
+
places: WasteCollectionPlace[],
|
|
79
|
+
placeId: string | null,
|
|
80
|
+
): Record<number, CollectionForDay[]> {
|
|
81
|
+
const out: Record<number, CollectionForDay[]> = {};
|
|
82
|
+
for (let offset = -NEXT_EVENTS_DAY_RADIUS; offset <= NEXT_EVENTS_DAY_RADIUS; offset += 1) {
|
|
83
|
+
out[offset] = getCollectionsForDay(wasteTypes, places, offset, placeId);
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function collectImageUris(
|
|
89
|
+
apiUrl: string,
|
|
90
|
+
byOffset: Record<number, CollectionForDay[]>,
|
|
91
|
+
): string[] {
|
|
92
|
+
const uris = new Set<string>();
|
|
93
|
+
for (const items of Object.values(byOffset)) {
|
|
94
|
+
for (const item of items) {
|
|
95
|
+
const fromUrl = resolveApiAssetUrl(apiUrl, item.imageUrl);
|
|
96
|
+
if (fromUrl) {
|
|
97
|
+
uris.add(fromUrl);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (
|
|
101
|
+
typeof item.icon === "string" &&
|
|
102
|
+
item.icon.trim() &&
|
|
103
|
+
/^https?:\/\//i.test(item.icon)
|
|
104
|
+
) {
|
|
105
|
+
uris.add(item.icon.trim());
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return Array.from(uris);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function prefetchImages(uris: string[]): Promise<void> {
|
|
113
|
+
if (uris.length === 0) return;
|
|
114
|
+
await Promise.all(
|
|
115
|
+
uris.map(async (uri) => {
|
|
116
|
+
try {
|
|
117
|
+
await Image.prefetch(uri);
|
|
118
|
+
} catch {
|
|
119
|
+
// ignore prefetch failures
|
|
120
|
+
}
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function fetchAllPages(
|
|
126
|
+
fetchPage: (params: { page: number; pageSize: number }) => Promise<unknown>,
|
|
127
|
+
): Promise<unknown[]> {
|
|
128
|
+
const PAGE_SIZE = 100;
|
|
129
|
+
const first = await fetchPage({ page: 1, pageSize: PAGE_SIZE });
|
|
130
|
+
const meta = (first as { meta?: { totalPages?: number } })?.meta;
|
|
131
|
+
const data = asArray((first as { data?: unknown })?.data);
|
|
132
|
+
const totalPages =
|
|
133
|
+
typeof meta?.totalPages === "number" && meta.totalPages >= 1 ? meta.totalPages : 1;
|
|
134
|
+
const items = [...data];
|
|
135
|
+
for (let page = 2; page <= totalPages; page += 1) {
|
|
136
|
+
const res = await fetchPage({ page, pageSize: PAGE_SIZE });
|
|
137
|
+
items.push(...asArray((res as { data?: unknown })?.data));
|
|
138
|
+
}
|
|
139
|
+
return items;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export type AppDataProviderProps = PropsWithChildren & {
|
|
143
|
+
apiUrl: string;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
export function AppDataProvider({ children, apiUrl }: AppDataProviderProps): ReactNode {
|
|
147
|
+
const { selectedCityId, selectedPlaceId, hasPlaceSelected, isLoading: localLoading } =
|
|
148
|
+
useLocal();
|
|
149
|
+
const client = useMemo(() => createWastoClient({ baseUrl: apiUrl }), [apiUrl]);
|
|
150
|
+
const { theme } = useTheme();
|
|
151
|
+
const t = resolveTheme(theme);
|
|
152
|
+
|
|
153
|
+
const [isBootstrapping, setIsBootstrapping] = useState(false);
|
|
154
|
+
const [city, setCity] = useState<City | null>(null);
|
|
155
|
+
const [wasteTypes, setWasteTypes] = useState<WasteType[]>([]);
|
|
156
|
+
const [wasteCollectionPlaces, setWasteCollectionPlaces] = useState<WasteCollectionPlace[]>(
|
|
157
|
+
[],
|
|
158
|
+
);
|
|
159
|
+
const [nextEventsByOffset, setNextEventsByOffset] = useState<
|
|
160
|
+
Record<number, CollectionForDay[]>
|
|
161
|
+
>({});
|
|
162
|
+
const [locations, setLocations] = useState<MapLocation[]>([]);
|
|
163
|
+
const [isMapLoading, setIsMapLoading] = useState(false);
|
|
164
|
+
const [isRecyclingLoading, setIsRecyclingLoading] = useState(false);
|
|
165
|
+
const [scheduleError, setScheduleError] = useState<string | null>(null);
|
|
166
|
+
const [mapError, setMapError] = useState<string | null>(null);
|
|
167
|
+
|
|
168
|
+
const preloadRef = useRef<CityAppPreload | null>(null);
|
|
169
|
+
const cityIdRef = useRef<string | null>(null);
|
|
170
|
+
|
|
171
|
+
const applySchedule = useCallback(
|
|
172
|
+
(
|
|
173
|
+
nextCity: City | null,
|
|
174
|
+
types: WasteType[],
|
|
175
|
+
places: WasteCollectionPlace[],
|
|
176
|
+
placeId: string | null,
|
|
177
|
+
) => {
|
|
178
|
+
setCity(nextCity);
|
|
179
|
+
setWasteTypes(types);
|
|
180
|
+
setWasteCollectionPlaces(places);
|
|
181
|
+
const byOffset = buildNextEventsByOffset(types, places, placeId);
|
|
182
|
+
setNextEventsByOffset(byOffset);
|
|
183
|
+
void prefetchImages(collectImageUris(apiUrl, byOffset));
|
|
184
|
+
},
|
|
185
|
+
[apiUrl],
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
const loadPreload = useCallback(
|
|
189
|
+
async (cityId: string): Promise<CityAppPreload> => {
|
|
190
|
+
try {
|
|
191
|
+
return await client.public.cities.getAppPreload(cityId);
|
|
192
|
+
} catch {
|
|
193
|
+
// Fallback si l’endpoint n’est pas encore déployé
|
|
194
|
+
const [cityRes, types, places, points] = await Promise.all([
|
|
195
|
+
client.public.cities.getById(cityId),
|
|
196
|
+
fetchAllPages((params) =>
|
|
197
|
+
client.public.cities.getWasteTypes(cityId, params),
|
|
198
|
+
),
|
|
199
|
+
fetchAllPages((params) =>
|
|
200
|
+
client.public.cities.getWasteCollectionPlaces(cityId, params),
|
|
201
|
+
),
|
|
202
|
+
fetchAllPages((params) =>
|
|
203
|
+
client.public.cities.getRecyclingPoints(cityId, params),
|
|
204
|
+
),
|
|
205
|
+
]);
|
|
206
|
+
return {
|
|
207
|
+
city: cityRes,
|
|
208
|
+
wasteTypes: types,
|
|
209
|
+
wasteCollectionPlaces: places,
|
|
210
|
+
recyclingPoints: points,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
[client.public.cities],
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
const hydrateCachedLayers = useCallback(
|
|
218
|
+
async (
|
|
219
|
+
cityId: string,
|
|
220
|
+
preload: CityAppPreload,
|
|
221
|
+
force: boolean,
|
|
222
|
+
): Promise<void> => {
|
|
223
|
+
setIsRecyclingLoading(true);
|
|
224
|
+
setIsMapLoading(true);
|
|
225
|
+
setMapError(null);
|
|
226
|
+
|
|
227
|
+
try {
|
|
228
|
+
const types = asArray(preload.wasteTypes) as WasteType[];
|
|
229
|
+
|
|
230
|
+
if (!force) {
|
|
231
|
+
const cachedTypes = await readTtlCache<WasteType[]>(
|
|
232
|
+
CACHE_KEY_RECYCLING(cityId),
|
|
233
|
+
APP_DATA_CACHE_TTL_MS,
|
|
234
|
+
);
|
|
235
|
+
if (cachedTypes && cachedTypes.length > 0) {
|
|
236
|
+
// Cache hit: conserve les types déjà appliqués au schedule ;
|
|
237
|
+
// on réécrit seulement si le preload n’a pas encore hydraté.
|
|
238
|
+
if (types.length === 0) {
|
|
239
|
+
setWasteTypes(cachedTypes);
|
|
240
|
+
}
|
|
241
|
+
} else {
|
|
242
|
+
await writeTtlCache(CACHE_KEY_RECYCLING(cityId), cacheRecyclingPayload(types));
|
|
243
|
+
}
|
|
244
|
+
} else {
|
|
245
|
+
await writeTtlCache(CACHE_KEY_RECYCLING(cityId), cacheRecyclingPayload(types));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let nextLocations: MapLocation[] | null = null;
|
|
249
|
+
if (!force) {
|
|
250
|
+
nextLocations = await readTtlCache<MapLocation[]>(
|
|
251
|
+
CACHE_KEY_MAP(cityId),
|
|
252
|
+
APP_DATA_CACHE_TTL_MS,
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!nextLocations) {
|
|
257
|
+
nextLocations = normalizeMapLocations(
|
|
258
|
+
asArray(preload.recyclingPoints),
|
|
259
|
+
asArray(preload.wasteCollectionPlaces),
|
|
260
|
+
);
|
|
261
|
+
await writeTtlCache(CACHE_KEY_MAP(cityId), nextLocations);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
setLocations(nextLocations);
|
|
265
|
+
} catch (err) {
|
|
266
|
+
setMapError(
|
|
267
|
+
err instanceof Error ? err.message : "Impossible de charger la carte / le tri",
|
|
268
|
+
);
|
|
269
|
+
} finally {
|
|
270
|
+
setIsRecyclingLoading(false);
|
|
271
|
+
setIsMapLoading(false);
|
|
272
|
+
}
|
|
273
|
+
},
|
|
274
|
+
[],
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
const bootstrap = useCallback(
|
|
278
|
+
async (forceCached = false) => {
|
|
279
|
+
const cityId = selectedCityId;
|
|
280
|
+
if (!cityId || !hasPlaceSelected) {
|
|
281
|
+
setIsBootstrapping(false);
|
|
282
|
+
setCity(null);
|
|
283
|
+
setWasteTypes([]);
|
|
284
|
+
setWasteCollectionPlaces([]);
|
|
285
|
+
setNextEventsByOffset({});
|
|
286
|
+
setLocations([]);
|
|
287
|
+
setScheduleError(null);
|
|
288
|
+
preloadRef.current = null;
|
|
289
|
+
cityIdRef.current = null;
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
setIsBootstrapping(true);
|
|
294
|
+
setScheduleError(null);
|
|
295
|
+
|
|
296
|
+
// Hydrate carte / tri depuis le cache local pendant le fetch critique
|
|
297
|
+
if (!forceCached) {
|
|
298
|
+
const [cachedMap, cachedTypes] = await Promise.all([
|
|
299
|
+
readTtlCache<MapLocation[]>(CACHE_KEY_MAP(cityId), APP_DATA_CACHE_TTL_MS),
|
|
300
|
+
readTtlCache<WasteType[]>(CACHE_KEY_RECYCLING(cityId), APP_DATA_CACHE_TTL_MS),
|
|
301
|
+
]);
|
|
302
|
+
if (cachedMap) setLocations(cachedMap);
|
|
303
|
+
if (cachedTypes && cachedTypes.length > 0) {
|
|
304
|
+
// Soft fill — le preload réseau écrasera ensuite
|
|
305
|
+
setWasteTypes((prev) => (prev.length > 0 ? prev : cachedTypes));
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
const preload = await loadPreload(cityId);
|
|
311
|
+
preloadRef.current = preload;
|
|
312
|
+
cityIdRef.current = cityId;
|
|
313
|
+
|
|
314
|
+
const types = asArray(preload.wasteTypes) as WasteType[];
|
|
315
|
+
const places = asArray(preload.wasteCollectionPlaces) as WasteCollectionPlace[];
|
|
316
|
+
applySchedule((preload.city as City) ?? null, types, places, selectedPlaceId);
|
|
317
|
+
|
|
318
|
+
// Events ±3j + mois calendrier prêts → on peut afficher l’UI
|
|
319
|
+
setIsBootstrapping(false);
|
|
320
|
+
|
|
321
|
+
// Tri + carte : cache 1 semaine (ou force)
|
|
322
|
+
void hydrateCachedLayers(cityId, preload, forceCached);
|
|
323
|
+
} catch (err) {
|
|
324
|
+
setScheduleError(
|
|
325
|
+
err instanceof Error ? err.message : "Impossible de précharger les données",
|
|
326
|
+
);
|
|
327
|
+
setIsBootstrapping(false);
|
|
328
|
+
}
|
|
329
|
+
},
|
|
330
|
+
[
|
|
331
|
+
applySchedule,
|
|
332
|
+
hasPlaceSelected,
|
|
333
|
+
hydrateCachedLayers,
|
|
334
|
+
loadPreload,
|
|
335
|
+
selectedCityId,
|
|
336
|
+
selectedPlaceId,
|
|
337
|
+
],
|
|
338
|
+
);
|
|
339
|
+
|
|
340
|
+
useEffect(() => {
|
|
341
|
+
if (localLoading) return;
|
|
342
|
+
void bootstrap(false);
|
|
343
|
+
}, [localLoading, bootstrap]);
|
|
344
|
+
|
|
345
|
+
// Recalcule les events ±3j si le lieu change sans recharger l’API
|
|
346
|
+
useEffect(() => {
|
|
347
|
+
if (!wasteTypes.length && !wasteCollectionPlaces.length) return;
|
|
348
|
+
const byOffset = buildNextEventsByOffset(
|
|
349
|
+
wasteTypes,
|
|
350
|
+
wasteCollectionPlaces,
|
|
351
|
+
selectedPlaceId,
|
|
352
|
+
);
|
|
353
|
+
setNextEventsByOffset(byOffset);
|
|
354
|
+
void prefetchImages(collectImageUris(apiUrl, byOffset));
|
|
355
|
+
}, [apiUrl, selectedPlaceId, wasteCollectionPlaces, wasteTypes]);
|
|
356
|
+
|
|
357
|
+
const refreshSchedule = useCallback(async () => {
|
|
358
|
+
await bootstrap(false);
|
|
359
|
+
}, [bootstrap]);
|
|
360
|
+
|
|
361
|
+
const refreshCachedData = useCallback(async () => {
|
|
362
|
+
const cityId = selectedCityId ?? cityIdRef.current;
|
|
363
|
+
if (!cityId) return;
|
|
364
|
+
|
|
365
|
+
setIsRecyclingLoading(true);
|
|
366
|
+
setIsMapLoading(true);
|
|
367
|
+
try {
|
|
368
|
+
const preload = await loadPreload(cityId);
|
|
369
|
+
preloadRef.current = preload;
|
|
370
|
+
cityIdRef.current = cityId;
|
|
371
|
+
|
|
372
|
+
const types = asArray(preload.wasteTypes) as WasteType[];
|
|
373
|
+
const places = asArray(preload.wasteCollectionPlaces) as WasteCollectionPlace[];
|
|
374
|
+
applySchedule((preload.city as City) ?? null, types, places, selectedPlaceId);
|
|
375
|
+
await hydrateCachedLayers(cityId, preload, true);
|
|
376
|
+
} catch (err) {
|
|
377
|
+
setMapError(
|
|
378
|
+
err instanceof Error ? err.message : "Impossible de rafraîchir les données",
|
|
379
|
+
);
|
|
380
|
+
setIsRecyclingLoading(false);
|
|
381
|
+
setIsMapLoading(false);
|
|
382
|
+
}
|
|
383
|
+
}, [applySchedule, hydrateCachedLayers, loadPreload, selectedCityId, selectedPlaceId]);
|
|
384
|
+
|
|
385
|
+
const mapCenter = useMemo(() => computeMapCenter(locations), [locations]);
|
|
386
|
+
|
|
387
|
+
const value = useMemo<AppDataContextType>(
|
|
388
|
+
() => ({
|
|
389
|
+
isBootstrapping: localLoading || isBootstrapping,
|
|
390
|
+
city,
|
|
391
|
+
wasteTypes,
|
|
392
|
+
wasteCollectionPlaces,
|
|
393
|
+
nextEventsByOffset,
|
|
394
|
+
locations,
|
|
395
|
+
mapCenter,
|
|
396
|
+
isMapLoading,
|
|
397
|
+
isRecyclingLoading,
|
|
398
|
+
scheduleError,
|
|
399
|
+
mapError,
|
|
400
|
+
refreshCachedData,
|
|
401
|
+
refreshSchedule,
|
|
402
|
+
}),
|
|
403
|
+
[
|
|
404
|
+
city,
|
|
405
|
+
isBootstrapping,
|
|
406
|
+
isMapLoading,
|
|
407
|
+
isRecyclingLoading,
|
|
408
|
+
localLoading,
|
|
409
|
+
locations,
|
|
410
|
+
mapCenter,
|
|
411
|
+
mapError,
|
|
412
|
+
nextEventsByOffset,
|
|
413
|
+
refreshCachedData,
|
|
414
|
+
refreshSchedule,
|
|
415
|
+
scheduleError,
|
|
416
|
+
wasteCollectionPlaces,
|
|
417
|
+
wasteTypes,
|
|
418
|
+
],
|
|
419
|
+
);
|
|
420
|
+
|
|
421
|
+
const showLoader = hasPlaceSelected && (localLoading || isBootstrapping);
|
|
422
|
+
|
|
423
|
+
return (
|
|
424
|
+
<AppDataContext value={value}>
|
|
425
|
+
{children}
|
|
426
|
+
{showLoader ? (
|
|
427
|
+
<View
|
|
428
|
+
style={[styles.loader, { backgroundColor: theme.colors.background }]}
|
|
429
|
+
pointerEvents="auto"
|
|
430
|
+
>
|
|
431
|
+
<ActivityIndicator size="large" color={t.colors.primary} />
|
|
432
|
+
</View>
|
|
433
|
+
) : null}
|
|
434
|
+
</AppDataContext>
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export function useAppData(): AppDataContextType {
|
|
439
|
+
const ctx = useContext(AppDataContext);
|
|
440
|
+
if (!ctx) {
|
|
441
|
+
throw new Error("useAppData must be used within an AppDataProvider.");
|
|
442
|
+
}
|
|
443
|
+
return ctx;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/** Safe variant for hooks that may run outside (preview / tests). */
|
|
447
|
+
export function useOptionalAppData(): AppDataContextType | null {
|
|
448
|
+
return useContext(AppDataContext) ?? null;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const styles = StyleSheet.create({
|
|
452
|
+
loader: {
|
|
453
|
+
...StyleSheet.absoluteFillObject,
|
|
454
|
+
zIndex: 1000,
|
|
455
|
+
alignItems: "center",
|
|
456
|
+
justifyContent: "center",
|
|
457
|
+
},
|
|
458
|
+
});
|