@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.
Files changed (94) hide show
  1. package/README.md +47 -0
  2. package/dist/index.d.mts +465 -0
  3. package/dist/index.d.ts +465 -0
  4. package/dist/index.js +10445 -0
  5. package/dist/index.mjs +10553 -0
  6. package/package.json +90 -0
  7. package/src/components/announcement-modal/AnnouncementModal.tsx +183 -0
  8. package/src/components/article/Article.tsx +22 -0
  9. package/src/components/article/ArticleCard.tsx +81 -0
  10. package/src/components/article/ArticleExcerpts.tsx +80 -0
  11. package/src/components/article/ArticleList.tsx +114 -0
  12. package/src/components/article/ArticleMarkdown.tsx +170 -0
  13. package/src/components/article/ArticleReaderModal.tsx +93 -0
  14. package/src/components/article/ArticleSingle.tsx +57 -0
  15. package/src/components/bottom-tab/BottomTab.tsx +392 -0
  16. package/src/components/button/Button.tsx +154 -0
  17. package/src/components/challenge/Badges.tsx +84 -0
  18. package/src/components/challenge/BadgesModal.tsx +166 -0
  19. package/src/components/challenge/Challenge.tsx +380 -0
  20. package/src/components/challenge/ChallengeModal.tsx +231 -0
  21. package/src/components/challenge/ChallengeVignette.tsx +249 -0
  22. package/src/components/challenge/ChallengesModal.tsx +243 -0
  23. package/src/components/challenge/QuizModal.tsx +381 -0
  24. package/src/components/challenge/ResultModal.tsx +144 -0
  25. package/src/components/chat/Chat.tsx +928 -0
  26. package/src/components/collection-calendar/CollectionCalendar.tsx +274 -0
  27. package/src/components/columns/Columns.tsx +34 -0
  28. package/src/components/consignes-modal/ConsignesModal.tsx +267 -0
  29. package/src/components/content-inset/ContentInset.tsx +33 -0
  30. package/src/components/header/Header.tsx +391 -0
  31. package/src/components/image/Image.tsx +16 -0
  32. package/src/components/image/ImageFrame.tsx +102 -0
  33. package/src/components/image/ImageMedia.tsx +31 -0
  34. package/src/components/image/ImageUrl.tsx +30 -0
  35. package/src/components/locations-map/LocationSheet.tsx +417 -0
  36. package/src/components/locations-map/LocationsMap.native.tsx +260 -0
  37. package/src/components/locations-map/LocationsMap.tsx +13 -0
  38. package/src/components/locations-map/LocationsMap.web.tsx +10 -0
  39. package/src/components/locations-map/LocationsMapShell.tsx +527 -0
  40. package/src/components/locations-map/LocationsMapView.tsx +108 -0
  41. package/src/components/locations-map/LocationsMapView.web.tsx +331 -0
  42. package/src/components/nav-button/NavButton.tsx +59 -0
  43. package/src/components/next-collection/NextCollection.tsx +621 -0
  44. package/src/components/notifications/NotificationsScreen.tsx +247 -0
  45. package/src/components/page/Page.tsx +64 -0
  46. package/src/components/parapraph/Paragraph.tsx +67 -0
  47. package/src/components/profile/ProfileScreen.tsx +236 -0
  48. package/src/components/recycling-info/RecyclingInfo.tsx +189 -0
  49. package/src/components/safe-area/OptionalSafeAreaProvider.tsx +17 -0
  50. package/src/components/search/Search.tsx +320 -0
  51. package/src/components/select-street-modal/SelectStreetModal.tsx +475 -0
  52. package/src/components/title/Title.tsx +100 -0
  53. package/src/components/vision/Vision.tsx +447 -0
  54. package/src/components/vision/VisionActionCard.tsx +117 -0
  55. package/src/components/vision/VisionCapture.tsx +30 -0
  56. package/src/components/vision/VisionTapOverlay.tsx +122 -0
  57. package/src/contexts/AnnouncementsContext.tsx +171 -0
  58. package/src/contexts/ChallengesContext.tsx +182 -0
  59. package/src/contexts/LocalContext.tsx +227 -0
  60. package/src/contexts/ThemeContext.tsx +37 -0
  61. package/src/contexts/WastoContext.tsx +127 -0
  62. package/src/helpers/blogPost.ts +72 -0
  63. package/src/helpers/generateQuizQuestions.ts +247 -0
  64. package/src/helpers/getContrastColor.ts +19 -0
  65. package/src/helpers/haptics.ts +20 -0
  66. package/src/helpers/hoursSchedule.ts +230 -0
  67. package/src/helpers/htmlContentToMarkdown.ts +114 -0
  68. package/src/helpers/layout.ts +54 -0
  69. package/src/helpers/loadLinearGradient.ts +22 -0
  70. package/src/helpers/maquetteTheme.ts +76 -0
  71. package/src/helpers/markdownUnderlinePlugin.ts +32 -0
  72. package/src/helpers/normalizeWasteTypeRecyclingInfo.ts +80 -0
  73. package/src/helpers/publicMediaUrl.ts +8 -0
  74. package/src/helpers/resolveApiAssetUrl.ts +21 -0
  75. package/src/helpers/systemRoutes.ts +7 -0
  76. package/src/helpers/themeStyles.ts +114 -0
  77. package/src/helpers/useSafeAreaPadding.ts +14 -0
  78. package/src/hooks/useBadges.ts +100 -0
  79. package/src/hooks/useBlogPosts.ts +105 -0
  80. package/src/hooks/useChallenges.ts +110 -0
  81. package/src/hooks/useCities.ts +56 -0
  82. package/src/hooks/useCityAnnouncements.ts +118 -0
  83. package/src/hooks/useCollectionCalendar.ts +421 -0
  84. package/src/hooks/useColumns.ts +20 -0
  85. package/src/hooks/useConsignes.ts +62 -0
  86. package/src/hooks/useLocations.ts +248 -0
  87. package/src/hooks/useNextCollection.ts +238 -0
  88. package/src/hooks/useRecyclingInfo.ts +90 -0
  89. package/src/hooks/useWastoClient.ts +16 -0
  90. package/src/index.ts +142 -0
  91. package/src/navigation/NavigationContext.tsx +56 -0
  92. package/src/sentry/initSentry.ts +120 -0
  93. package/src/types/Theme.ts +72 -0
  94. package/src/types/css-modules.d.ts +1 -0
@@ -0,0 +1,37 @@
1
+ import {
2
+ createContext,
3
+ PropsWithChildren,
4
+ useContext,
5
+ useMemo,
6
+ type ReactElement,
7
+ } from "react";
8
+ import { resolveTheme } from "../helpers/themeStyles";
9
+ import type { ResolvedTheme, Theme } from "../types/Theme";
10
+
11
+ export type ThemeContextType = {
12
+ theme: ResolvedTheme;
13
+ };
14
+
15
+ export const ThemeContext = createContext<ThemeContextType | undefined>(undefined);
16
+
17
+ export type ThemePoviderProps = PropsWithChildren & {
18
+ theme: Theme;
19
+ };
20
+
21
+ export function ThemeProvider(props: ThemePoviderProps): ReactElement {
22
+ const theme = useMemo(() => resolveTheme(props.theme), [props.theme]);
23
+
24
+ return <ThemeContext
25
+ value={{ theme }}
26
+ >
27
+ {props.children}
28
+ </ThemeContext>
29
+ }
30
+
31
+ export function useTheme() {
32
+ const context = useContext(ThemeContext);
33
+ if (!context) {
34
+ throw new Error("useTheme must be used within a ThemeProvider.");
35
+ }
36
+ return context;
37
+ }
@@ -0,0 +1,127 @@
1
+ import {
2
+ createContext,
3
+ PropsWithChildren,
4
+ useContext,
5
+ useEffect,
6
+ useMemo,
7
+ type ReactNode,
8
+ } from "react";
9
+ import type { WastoRequestErrorContext } from "@wasto/sdk";
10
+ import { Theme } from "../types/Theme";
11
+ import { ThemeProvider } from "./ThemeContext";
12
+ import { LocalProvider } from "./LocalContext";
13
+ import { ChallengesProvider } from "./ChallengesContext";
14
+ import { AnnouncementsProvider } from "./AnnouncementsContext";
15
+ import SelectStreetModal from "../components/select-street-modal/SelectStreetModal";
16
+ import { AnnouncementModal } from "../components/announcement-modal/AnnouncementModal";
17
+ import { OptionalSafeAreaProvider } from "../components/safe-area/OptionalSafeAreaProvider";
18
+ import {
19
+ initWastoSentry,
20
+ reportError as sentryReportError,
21
+ type WastoErrorContext,
22
+ type WastoSentryConfig,
23
+ } from "../sentry/initSentry";
24
+
25
+ export type WastoContextType = {
26
+ apiUrl: string;
27
+ appId: string;
28
+ cityGroupId: string | null;
29
+ /**
30
+ * Studio / iframe preview — no anonymous device registration or challenge writes.
31
+ */
32
+ previewMode: boolean;
33
+ reportError: (error: unknown, context?: WastoErrorContext) => void;
34
+ onRequestError?: (error: Error, context: WastoRequestErrorContext) => void;
35
+ };
36
+
37
+ export const WastoContext = createContext<WastoContextType | undefined>(undefined);
38
+
39
+ export type WastoProviderProps = PropsWithChildren & {
40
+ apiUrl: string;
41
+ appId: string;
42
+ theme: Theme;
43
+ /** City group scoping street search / multi-city apps. */
44
+ cityGroupId?: string | null;
45
+ /** GitLab Error Tracking / Sentry config (DSN from env). */
46
+ sentry?: WastoSentryConfig;
47
+ /**
48
+ * When true (Studio preview): LocalContext skips analytics.track / device id.
49
+ */
50
+ previewMode?: boolean;
51
+ };
52
+
53
+ export function WastoProvider(props: WastoProviderProps): ReactNode {
54
+ const apiUrl = useMemo(() => props.apiUrl, [props.apiUrl]);
55
+ const appId = useMemo(() => props.appId, [props.appId]);
56
+ const cityGroupId = useMemo(
57
+ () => props.cityGroupId?.trim() || null,
58
+ [props.cityGroupId],
59
+ );
60
+ const previewMode = Boolean(props.previewMode);
61
+ const sentryConfig = props.sentry;
62
+
63
+ useEffect(() => {
64
+ if (!sentryConfig) return;
65
+ initWastoSentry({
66
+ ...sentryConfig,
67
+ appId: sentryConfig.appId ?? appId,
68
+ });
69
+ }, [appId, sentryConfig]);
70
+
71
+ const reportError = useMemo(
72
+ () => (error: unknown, context?: WastoErrorContext) => {
73
+ sentryReportError(error, context);
74
+ },
75
+ [],
76
+ );
77
+
78
+ const onRequestError = useMemo(
79
+ () => (error: Error, ctx: WastoRequestErrorContext) => {
80
+ reportError(error, {
81
+ source: "api",
82
+ method: ctx.method,
83
+ path: ctx.path,
84
+ status: ctx.status,
85
+ });
86
+ },
87
+ [reportError],
88
+ );
89
+
90
+ const value = useMemo<WastoContextType>(
91
+ () => ({
92
+ apiUrl,
93
+ appId,
94
+ cityGroupId,
95
+ previewMode,
96
+ reportError,
97
+ onRequestError,
98
+ }),
99
+ [apiUrl, appId, cityGroupId, previewMode, reportError, onRequestError],
100
+ );
101
+
102
+ return (
103
+ <WastoContext value={value}>
104
+ <OptionalSafeAreaProvider>
105
+ <LocalProvider previewMode={previewMode}>
106
+ <ChallengesProvider>
107
+ <AnnouncementsProvider apiUrl={apiUrl}>
108
+ <ThemeProvider theme={props.theme}>
109
+ {props.children}
110
+ <SelectStreetModal />
111
+ <AnnouncementModal />
112
+ </ThemeProvider>
113
+ </AnnouncementsProvider>
114
+ </ChallengesProvider>
115
+ </LocalProvider>
116
+ </OptionalSafeAreaProvider>
117
+ </WastoContext>
118
+ );
119
+ }
120
+
121
+ export function useWasto() {
122
+ const context = useContext(WastoContext);
123
+ if (!context) {
124
+ throw new Error("useContext must be used within a WastoProvider.");
125
+ }
126
+ return context;
127
+ }
@@ -0,0 +1,72 @@
1
+ import { htmlContentToMarkdown } from "./htmlContentToMarkdown";
2
+
3
+ export type BlogPost = {
4
+ id: string;
5
+ title: string;
6
+ content: string;
7
+ publishedAt?: string;
8
+ createdAt?: string;
9
+ coverMediaId?: string | null;
10
+ };
11
+
12
+ function asRecord(raw: unknown): Record<string, unknown> | null {
13
+ if (raw == null || typeof raw !== "object") return null;
14
+ const root = raw as Record<string, unknown>;
15
+ if (root.data != null && typeof root.data === "object") {
16
+ return root.data as Record<string, unknown>;
17
+ }
18
+ return root;
19
+ }
20
+
21
+ function pickStr(o: Record<string, unknown>, camel: string, snake?: string): string | undefined {
22
+ const v = o[camel] ?? (snake ? o[snake] : undefined);
23
+ return typeof v === "string" ? v : undefined;
24
+ }
25
+
26
+ export function normalizeBlogPost(raw: unknown): BlogPost | null {
27
+ const o = asRecord(raw);
28
+ if (!o) return null;
29
+
30
+ const idRaw = o.id;
31
+ const id = typeof idRaw === "string" ? idRaw : idRaw != null ? String(idRaw) : "";
32
+ if (!id) return null;
33
+
34
+ const coverFromRelation =
35
+ o.coverMedia != null && typeof o.coverMedia === "object"
36
+ ? (o.coverMedia as { id?: unknown }).id
37
+ : undefined;
38
+
39
+ return {
40
+ id,
41
+ title: pickStr(o, "title") || "Sans titre",
42
+ content: pickStr(o, "content") || "",
43
+ publishedAt: pickStr(o, "publishedAt", "published_at"),
44
+ createdAt: pickStr(o, "createdAt", "created_at"),
45
+ coverMediaId:
46
+ pickStr(o, "coverMediaId", "cover_media_id") ??
47
+ (typeof coverFromRelation === "string" ? coverFromRelation : null),
48
+ };
49
+ }
50
+
51
+ export function stripToExcerpt(content: string, maxLen = 180): string {
52
+ const fromHtml = htmlContentToMarkdown(content);
53
+ const plain = fromHtml
54
+ .replace(/```[\s\S]*?```/g, " ")
55
+ .replace(/`[^`]*`/g, " ")
56
+ .replace(/!\[[^\]]*]\([^)]*\)/g, " ")
57
+ .replace(/\[[^\]]*]\([^)]*\)/g, " ")
58
+ .replace(/\+\+([^+]+)\+\+/g, "$1")
59
+ .replace(/[#>*_~\-|]+/g, " ")
60
+ .replace(/\s+/g, " ")
61
+ .trim();
62
+ if (plain.length <= maxLen) return plain;
63
+ return `${plain.slice(0, maxLen).trim()}…`;
64
+ }
65
+
66
+ export function formatArticleDate(value?: string): string | undefined {
67
+ if (!value) return undefined;
68
+ const slice = value.length >= 10 ? value.slice(0, 10) : value;
69
+ const date = new Date(slice);
70
+ if (Number.isNaN(date.getTime())) return slice;
71
+ return date.toLocaleDateString("fr-FR", { day: "numeric", month: "long", year: "numeric" });
72
+ }
@@ -0,0 +1,247 @@
1
+ export type QuizQuestionKind = "waste_bin" | "ab_image" | "mcq";
2
+
3
+ export type QuizAnswerOption = {
4
+ id: string;
5
+ text: string;
6
+ wasteTypeId?: string;
7
+ imageUrl?: string;
8
+ type: "correct" | "wrong";
9
+ };
10
+
11
+ export type GeneratedQuizQuestion = {
12
+ kind: QuizQuestionKind;
13
+ id: string;
14
+ question: string;
15
+ correctAnswer: string;
16
+ /** Identifiant de la bonne option (wasteTypeId ou choice id). */
17
+ correctOptionId: string;
18
+ /** @deprecated Alias de correctOptionId pour waste_bin. */
19
+ correctWasteTypeId: string;
20
+ answers: QuizAnswerOption[];
21
+ material?: string;
22
+ imageUrl?: string;
23
+ /** Explication affichée après validation de la réponse. */
24
+ explanation?: string;
25
+ };
26
+
27
+ type QuizQuestionSource = {
28
+ id: string;
29
+ question?: string;
30
+ correctWasteTypeId?: string | null;
31
+ order?: number;
32
+ material?: string;
33
+ imageUrl?: string | null;
34
+ kind?: string | null;
35
+ options?: unknown;
36
+ explanationText?: string | null;
37
+ explanationDetailId?: string | null;
38
+ explanationDetail?: {
39
+ material?: string | null;
40
+ description?: string | null;
41
+ instructions?: string | null;
42
+ } | null;
43
+ };
44
+
45
+ export function resolveQuizExplanation(source: {
46
+ explanationText?: string | null;
47
+ explanationDetail?: {
48
+ material?: string | null;
49
+ description?: string | null;
50
+ instructions?: string | null;
51
+ } | null;
52
+ }): string | undefined {
53
+ const free = typeof source.explanationText === "string"
54
+ ? source.explanationText.trim()
55
+ : "";
56
+ if (free) return free;
57
+ const detail = source.explanationDetail;
58
+ if (!detail) return undefined;
59
+ const fromDetail = (detail.instructions || detail.description || "").trim();
60
+ if (!fromDetail) return undefined;
61
+ const material = detail.material?.trim();
62
+ return material ? `${material} : ${fromDetail}` : fromDetail;
63
+ }
64
+
65
+ function asAbImageOptions(options: unknown): {
66
+ choices: Array<{ id: string; imageUrl: string; label?: string }>;
67
+ correctChoiceId: string;
68
+ } | null {
69
+ if (!options || typeof options !== "object") return null;
70
+ const raw = options as {
71
+ choices?: Array<{ id?: string; imageUrl?: string; label?: string }>;
72
+ correctChoiceId?: string;
73
+ };
74
+ const choices = Array.isArray(raw.choices)
75
+ ? raw.choices
76
+ .map((c, i) => ({
77
+ id: String(c?.id ?? (i === 0 ? "a" : "b")),
78
+ imageUrl: String(c?.imageUrl ?? "").trim(),
79
+ label: c?.label ? String(c.label) : undefined,
80
+ }))
81
+ .filter((c) => c.imageUrl)
82
+ : [];
83
+ if (choices.length < 2) return null;
84
+ const correctChoiceId = String(raw.correctChoiceId ?? "").trim();
85
+ if (!choices.some((c) => c.id === correctChoiceId)) return null;
86
+ return { choices: choices.slice(0, 2), correctChoiceId };
87
+ }
88
+
89
+ function asMcqOptions(options: unknown): {
90
+ choices: Array<{ id: string; text: string }>;
91
+ correctChoiceId: string;
92
+ } | null {
93
+ if (!options || typeof options !== "object") return null;
94
+ const raw = options as {
95
+ choices?: Array<{ id?: string; text?: string }>;
96
+ correctChoiceId?: string;
97
+ };
98
+ const choices = Array.isArray(raw.choices)
99
+ ? raw.choices
100
+ .map((c, i) => ({
101
+ id: String(c?.id ?? `c${i}`),
102
+ text: String(c?.text ?? "").trim(),
103
+ }))
104
+ .filter((c) => c.text)
105
+ : [];
106
+ if (choices.length < 2) return null;
107
+ const correctChoiceId = String(raw.correctChoiceId ?? "").trim();
108
+ if (!choices.some((c) => c.id === correctChoiceId)) return null;
109
+ return { choices, correctChoiceId };
110
+ }
111
+
112
+ export function generateQuizQuestions(
113
+ wasteTypes: Array<{ id: string; name?: string }>,
114
+ serie?: {
115
+ id: string;
116
+ quizQuestions?: QuizQuestionSource[];
117
+ } | null
118
+ ): GeneratedQuizQuestion[] {
119
+ const questionsToUse = [...(serie?.quizQuestions ?? [])].sort(
120
+ (a, b) => (a.order ?? 0) - (b.order ?? 0)
121
+ );
122
+
123
+ if (questionsToUse.length === 0) {
124
+ return [];
125
+ }
126
+
127
+ return questionsToUse
128
+ .map((quizItem): GeneratedQuizQuestion | null => {
129
+ const kind = (quizItem.kind as QuizQuestionKind) || "waste_bin";
130
+
131
+ if (kind === "ab_image") {
132
+ const opts = asAbImageOptions(quizItem.options);
133
+ if (!opts) return null;
134
+ const correct = opts.choices.find((c) => c.id === opts.correctChoiceId);
135
+ return {
136
+ kind: "ab_image",
137
+ id: quizItem.id,
138
+ question: quizItem.question || "Quelle image est la bonne ?",
139
+ correctAnswer: correct?.label || "Image correcte",
140
+ correctOptionId: opts.correctChoiceId,
141
+ correctWasteTypeId: opts.correctChoiceId,
142
+ answers: opts.choices.map((choice) => ({
143
+ id: choice.id,
144
+ text: choice.label || (choice.id === "a" ? "A" : "B"),
145
+ imageUrl: choice.imageUrl,
146
+ type: choice.id === opts.correctChoiceId ? "correct" : "wrong",
147
+ })),
148
+ material: quizItem.material,
149
+ explanation: resolveQuizExplanation(quizItem),
150
+ };
151
+ }
152
+
153
+ if (kind === "mcq") {
154
+ const opts = asMcqOptions(quizItem.options);
155
+ if (!opts) return null;
156
+ const correct = opts.choices.find((c) => c.id === opts.correctChoiceId);
157
+ const shuffled = [...opts.choices].sort(() => Math.random() - 0.5);
158
+ return {
159
+ kind: "mcq",
160
+ id: quizItem.id,
161
+ question: quizItem.question || "Choisis la bonne réponse",
162
+ correctAnswer: correct?.text || "Réponse correcte",
163
+ correctOptionId: opts.correctChoiceId,
164
+ correctWasteTypeId: opts.correctChoiceId,
165
+ answers: shuffled.map((choice) => ({
166
+ id: choice.id,
167
+ text: choice.text,
168
+ type: choice.id === opts.correctChoiceId ? "correct" : "wrong",
169
+ })),
170
+ material: quizItem.material,
171
+ imageUrl:
172
+ typeof quizItem.imageUrl === "string" && quizItem.imageUrl.trim()
173
+ ? quizItem.imageUrl.trim()
174
+ : undefined,
175
+ explanation: resolveQuizExplanation(quizItem),
176
+ };
177
+ }
178
+
179
+ // waste_bin (legacy default)
180
+ if (wasteTypes.length === 0) return null;
181
+ const correctWasteType = wasteTypes.find(
182
+ (wt) => wt.id === quizItem.correctWasteTypeId
183
+ );
184
+ const otherWasteTypes = wasteTypes.filter(
185
+ (wt) => wt.id !== quizItem.correctWasteTypeId
186
+ );
187
+ const correctId = quizItem.correctWasteTypeId || "";
188
+
189
+ const answers: QuizAnswerOption[] = [
190
+ {
191
+ id: correctId,
192
+ text: correctWasteType?.name || "Poubelle incorrecte",
193
+ wasteTypeId: correctId,
194
+ type: "correct",
195
+ },
196
+ {
197
+ id: otherWasteTypes[0]?.id || "wrong-0",
198
+ text: otherWasteTypes[0]?.name || "Poubelle incorrecte",
199
+ wasteTypeId: otherWasteTypes[0]?.id || "",
200
+ type: "wrong",
201
+ },
202
+ {
203
+ id: otherWasteTypes[1]?.id || "wrong-1",
204
+ text: otherWasteTypes[1]?.name || "Poubelle incorrecte",
205
+ wasteTypeId: otherWasteTypes[1]?.id || "",
206
+ type: "wrong",
207
+ },
208
+ ];
209
+
210
+ const shuffledAnswers = [...answers].sort(() => Math.random() - 0.5);
211
+
212
+ return {
213
+ kind: "waste_bin",
214
+ id: quizItem.id,
215
+ question: quizItem.question || "Où jeter cet objet ?",
216
+ correctAnswer: correctWasteType?.name || "Poubelle",
217
+ correctOptionId: correctId,
218
+ correctWasteTypeId: correctId,
219
+ answers: shuffledAnswers,
220
+ material: quizItem.material,
221
+ imageUrl:
222
+ typeof quizItem.imageUrl === "string" && quizItem.imageUrl.trim()
223
+ ? quizItem.imageUrl.trim()
224
+ : undefined,
225
+ explanation: resolveQuizExplanation(quizItem),
226
+ };
227
+ })
228
+ .filter((q): q is GeneratedQuizQuestion => q !== null);
229
+ }
230
+
231
+ export function quizStarCount(correct: number, total: number): number {
232
+ if (total === 0) return 0;
233
+ const percentage = (correct / total) * 100;
234
+ if (percentage === 100) return 5;
235
+ if (percentage >= 80) return 4;
236
+ if (percentage >= 60) return 3;
237
+ if (percentage >= 40) return 2;
238
+ return 1;
239
+ }
240
+
241
+ export function quizResultMessage(correct: number, total: number): string {
242
+ if (total === 0) return "Aucune question pour cette série.";
243
+ const percentage = (correct / total) * 100;
244
+ if (percentage >= 80) return "Excellent ! Tu es un vrai champion du tri !";
245
+ if (percentage >= 50) return "Super, encore un petit effort !";
246
+ return "Continue d'apprendre, tu vas y arriver !";
247
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Détermine si le texte doit être noir ou blanc en fonction de la couleur de fond
3
+ * @param hex Color au format #RRGGBB ou #RGB
4
+ * @returns '#000000' ou '#FFFFFF'
5
+ */
6
+ export function getContrastColor(hex: string): '#000000' | '#FFFFFF' {
7
+ let cleanHex = hex.replace('#', '');
8
+ if (cleanHex.length === 3) {
9
+ cleanHex = cleanHex.split('').map(char => char + char).join('');
10
+ }
11
+
12
+ const r = parseInt(cleanHex.substring(0, 2), 16);
13
+ const g = parseInt(cleanHex.substring(2, 4), 16);
14
+ const b = parseInt(cleanHex.substring(4, 6), 16);
15
+
16
+ const yiq = (r * 299 + g * 587 + b * 114) / 1000;
17
+
18
+ return yiq >= 128 ? '#000000' : '#FFFFFF';
19
+ }
@@ -0,0 +1,20 @@
1
+ import * as Haptics from "expo-haptics";
2
+ import { Platform } from "react-native";
3
+
4
+ export async function hapticSelection() {
5
+ if (Platform.OS === "web") return;
6
+ try {
7
+ await Haptics.selectionAsync();
8
+ } catch {
9
+ // Haptics unavailable on simulator / unsupported devices
10
+ }
11
+ }
12
+
13
+ export async function hapticLight() {
14
+ if (Platform.OS === "web") return;
15
+ try {
16
+ await Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
17
+ } catch {
18
+ // ignore
19
+ }
20
+ }