@wasto/cms-components 1.5.10
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 -0
- package/dist/index.d.mts +465 -0
- package/dist/index.d.ts +465 -0
- package/dist/index.js +10445 -0
- package/dist/index.mjs +10553 -0
- package/package.json +90 -0
- package/src/components/announcement-modal/AnnouncementModal.tsx +183 -0
- package/src/components/article/Article.tsx +22 -0
- package/src/components/article/ArticleCard.tsx +81 -0
- package/src/components/article/ArticleExcerpts.tsx +80 -0
- package/src/components/article/ArticleList.tsx +114 -0
- package/src/components/article/ArticleMarkdown.tsx +170 -0
- package/src/components/article/ArticleReaderModal.tsx +93 -0
- package/src/components/article/ArticleSingle.tsx +57 -0
- package/src/components/bottom-tab/BottomTab.tsx +392 -0
- package/src/components/button/Button.tsx +154 -0
- package/src/components/challenge/Badges.tsx +84 -0
- package/src/components/challenge/BadgesModal.tsx +166 -0
- package/src/components/challenge/Challenge.tsx +380 -0
- package/src/components/challenge/ChallengeModal.tsx +231 -0
- package/src/components/challenge/ChallengeVignette.tsx +249 -0
- package/src/components/challenge/ChallengesModal.tsx +243 -0
- package/src/components/challenge/QuizModal.tsx +381 -0
- package/src/components/challenge/ResultModal.tsx +144 -0
- package/src/components/chat/Chat.tsx +928 -0
- package/src/components/collection-calendar/CollectionCalendar.tsx +274 -0
- package/src/components/columns/Columns.tsx +34 -0
- package/src/components/consignes-modal/ConsignesModal.tsx +267 -0
- package/src/components/content-inset/ContentInset.tsx +33 -0
- package/src/components/header/Header.tsx +391 -0
- package/src/components/image/Image.tsx +16 -0
- package/src/components/image/ImageFrame.tsx +102 -0
- package/src/components/image/ImageMedia.tsx +31 -0
- package/src/components/image/ImageUrl.tsx +30 -0
- package/src/components/locations-map/LocationSheet.tsx +417 -0
- package/src/components/locations-map/LocationsMap.native.tsx +260 -0
- package/src/components/locations-map/LocationsMap.tsx +13 -0
- package/src/components/locations-map/LocationsMap.web.tsx +10 -0
- package/src/components/locations-map/LocationsMapShell.tsx +527 -0
- package/src/components/locations-map/LocationsMapView.tsx +108 -0
- package/src/components/locations-map/LocationsMapView.web.tsx +331 -0
- package/src/components/nav-button/NavButton.tsx +59 -0
- package/src/components/next-collection/NextCollection.tsx +621 -0
- package/src/components/notifications/NotificationsScreen.tsx +247 -0
- package/src/components/page/Page.tsx +64 -0
- package/src/components/parapraph/Paragraph.tsx +67 -0
- package/src/components/profile/ProfileScreen.tsx +236 -0
- package/src/components/recycling-info/RecyclingInfo.tsx +189 -0
- package/src/components/safe-area/OptionalSafeAreaProvider.tsx +17 -0
- package/src/components/search/Search.tsx +320 -0
- package/src/components/select-street-modal/SelectStreetModal.tsx +475 -0
- package/src/components/title/Title.tsx +100 -0
- package/src/components/vision/Vision.tsx +447 -0
- package/src/components/vision/VisionActionCard.tsx +117 -0
- package/src/components/vision/VisionCapture.tsx +30 -0
- package/src/components/vision/VisionTapOverlay.tsx +122 -0
- package/src/contexts/AnnouncementsContext.tsx +171 -0
- package/src/contexts/ChallengesContext.tsx +182 -0
- package/src/contexts/LocalContext.tsx +227 -0
- package/src/contexts/ThemeContext.tsx +37 -0
- package/src/contexts/WastoContext.tsx +127 -0
- package/src/helpers/blogPost.ts +72 -0
- package/src/helpers/generateQuizQuestions.ts +247 -0
- package/src/helpers/getContrastColor.ts +19 -0
- package/src/helpers/haptics.ts +20 -0
- package/src/helpers/hoursSchedule.ts +230 -0
- package/src/helpers/htmlContentToMarkdown.ts +114 -0
- package/src/helpers/layout.ts +54 -0
- package/src/helpers/loadLinearGradient.ts +22 -0
- package/src/helpers/maquetteTheme.ts +76 -0
- package/src/helpers/markdownUnderlinePlugin.ts +32 -0
- package/src/helpers/normalizeWasteTypeRecyclingInfo.ts +80 -0
- package/src/helpers/publicMediaUrl.ts +8 -0
- package/src/helpers/resolveApiAssetUrl.ts +21 -0
- package/src/helpers/systemRoutes.ts +7 -0
- package/src/helpers/themeStyles.ts +114 -0
- package/src/helpers/useSafeAreaPadding.ts +14 -0
- package/src/hooks/useBadges.ts +100 -0
- package/src/hooks/useBlogPosts.ts +105 -0
- package/src/hooks/useChallenges.ts +110 -0
- package/src/hooks/useCities.ts +56 -0
- package/src/hooks/useCityAnnouncements.ts +118 -0
- package/src/hooks/useCollectionCalendar.ts +421 -0
- package/src/hooks/useColumns.ts +20 -0
- package/src/hooks/useConsignes.ts +62 -0
- package/src/hooks/useLocations.ts +248 -0
- package/src/hooks/useNextCollection.ts +238 -0
- package/src/hooks/useRecyclingInfo.ts +90 -0
- package/src/hooks/useWastoClient.ts +16 -0
- package/src/index.ts +142 -0
- package/src/navigation/NavigationContext.tsx +56 -0
- package/src/sentry/initSentry.ts +120 -0
- package/src/types/Theme.ts +72 -0
- package/src/types/css-modules.d.ts +1 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { ViewStyle } from "react-native";
|
|
2
|
+
import type { ResolvedTheme, Theme, ThemeShadowStyle } from "../types/Theme";
|
|
3
|
+
|
|
4
|
+
function withAlpha(hex: string, alphaHex: string): string {
|
|
5
|
+
const h = hex.replace("#", "").trim();
|
|
6
|
+
if (h.length === 3) {
|
|
7
|
+
const r = h[0];
|
|
8
|
+
const g = h[1];
|
|
9
|
+
const b = h[2];
|
|
10
|
+
return `#${r}${r}${g}${g}${b}${b}${alphaHex}`;
|
|
11
|
+
}
|
|
12
|
+
if (h.length === 6) return `#${h}${alphaHex}`;
|
|
13
|
+
if (h.length === 8) return `#${h.slice(0, 6)}${alphaHex}`;
|
|
14
|
+
return `${hex}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const DEFAULT_SHADOW_COLOR = "#0F172A";
|
|
18
|
+
|
|
19
|
+
/** Console-like shadow-sm defaults. */
|
|
20
|
+
function defaultCardShadow(): ThemeShadowStyle {
|
|
21
|
+
return {
|
|
22
|
+
shadowColor: DEFAULT_SHADOW_COLOR,
|
|
23
|
+
shadowOffset: { width: 0, height: 1 },
|
|
24
|
+
shadowOpacity: 0.06,
|
|
25
|
+
shadowRadius: 3,
|
|
26
|
+
elevation: 1,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function defaultSoftShadow(): ThemeShadowStyle {
|
|
31
|
+
return {
|
|
32
|
+
shadowColor: DEFAULT_SHADOW_COLOR,
|
|
33
|
+
shadowOffset: { width: 0, height: 1 },
|
|
34
|
+
shadowOpacity: 0.04,
|
|
35
|
+
shadowRadius: 2,
|
|
36
|
+
elevation: 1,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Fill optional theme fields with sensible defaults (retrocompat / maquette-like). */
|
|
41
|
+
export function resolveTheme(theme: Theme): ResolvedTheme {
|
|
42
|
+
// Maquette default principal radius when omitted (cards ~20).
|
|
43
|
+
const radius = theme.radius ?? 20;
|
|
44
|
+
const primary = theme.colors.primary;
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
...theme,
|
|
48
|
+
colors: {
|
|
49
|
+
...theme.colors,
|
|
50
|
+
primarySoft: theme.colors.primarySoft ?? withAlpha(primary, "22"),
|
|
51
|
+
surfaceElevated: theme.colors.surfaceElevated ?? theme.colors.surface,
|
|
52
|
+
muted: theme.colors.muted ?? withAlpha(theme.colors.onSurface, "99"),
|
|
53
|
+
},
|
|
54
|
+
radii: theme.radii ?? {
|
|
55
|
+
sm: Math.max(12, Math.round(radius * 0.6)),
|
|
56
|
+
md: Math.max(14, Math.round(radius * 0.8)),
|
|
57
|
+
lg: Math.max(radius, Math.round(radius * 1.2)),
|
|
58
|
+
pill: 999,
|
|
59
|
+
},
|
|
60
|
+
shadow: {
|
|
61
|
+
card: theme.shadow?.card ?? defaultCardShadow(),
|
|
62
|
+
soft: theme.shadow?.soft ?? defaultSoftShadow(),
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Soft elevation tinted with a brand / category color.
|
|
69
|
+
* Prefer border + category fill for cards; use sparingly (e.g. active chips).
|
|
70
|
+
*/
|
|
71
|
+
export function coloredShadow(hex: string, opacity = 0.12): ThemeShadowStyle {
|
|
72
|
+
return {
|
|
73
|
+
shadowColor: hex,
|
|
74
|
+
shadowOffset: { width: 0, height: 2 },
|
|
75
|
+
shadowOpacity: opacity,
|
|
76
|
+
shadowRadius: 4,
|
|
77
|
+
elevation: 2,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Shared card: surface + border + light shadow (console / shadcn). */
|
|
82
|
+
export function cardStyle(theme: Theme, opts?: { elevated?: boolean }): ViewStyle {
|
|
83
|
+
const t = resolveTheme(theme);
|
|
84
|
+
return {
|
|
85
|
+
backgroundColor: opts?.elevated ? t.colors.surfaceElevated : t.colors.surface,
|
|
86
|
+
borderRadius: t.radii.lg,
|
|
87
|
+
borderWidth: 1,
|
|
88
|
+
borderColor: t.colors.border,
|
|
89
|
+
...t.shadow.card,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function softCardStyle(theme: Theme): ViewStyle {
|
|
94
|
+
const t = resolveTheme(theme);
|
|
95
|
+
return {
|
|
96
|
+
backgroundColor: t.colors.surface,
|
|
97
|
+
borderRadius: t.radii.md,
|
|
98
|
+
borderWidth: 1,
|
|
99
|
+
borderColor: t.colors.border,
|
|
100
|
+
...t.shadow.soft,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Inset grouped list section: one surface, rows separated by internal hairlines. */
|
|
105
|
+
export function groupedSectionStyle(theme: Theme): ViewStyle {
|
|
106
|
+
const t = resolveTheme(theme);
|
|
107
|
+
return {
|
|
108
|
+
backgroundColor: t.colors.surface,
|
|
109
|
+
borderRadius: t.radii.lg,
|
|
110
|
+
borderWidth: 1,
|
|
111
|
+
borderColor: t.colors.border,
|
|
112
|
+
overflow: "hidden",
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
2
|
+
|
|
3
|
+
export type SafeAreaEdge = "top" | "bottom" | "left" | "right";
|
|
4
|
+
|
|
5
|
+
/** Padding on a regular RN `View` — avoids Fabric `SafeAreaView` / codegen in bundled libs. */
|
|
6
|
+
export function useSafeAreaPadding(edges: readonly SafeAreaEdge[]) {
|
|
7
|
+
const insets = useSafeAreaInsets();
|
|
8
|
+
return {
|
|
9
|
+
...(edges.includes("top") ? { paddingTop: insets.top } : null),
|
|
10
|
+
...(edges.includes("bottom") ? { paddingBottom: insets.bottom } : null),
|
|
11
|
+
...(edges.includes("left") ? { paddingLeft: insets.left } : null),
|
|
12
|
+
...(edges.includes("right") ? { paddingRight: insets.right } : null),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { useLocal } from "../contexts/LocalContext";
|
|
3
|
+
import { useWastoClient } from "./useWastoClient";
|
|
4
|
+
|
|
5
|
+
export type Badge = {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
imageUrl?: string;
|
|
10
|
+
icon?: string;
|
|
11
|
+
challenge?: { id: string; name: string; order?: number };
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type PaginationMeta = {
|
|
16
|
+
page: number;
|
|
17
|
+
pageSize: number;
|
|
18
|
+
total: number;
|
|
19
|
+
pageCount: number;
|
|
20
|
+
hasNextPage: boolean;
|
|
21
|
+
hasPreviousPage: boolean;
|
|
22
|
+
[key: string]: unknown;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type UseBadgesOptions = {
|
|
26
|
+
/** Si omis, utilise `LocalContext.selectedCityId`. */
|
|
27
|
+
cityId?: string | null;
|
|
28
|
+
page?: number;
|
|
29
|
+
pageSize?: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type UseBadgesResult = {
|
|
33
|
+
cityId: string | null;
|
|
34
|
+
badges: Badge[];
|
|
35
|
+
meta: PaginationMeta | null;
|
|
36
|
+
isLoading: boolean;
|
|
37
|
+
error: string | null;
|
|
38
|
+
refetch: () => Promise<void>;
|
|
39
|
+
getBadgeById: (id: string) => Badge | undefined;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export function useBadges(options: UseBadgesOptions = {}): UseBadgesResult {
|
|
43
|
+
const { selectedCityId } = useLocal();
|
|
44
|
+
|
|
45
|
+
const resolvedCityId = options.cityId ?? selectedCityId ?? null;
|
|
46
|
+
const page = options.page ?? 1;
|
|
47
|
+
const pageSize = options.pageSize ?? 100;
|
|
48
|
+
|
|
49
|
+
const client = useWastoClient();
|
|
50
|
+
|
|
51
|
+
const [badges, setBadges] = useState<Badge[]>([]);
|
|
52
|
+
const [meta, setMeta] = useState<PaginationMeta | null>(null);
|
|
53
|
+
const [isLoading, setIsLoading] = useState<boolean>(false);
|
|
54
|
+
const [error, setError] = useState<string | null>(null);
|
|
55
|
+
|
|
56
|
+
const fetchBadges = useCallback(async () => {
|
|
57
|
+
if (!resolvedCityId) {
|
|
58
|
+
setBadges([]);
|
|
59
|
+
setMeta(null);
|
|
60
|
+
setIsLoading(false);
|
|
61
|
+
setError(null);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
setIsLoading(true);
|
|
67
|
+
setError(null);
|
|
68
|
+
const res = await client.public.cities.getBadges(resolvedCityId, { page, pageSize });
|
|
69
|
+
const data = (res as { data?: unknown })?.data;
|
|
70
|
+
const meta = (res as { meta?: unknown })?.meta;
|
|
71
|
+
setBadges(Array.isArray(data) ? (data as Badge[]) : []);
|
|
72
|
+
setMeta(meta && typeof meta === "object" ? (meta as PaginationMeta) : null);
|
|
73
|
+
} catch (err) {
|
|
74
|
+
const message =
|
|
75
|
+
err instanceof Error ? err.message : typeof err === "string" ? err : "Impossible de charger les badges";
|
|
76
|
+
setError(message);
|
|
77
|
+
setBadges([]);
|
|
78
|
+
setMeta(null);
|
|
79
|
+
} finally {
|
|
80
|
+
setIsLoading(false);
|
|
81
|
+
}
|
|
82
|
+
}, [client.public.cities, page, pageSize, resolvedCityId]);
|
|
83
|
+
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
void fetchBadges();
|
|
86
|
+
}, [fetchBadges]);
|
|
87
|
+
|
|
88
|
+
const getBadgeById = useCallback((id: string) => badges.find((b) => b.id === id), [badges]);
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
cityId: resolvedCityId,
|
|
92
|
+
badges,
|
|
93
|
+
meta,
|
|
94
|
+
isLoading,
|
|
95
|
+
error,
|
|
96
|
+
refetch: fetchBadges,
|
|
97
|
+
getBadgeById,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
@@ -0,0 +1,105 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
3
|
+
import { useLocal } from "../contexts/LocalContext";
|
|
4
|
+
import { useWasto } from "../contexts/WastoContext";
|
|
5
|
+
import { useWastoClient } from "./useWastoClient";
|
|
6
|
+
import type { PublicAnnouncement } from "../contexts/AnnouncementsContext";
|
|
7
|
+
import { useAnnouncements } from "../contexts/AnnouncementsContext";
|
|
8
|
+
|
|
9
|
+
const DISMISSED_KEY = "dismissed_announcements";
|
|
10
|
+
const PAGE_SIZE = 20;
|
|
11
|
+
|
|
12
|
+
export type CityAnnouncementItem = PublicAnnouncement & {
|
|
13
|
+
unread: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
async function readDismissedIds(): Promise<Set<string>> {
|
|
17
|
+
try {
|
|
18
|
+
const raw = await AsyncStorage.getItem(DISMISSED_KEY);
|
|
19
|
+
if (!raw) return new Set();
|
|
20
|
+
const parsed = JSON.parse(raw) as string[];
|
|
21
|
+
return new Set(Array.isArray(parsed) ? parsed : []);
|
|
22
|
+
} catch {
|
|
23
|
+
return new Set();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function resolveImageUrl(apiUrl: string, url?: string | null): string | null {
|
|
28
|
+
if (!url) return null;
|
|
29
|
+
if (url.startsWith("http://") || url.startsWith("https://")) return url;
|
|
30
|
+
const base = apiUrl.replace(/\/$/, "");
|
|
31
|
+
const path = url.startsWith("/") ? url : `/${url}`;
|
|
32
|
+
return `${base}${path}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function useCityAnnouncements() {
|
|
36
|
+
const { apiUrl } = useWasto();
|
|
37
|
+
const { selectedCityId, hasPlaceSelected } = useLocal();
|
|
38
|
+
const client = useWastoClient();
|
|
39
|
+
const { markAsRead: contextMarkAsRead, openAnnouncement } = useAnnouncements();
|
|
40
|
+
|
|
41
|
+
const [all, setAll] = useState<CityAnnouncementItem[]>([]);
|
|
42
|
+
const [visibleCount, setVisibleCount] = useState(PAGE_SIZE);
|
|
43
|
+
const [loading, setLoading] = useState(true);
|
|
44
|
+
const [error, setError] = useState<string | null>(null);
|
|
45
|
+
|
|
46
|
+
const reload = useCallback(async () => {
|
|
47
|
+
if (!selectedCityId || !hasPlaceSelected) {
|
|
48
|
+
setAll([]);
|
|
49
|
+
setLoading(false);
|
|
50
|
+
setError(null);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
setLoading(true);
|
|
54
|
+
setError(null);
|
|
55
|
+
try {
|
|
56
|
+
const [list, dismissed] = await Promise.all([
|
|
57
|
+
client.public.announcements.listForCity(selectedCityId),
|
|
58
|
+
readDismissedIds(),
|
|
59
|
+
]);
|
|
60
|
+
const items: CityAnnouncementItem[] = (list ?? [])
|
|
61
|
+
.filter((a) => a?.id)
|
|
62
|
+
.map((a) => ({
|
|
63
|
+
...a,
|
|
64
|
+
imageUrl: resolveImageUrl(apiUrl, a.imageUrl),
|
|
65
|
+
unread: !dismissed.has(a.id),
|
|
66
|
+
}))
|
|
67
|
+
.sort((a, b) => {
|
|
68
|
+
const ta = a.sentAt ? new Date(a.sentAt).getTime() : 0;
|
|
69
|
+
const tb = b.sentAt ? new Date(b.sentAt).getTime() : 0;
|
|
70
|
+
return tb - ta;
|
|
71
|
+
});
|
|
72
|
+
setAll(items);
|
|
73
|
+
setVisibleCount(PAGE_SIZE);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
setError(e instanceof Error ? e.message : "Impossible de charger les notifications");
|
|
76
|
+
setAll([]);
|
|
77
|
+
} finally {
|
|
78
|
+
setLoading(false);
|
|
79
|
+
}
|
|
80
|
+
}, [apiUrl, client.public.announcements, hasPlaceSelected, selectedCityId]);
|
|
81
|
+
|
|
82
|
+
useEffect(() => {
|
|
83
|
+
void reload();
|
|
84
|
+
}, [reload]);
|
|
85
|
+
|
|
86
|
+
const items = all.slice(0, visibleCount);
|
|
87
|
+
const hasMore = visibleCount < all.length;
|
|
88
|
+
|
|
89
|
+
const loadMore = useCallback(() => {
|
|
90
|
+
if (!hasMore) return;
|
|
91
|
+
setVisibleCount((n) => Math.min(n + PAGE_SIZE, all.length));
|
|
92
|
+
}, [all.length, hasMore]);
|
|
93
|
+
|
|
94
|
+
const openItem = useCallback(
|
|
95
|
+
async (item: CityAnnouncementItem) => {
|
|
96
|
+
openAnnouncement(item);
|
|
97
|
+
if (item.unread) {
|
|
98
|
+
await contextMarkAsRead(item.id);
|
|
99
|
+
setAll((prev) =>
|
|
100
|
+
prev.map((a) => (a.id === item.id ? { ...a, unread: false } : a)),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
[contextMarkAsRead, openAnnouncement],
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
items,
|
|
109
|
+
loading,
|
|
110
|
+
error,
|
|
111
|
+
hasMore,
|
|
112
|
+
loadMore,
|
|
113
|
+
reload,
|
|
114
|
+
openItem,
|
|
115
|
+
hasPlaceSelected: !!hasPlaceSelected && !!selectedCityId,
|
|
116
|
+
totalCount: all.length,
|
|
117
|
+
};
|
|
118
|
+
}
|