@wasto/cms-components 1.5.10 → 1.5.12

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 (101) hide show
  1. package/README.md +47 -47
  2. package/dist/index.d.mts +181 -51
  3. package/dist/index.d.ts +181 -51
  4. package/dist/index.js +3614 -3015
  5. package/dist/index.mjs +3021 -2418
  6. package/package.json +12 -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 +415 -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 +546 -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/push/PushTokenRegistrar.tsx +47 -0
  49. package/src/components/recycling-info/RecyclingInfo.tsx +189 -189
  50. package/src/components/safe-area/OptionalSafeAreaProvider.tsx +17 -17
  51. package/src/components/search/Search.tsx +320 -320
  52. package/src/components/select-street-modal/SelectStreetModal.tsx +475 -475
  53. package/src/components/title/Title.tsx +100 -100
  54. package/src/components/vision/Vision.tsx +447 -447
  55. package/src/components/vision/VisionActionCard.tsx +117 -117
  56. package/src/components/vision/VisionCapture.tsx +30 -30
  57. package/src/components/vision/VisionTapOverlay.tsx +122 -122
  58. package/src/contexts/AnnouncementsContext.tsx +171 -171
  59. package/src/contexts/AppDataContext.tsx +458 -0
  60. package/src/contexts/ChallengesContext.tsx +182 -182
  61. package/src/contexts/HeaderConfigContext.tsx +29 -0
  62. package/src/contexts/LocalContext.tsx +227 -227
  63. package/src/contexts/ThemeContext.tsx +37 -37
  64. package/src/contexts/WastoContext.tsx +132 -127
  65. package/src/helpers/blogPost.ts +72 -72
  66. package/src/helpers/generateQuizQuestions.ts +247 -247
  67. package/src/helpers/getContrastColor.ts +18 -18
  68. package/src/helpers/haptics.ts +20 -20
  69. package/src/helpers/hoursSchedule.ts +230 -230
  70. package/src/helpers/htmlContentToMarkdown.ts +114 -114
  71. package/src/helpers/layout.ts +54 -54
  72. package/src/helpers/loadLinearGradient.ts +22 -22
  73. package/src/helpers/mapLocations.ts +170 -0
  74. package/src/helpers/maquetteTheme.ts +76 -76
  75. package/src/helpers/markdownUnderlinePlugin.ts +32 -32
  76. package/src/helpers/nextCollection.ts +122 -0
  77. package/src/helpers/normalizeWasteTypeRecyclingInfo.ts +80 -80
  78. package/src/helpers/publicMediaUrl.ts +8 -8
  79. package/src/helpers/resolveApiAssetUrl.ts +21 -21
  80. package/src/helpers/systemRoutes.ts +7 -7
  81. package/src/helpers/themeStyles.ts +114 -114
  82. package/src/helpers/ttlCache.ts +41 -0
  83. package/src/helpers/useSafeAreaPadding.ts +14 -14
  84. package/src/hooks/useBadges.ts +100 -100
  85. package/src/hooks/useBlogPosts.ts +109 -105
  86. package/src/hooks/useChallenges.ts +110 -110
  87. package/src/hooks/useCities.ts +56 -56
  88. package/src/hooks/useCityAnnouncements.ts +118 -118
  89. package/src/hooks/useCollectionCalendar.ts +421 -421
  90. package/src/hooks/useColumns.ts +19 -19
  91. package/src/hooks/useConsignes.ts +62 -62
  92. package/src/hooks/useLocations.ts +120 -248
  93. package/src/hooks/useNextCollection.ts +163 -238
  94. package/src/hooks/useRecyclingInfo.ts +90 -90
  95. package/src/hooks/useWastoClient.ts +16 -16
  96. package/src/index.ts +161 -142
  97. package/src/navigation/NavigationContext.tsx +56 -56
  98. package/src/push/registerExpoPushToken.ts +133 -0
  99. package/src/sentry/initSentry.ts +120 -120
  100. package/src/types/Theme.ts +72 -72
  101. package/src/types/css-modules.d.ts +1 -1
@@ -1,105 +1,109 @@
1
- import { useCallback, useEffect, useState } from "react";
2
- import { useNextCollection } from "./useNextCollection";
3
- import { useWastoClient } from "./useWastoClient";
4
- import { normalizeBlogPost, type BlogPost } from "../helpers/blogPost";
5
-
6
- function cityGroupIdFromCity(city: Record<string, unknown> | null): string | undefined {
7
- if (!city) return undefined;
8
- const raw =
9
- city.cityGroupId ??
10
- city.city_group_id ??
11
- (city.cityGroup != null && typeof city.cityGroup === "object"
12
- ? (city.cityGroup as { id?: unknown }).id
13
- : undefined);
14
- return typeof raw === "string" && raw.trim() ? raw.trim() : undefined;
15
- }
16
-
17
- export type UseBlogPostsResult = {
18
- posts: BlogPost[];
19
- isLoading: boolean;
20
- error: string | null;
21
- refetch: () => Promise<void>;
22
- };
23
-
24
- export function useBlogPosts(options: { pageSize?: number } = {}): UseBlogPostsResult {
25
- const { city } = useNextCollection();
26
- const client = useWastoClient();
27
- const pageSize = options.pageSize ?? 50;
28
- const cityGroupId = cityGroupIdFromCity(city as Record<string, unknown> | null);
29
-
30
- const [posts, setPosts] = useState<BlogPost[]>([]);
31
- const [isLoading, setIsLoading] = useState(false);
32
- const [error, setError] = useState<string | null>(null);
33
-
34
- const fetchPosts = useCallback(async () => {
35
- try {
36
- setIsLoading(true);
37
- setError(null);
38
- const res = await client.public.blogPosts.list(cityGroupId, { pageSize });
39
- const data = (res as { data?: unknown })?.data;
40
- const list = Array.isArray(data) ? data : [];
41
- setPosts(list.map(normalizeBlogPost).filter((p): p is BlogPost => p != null));
42
- } catch (err) {
43
- setError(
44
- err instanceof Error ? err.message : "Impossible de charger les articles"
45
- );
46
- setPosts([]);
47
- } finally {
48
- setIsLoading(false);
49
- }
50
- }, [cityGroupId, client.public.blogPosts, pageSize]);
51
-
52
- useEffect(() => {
53
- void fetchPosts();
54
- }, [fetchPosts]);
55
-
56
- return { posts, isLoading, error, refetch: fetchPosts };
57
- }
58
-
59
- export type UseBlogPostResult = {
60
- post: BlogPost | null;
61
- isLoading: boolean;
62
- error: string | null;
63
- };
64
-
65
- export function useBlogPost(articleId: string): UseBlogPostResult {
66
- const client = useWastoClient();
67
- const [post, setPost] = useState<BlogPost | null>(null);
68
- const [isLoading, setIsLoading] = useState(false);
69
- const [error, setError] = useState<string | null>(null);
70
-
71
- useEffect(() => {
72
- const id = articleId.trim();
73
- if (!id) {
74
- setPost(null);
75
- setError(null);
76
- setIsLoading(false);
77
- return;
78
- }
79
-
80
- let cancelled = false;
81
- const run = async () => {
82
- try {
83
- setIsLoading(true);
84
- setError(null);
85
- const res = await client.public.blogPosts.getById(id);
86
- if (cancelled) return;
87
- const normalized = normalizeBlogPost(res);
88
- setPost(normalized);
89
- if (!normalized) setError("Article introuvable");
90
- } catch (err) {
91
- if (cancelled) return;
92
- setPost(null);
93
- setError(err instanceof Error ? err.message : "Impossible de charger l'article");
94
- } finally {
95
- if (!cancelled) setIsLoading(false);
96
- }
97
- };
98
- void run();
99
- return () => {
100
- cancelled = true;
101
- };
102
- }, [articleId, client.public.blogPosts]);
103
-
104
- return { post, isLoading, error };
105
- }
1
+ import { useCallback, useEffect, useState } from "react";
2
+ import { useWasto } from "../contexts/WastoContext";
3
+ import { normalizeBlogPost, type BlogPost } from "../helpers/blogPost";
4
+ import { useNextCollection } from "./useNextCollection";
5
+ import { useWastoClient } from "./useWastoClient";
6
+
7
+ function cityGroupIdFromCity(city: Record<string, unknown> | null): string | undefined {
8
+ if (!city) return undefined;
9
+ const raw =
10
+ city.cityGroupId ??
11
+ city.city_group_id ??
12
+ (city.cityGroup != null && typeof city.cityGroup === "object"
13
+ ? (city.cityGroup as { id?: unknown }).id
14
+ : undefined);
15
+ return typeof raw === "string" && raw.trim() ? raw.trim() : undefined;
16
+ }
17
+
18
+ export type UseBlogPostsResult = {
19
+ posts: BlogPost[];
20
+ isLoading: boolean;
21
+ error: string | null;
22
+ refetch: () => Promise<void>;
23
+ };
24
+
25
+ export function useBlogPosts(options: { pageSize?: number } = {}): UseBlogPostsResult {
26
+ const { city } = useNextCollection();
27
+ const { cityGroupId: contextCityGroupId } = useWasto();
28
+ const client = useWastoClient();
29
+ const pageSize = options.pageSize ?? 50;
30
+ const cityGroupId =
31
+ cityGroupIdFromCity(city as Record<string, unknown> | null) ??
32
+ (contextCityGroupId?.trim() || undefined);
33
+
34
+ const [posts, setPosts] = useState<BlogPost[]>([]);
35
+ const [isLoading, setIsLoading] = useState(false);
36
+ const [error, setError] = useState<string | null>(null);
37
+
38
+ const fetchPosts = useCallback(async () => {
39
+ try {
40
+ setIsLoading(true);
41
+ setError(null);
42
+ const res = await client.public.blogPosts.list(cityGroupId, { pageSize });
43
+ const data = (res as { data?: unknown })?.data;
44
+ const list = Array.isArray(data) ? data : [];
45
+ setPosts(list.map(normalizeBlogPost).filter((p): p is BlogPost => p != null));
46
+ } catch (err) {
47
+ setError(
48
+ err instanceof Error ? err.message : "Impossible de charger les articles"
49
+ );
50
+ setPosts([]);
51
+ } finally {
52
+ setIsLoading(false);
53
+ }
54
+ }, [cityGroupId, client.public.blogPosts, pageSize]);
55
+
56
+ useEffect(() => {
57
+ void fetchPosts();
58
+ }, [fetchPosts]);
59
+
60
+ return { posts, isLoading, error, refetch: fetchPosts };
61
+ }
62
+
63
+ export type UseBlogPostResult = {
64
+ post: BlogPost | null;
65
+ isLoading: boolean;
66
+ error: string | null;
67
+ };
68
+
69
+ export function useBlogPost(articleId: string): UseBlogPostResult {
70
+ const client = useWastoClient();
71
+ const [post, setPost] = useState<BlogPost | null>(null);
72
+ const [isLoading, setIsLoading] = useState(false);
73
+ const [error, setError] = useState<string | null>(null);
74
+
75
+ useEffect(() => {
76
+ const id = articleId.trim();
77
+ if (!id) {
78
+ setPost(null);
79
+ setError(null);
80
+ setIsLoading(false);
81
+ return;
82
+ }
83
+
84
+ let cancelled = false;
85
+ const run = async () => {
86
+ try {
87
+ setIsLoading(true);
88
+ setError(null);
89
+ const res = await client.public.blogPosts.getById(id);
90
+ if (cancelled) return;
91
+ const normalized = normalizeBlogPost(res);
92
+ setPost(normalized);
93
+ if (!normalized) setError("Article introuvable");
94
+ } catch (err) {
95
+ if (cancelled) return;
96
+ setPost(null);
97
+ setError(err instanceof Error ? err.message : "Impossible de charger l'article");
98
+ } finally {
99
+ if (!cancelled) setIsLoading(false);
100
+ }
101
+ };
102
+ void run();
103
+ return () => {
104
+ cancelled = true;
105
+ };
106
+ }, [articleId, client.public.blogPosts]);
107
+
108
+ return { post, isLoading, error };
109
+ }
@@ -1,110 +1,110 @@
1
- import { useChallengesContext } from "../contexts/ChallengesContext";
2
-
3
- export type Challenge = {
4
- id: string;
5
- name: string;
6
- description?: string;
7
- imageUrl?: string | null;
8
- enabled?: boolean;
9
- startDate?: string;
10
- endDate?: string;
11
- order?: number;
12
- requiredChallengeId?: string | null;
13
- requiredChallenge?: { id: string; name: string } | null;
14
- series?: Array<{
15
- id: string;
16
- challengeId: string;
17
- name: string;
18
- description?: string;
19
- imageUrl?: string | null;
20
- order?: number;
21
- quizQuestions?: Array<{
22
- id: string;
23
- material?: string;
24
- question?: string;
25
- correctWasteTypeId?: string;
26
- order?: number;
27
- imageUrl?: string | null;
28
- kind?: string | null;
29
- options?: unknown;
30
- explanationText?: string | null;
31
- explanationDetailId?: string | null;
32
- explanationDetail?: {
33
- material?: string | null;
34
- description?: string | null;
35
- instructions?: string | null;
36
- } | null;
37
- }>;
38
- }>;
39
- badge?: {
40
- id: string;
41
- challengeId: string;
42
- name: string;
43
- description?: string;
44
- icon?: string;
45
- imageUrl?: string;
46
- } | null;
47
- };
48
-
49
- export type SeriesProgress = {
50
- seriesId: string;
51
- score: number;
52
- completedAt: string;
53
- };
54
-
55
- export type ChallengeSeriesWithProgress = NonNullable<Challenge["series"]>[number] & {
56
- progression?: SeriesProgress;
57
- };
58
-
59
- /** `Omit<Challenge, "series">` évite que `series` de `Challenge` écrase le type enrichi. */
60
- export type ChallengeWithProgress = Omit<Challenge, "series"> & {
61
- progression?: UserProgress;
62
- series?: ChallengeSeriesWithProgress[];
63
- };
64
-
65
- export type UserProgress = {
66
- challengeId: string;
67
- challengeName?: string;
68
- score?: number;
69
- completedAt: string;
70
- badge?: Challenge["badge"];
71
- series?: SeriesProgress[];
72
- [key: string]: unknown;
73
- };
74
-
75
- export type AcquiredBadge = {
76
- badgeId: string;
77
- challengeId: string;
78
- earnedAt: string;
79
- badge?: Challenge["badge"];
80
- };
81
-
82
- export type UseChallengesOptions = {
83
- /** Conservé pour compat ; le store partagé utilise LocalContext. */
84
- cityId?: string | null;
85
- pageSize?: number;
86
- deviceId?: string | null;
87
- };
88
-
89
- export type UseChallengesResult = {
90
- cityId: string | null;
91
- challenges: ChallengeWithProgress[];
92
- userProgress: UserProgress[];
93
- /** Progression indexée par `challengeId` (pratique pour l'affichage). */
94
- progressByChallengeId: Record<string, UserProgress>;
95
- /** Raccourci pour récupérer la progression d'un challenge. */
96
- getProgressByChallengeId: (challengeId: string) => UserProgress | undefined;
97
- acquiredBadges: AcquiredBadge[];
98
- isLoading: boolean;
99
- error: string | null;
100
- refetch: () => Promise<void>;
101
- getChallengeById: (id: string) => ChallengeWithProgress | undefined;
102
- };
103
-
104
- /**
105
- * Shared challenges store (via ChallengesProvider inside WastoProvider).
106
- * Options are ignored — city/device come from LocalContext so all screens stay in sync.
107
- */
108
- export function useChallenges(_options: UseChallengesOptions = {}): UseChallengesResult {
109
- return useChallengesContext();
110
- }
1
+ import { useChallengesContext } from "../contexts/ChallengesContext";
2
+
3
+ export type Challenge = {
4
+ id: string;
5
+ name: string;
6
+ description?: string;
7
+ imageUrl?: string | null;
8
+ enabled?: boolean;
9
+ startDate?: string;
10
+ endDate?: string;
11
+ order?: number;
12
+ requiredChallengeId?: string | null;
13
+ requiredChallenge?: { id: string; name: string } | null;
14
+ series?: Array<{
15
+ id: string;
16
+ challengeId: string;
17
+ name: string;
18
+ description?: string;
19
+ imageUrl?: string | null;
20
+ order?: number;
21
+ quizQuestions?: Array<{
22
+ id: string;
23
+ material?: string;
24
+ question?: string;
25
+ correctWasteTypeId?: string;
26
+ order?: number;
27
+ imageUrl?: string | null;
28
+ kind?: string | null;
29
+ options?: unknown;
30
+ explanationText?: string | null;
31
+ explanationDetailId?: string | null;
32
+ explanationDetail?: {
33
+ material?: string | null;
34
+ description?: string | null;
35
+ instructions?: string | null;
36
+ } | null;
37
+ }>;
38
+ }>;
39
+ badge?: {
40
+ id: string;
41
+ challengeId: string;
42
+ name: string;
43
+ description?: string;
44
+ icon?: string;
45
+ imageUrl?: string;
46
+ } | null;
47
+ };
48
+
49
+ export type SeriesProgress = {
50
+ seriesId: string;
51
+ score: number;
52
+ completedAt: string;
53
+ };
54
+
55
+ export type ChallengeSeriesWithProgress = NonNullable<Challenge["series"]>[number] & {
56
+ progression?: SeriesProgress;
57
+ };
58
+
59
+ /** `Omit<Challenge, "series">` évite que `series` de `Challenge` écrase le type enrichi. */
60
+ export type ChallengeWithProgress = Omit<Challenge, "series"> & {
61
+ progression?: UserProgress;
62
+ series?: ChallengeSeriesWithProgress[];
63
+ };
64
+
65
+ export type UserProgress = {
66
+ challengeId: string;
67
+ challengeName?: string;
68
+ score?: number;
69
+ completedAt: string;
70
+ badge?: Challenge["badge"];
71
+ series?: SeriesProgress[];
72
+ [key: string]: unknown;
73
+ };
74
+
75
+ export type AcquiredBadge = {
76
+ badgeId: string;
77
+ challengeId: string;
78
+ earnedAt: string;
79
+ badge?: Challenge["badge"];
80
+ };
81
+
82
+ export type UseChallengesOptions = {
83
+ /** Conservé pour compat ; le store partagé utilise LocalContext. */
84
+ cityId?: string | null;
85
+ pageSize?: number;
86
+ deviceId?: string | null;
87
+ };
88
+
89
+ export type UseChallengesResult = {
90
+ cityId: string | null;
91
+ challenges: ChallengeWithProgress[];
92
+ userProgress: UserProgress[];
93
+ /** Progression indexée par `challengeId` (pratique pour l'affichage). */
94
+ progressByChallengeId: Record<string, UserProgress>;
95
+ /** Raccourci pour récupérer la progression d'un challenge. */
96
+ getProgressByChallengeId: (challengeId: string) => UserProgress | undefined;
97
+ acquiredBadges: AcquiredBadge[];
98
+ isLoading: boolean;
99
+ error: string | null;
100
+ refetch: () => Promise<void>;
101
+ getChallengeById: (id: string) => ChallengeWithProgress | undefined;
102
+ };
103
+
104
+ /**
105
+ * Shared challenges store (via ChallengesProvider inside WastoProvider).
106
+ * Options are ignored — city/device come from LocalContext so all screens stay in sync.
107
+ */
108
+ export function useChallenges(_options: UseChallengesOptions = {}): UseChallengesResult {
109
+ return useChallengesContext();
110
+ }
@@ -1,56 +1,56 @@
1
- import { useCallback, useEffect, useMemo, useState } from "react";
2
- import { useWastoClient } from "./useWastoClient";
3
-
4
- export type City = {
5
- id: string;
6
- name: string;
7
- status?: "NON_ACTIF" | "INCOMPLET" | "ACTIF" | "MAINTENANCE";
8
- [key: string]: unknown;
9
- };
10
-
11
- export type UseCitiesResult = {
12
- cities: City[];
13
- isLoading: boolean;
14
- error: string | null;
15
- refetch: () => Promise<void>;
16
- getCityById: (id: string) => City | undefined;
17
- };
18
-
19
- export type UseCitiesOptions = {
20
- pageSize?: number;
21
- };
22
-
23
- export function useCities(options: UseCitiesOptions = {}): UseCitiesResult {
24
- const client = useWastoClient();
25
-
26
- const [cities, setCities] = useState<City[]>([]);
27
- const [isLoading, setIsLoading] = useState<boolean>(true);
28
- const [error, setError] = useState<string | null>(null);
29
-
30
- const pageSize = options.pageSize ?? 100;
31
-
32
- const fetchCities = useCallback(async () => {
33
- try {
34
- setIsLoading(true);
35
- setError(null);
36
- const res = await client.public.cities.findAll({ pageSize });
37
- const data = (res as { data?: unknown })?.data;
38
- setCities(Array.isArray(data) ? (data as City[]) : []);
39
- } catch (err) {
40
- const message =
41
- err instanceof Error ? err.message : typeof err === "string" ? err : "Impossible de charger les villes";
42
- setError(message);
43
- setCities([]);
44
- } finally {
45
- setIsLoading(false);
46
- }
47
- }, [client, pageSize]);
48
-
49
- useEffect(() => {
50
- void fetchCities();
51
- }, [fetchCities]);
52
-
53
- const getCityById = useCallback((id: string) => cities.find((c) => c.id === id), [cities]);
54
-
55
- return { cities, isLoading, error, refetch: fetchCities, getCityById };
56
- }
1
+ import { useCallback, useEffect, useMemo, useState } from "react";
2
+ import { useWastoClient } from "./useWastoClient";
3
+
4
+ export type City = {
5
+ id: string;
6
+ name: string;
7
+ status?: "NON_ACTIF" | "INCOMPLET" | "ACTIF" | "MAINTENANCE";
8
+ [key: string]: unknown;
9
+ };
10
+
11
+ export type UseCitiesResult = {
12
+ cities: City[];
13
+ isLoading: boolean;
14
+ error: string | null;
15
+ refetch: () => Promise<void>;
16
+ getCityById: (id: string) => City | undefined;
17
+ };
18
+
19
+ export type UseCitiesOptions = {
20
+ pageSize?: number;
21
+ };
22
+
23
+ export function useCities(options: UseCitiesOptions = {}): UseCitiesResult {
24
+ const client = useWastoClient();
25
+
26
+ const [cities, setCities] = useState<City[]>([]);
27
+ const [isLoading, setIsLoading] = useState<boolean>(true);
28
+ const [error, setError] = useState<string | null>(null);
29
+
30
+ const pageSize = options.pageSize ?? 100;
31
+
32
+ const fetchCities = useCallback(async () => {
33
+ try {
34
+ setIsLoading(true);
35
+ setError(null);
36
+ const res = await client.public.cities.findAll({ pageSize });
37
+ const data = (res as { data?: unknown })?.data;
38
+ setCities(Array.isArray(data) ? (data as City[]) : []);
39
+ } catch (err) {
40
+ const message =
41
+ err instanceof Error ? err.message : typeof err === "string" ? err : "Impossible de charger les villes";
42
+ setError(message);
43
+ setCities([]);
44
+ } finally {
45
+ setIsLoading(false);
46
+ }
47
+ }, [client, pageSize]);
48
+
49
+ useEffect(() => {
50
+ void fetchCities();
51
+ }, [fetchCities]);
52
+
53
+ const getCityById = useCallback((id: string) => cities.find((c) => c.id === id), [cities]);
54
+
55
+ return { cities, isLoading, error, refetch: fetchCities, getCityById };
56
+ }