@micha.bigler/ui-core-micha 2.10.1 → 2.12.0
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/WORK_ORDERS.md +3 -1
- package/dist/auth/AuthContext.js +1 -0
- package/dist/auth/authApi.js +3 -1
- package/dist/components/TurnstileWidget.js +84 -0
- package/dist/i18n/notificationsTranslations.js +3 -0
- package/dist/index.js +3 -0
- package/dist/notifications/NotificationBell.js +29 -0
- package/dist/notifications/NotificationsProvider.js +194 -0
- package/dist/notifications/feedApi.js +15 -0
- package/dist/pages/SignUpPage.js +22 -2
- package/package.json +1 -1
- package/src/auth/AuthContext.jsx +1 -0
- package/src/auth/authApi.jsx +2 -0
- package/src/components/TurnstileWidget.jsx +99 -0
- package/src/i18n/notificationsTranslations.ts +3 -0
- package/src/index.js +3 -0
- package/src/notifications/NotificationBell.jsx +84 -0
- package/src/notifications/NotificationsProvider.jsx +232 -0
- package/src/notifications/feedApi.js +18 -0
- package/src/pages/SignUpPage.jsx +38 -2
- package/tests/NotificationBell.test.jsx +60 -0
- package/tests/NotificationsProvider.test.jsx +197 -0
- package/tests/SignUpPage.test.jsx +138 -0
- package/tests/authApiRegistration.test.js +31 -0
- package/tests/feedApi.test.js +46 -0
- package/tests/notificationsExports.test.js +29 -0
package/WORK_ORDERS.md
CHANGED
|
@@ -10,6 +10,7 @@ rules are defined centrally in `webapps/AGENTS.md` → "Work-Order Register".
|
|
|
10
10
|
|---|---|
|
|
11
11
|
| `ONB-*` | Onboarding wizard (steps, conditions, persistence) |
|
|
12
12
|
| `PERF-*` | Performance improvements |
|
|
13
|
+
| `NOTIF-*` | Shared notifications platform (dcm register is canonical; this repo's rows are its ucm-side WOs) |
|
|
13
14
|
|
|
14
15
|
Introduce a new prefix when none fits and add it here. New WOs always get a
|
|
15
16
|
prefixed ID; never reuse a bare flat number across workstreams.
|
|
@@ -19,4 +20,5 @@ prefixed ID; never reuse a bare flat number across workstreams.
|
|
|
19
20
|
| ID | Titel | Beschreibung | Datum | Status | Commit(s) | Notiz |
|
|
20
21
|
|---|---|---|---|---|---|---|
|
|
21
22
|
| ONB-1 | Per-app configurable notifications onboarding step | New `browserPush` prop `{nagUntil, showOnce}` on `OnboardingProvider`; parameterizes the `browser_push` descriptor's condition (default changes from implicit "all-channels" to "any-channel", stopping the over-nag); `showOnce` via a persisted `onboarding_seen` set (frozen-at-mount ref, no mid-session flicker) | 2026-07-17 | done | 1ae7c20 | Default behavior change affects cockpit and all consumers on their next ucm bump. jg-ferien companion WO pins the new version with `browserPush={{nagUntil: 'any-channel'}}` explicit (matches new default, but pinned explicitly per the WO). |
|
|
22
|
-
| PERF-3B1 | Parallel auth bootstrap after CSRF | Starts auth-methods and current-user concurrently once CSRF is available, while preserving error handling and loading semantics. | 2026-07-19 | done |
|
|
23
|
+
| PERF-3B1 | Parallel auth bootstrap after CSRF | Starts auth-methods and current-user concurrently once CSRF is available, while preserving error handling and loading semantics. | 2026-07-19 | done | ee89329 | Independent `reviewer` + `sec_reviewer` passes both clean (no findings); one P3 test-coverage gap from the reviewer (missing mirror case: auth-methods rejects, current-user succeeds) closed with an added regression test — 45/45 tests green. Published as 2.10.1 (patch, no interface change) and pinned in jg-ferien alongside its PERF-3A companion WO. |
|
|
24
|
+
| NOTIF-6 | NotificationsProvider + bell on canonical dcm feed/* API | Single-WS-owner `NotificationsProvider`/`useNotifications` (initial REST seed via `feedApi.js` + one `/ws/notifications/` socket, reconnect w/ backoff) + generic MUI `NotificationBell`; additive exports only | 2026-07-27 | done | 6c63fb9 | Codex-built direct (no nesting), independent `reviewer`: single-WS-owner invariant traced clean (no way to break it — `authenticated`/`replaceNotifications`/`refresh` all referentially stable across unrelated re-renders, re-render test is genuine not a no-op). R1 (P2, real bug): the `notification.status` WS handler patched item flags but never adjusted `unreadCount`, so the badge went stale after a cross-tab/cross-device mark — the test had locked in the stale count as expected behavior. Fixed: delta computed from before/after unread state on the WS patch path, mirroring `mark()`'s own logic; test corrected + a new cross-tab regression test added. 57 passing, build clean. No new npm dependency. Full history + backend contract: dcm's `WORK_ORDERS.md`/`docs/design/notifications-platform.md`. Publishes ucm 2.11.0 |
|
package/dist/auth/AuthContext.js
CHANGED
package/dist/auth/authApi.js
CHANGED
|
@@ -341,7 +341,7 @@ export async function sendAdminInvite(email) {
|
|
|
341
341
|
throw normaliseApiError(error, 'Auth.INVITE_FAILED');
|
|
342
342
|
}
|
|
343
343
|
}
|
|
344
|
-
export async function submitRegistrationRequest({ email, mode, accessCode, registrationContextToken, registrationContext, }) {
|
|
344
|
+
export async function submitRegistrationRequest({ email, mode, accessCode, registrationContextToken, registrationContext, turnstileToken, }) {
|
|
345
345
|
const payload = { email, mode };
|
|
346
346
|
if (accessCode)
|
|
347
347
|
payload.access_code = accessCode;
|
|
@@ -351,6 +351,8 @@ export async function submitRegistrationRequest({ email, mode, accessCode, regis
|
|
|
351
351
|
if (registrationContext) {
|
|
352
352
|
payload.registration_context = registrationContext;
|
|
353
353
|
}
|
|
354
|
+
if (turnstileToken)
|
|
355
|
+
payload.turnstile_token = turnstileToken;
|
|
354
356
|
try {
|
|
355
357
|
const res = await apiClient.post(`${USERS_BASE}/register-request/`, payload);
|
|
356
358
|
return res.data;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, } from 'react';
|
|
3
|
+
const TURNSTILE_SCRIPT_URL = 'https://challenges.cloudflare.com/turnstile/v0/api.js';
|
|
4
|
+
const TURNSTILE_SCRIPT_SELECTOR = 'script[data-turnstile-api="true"]';
|
|
5
|
+
let turnstileScriptPromise;
|
|
6
|
+
function loadTurnstileScript() {
|
|
7
|
+
var _a;
|
|
8
|
+
if ((_a = window.turnstile) === null || _a === void 0 ? void 0 : _a.render) {
|
|
9
|
+
return Promise.resolve(window.turnstile);
|
|
10
|
+
}
|
|
11
|
+
if (!turnstileScriptPromise) {
|
|
12
|
+
turnstileScriptPromise = new Promise((resolve, reject) => {
|
|
13
|
+
let script = document.querySelector(TURNSTILE_SCRIPT_SELECTOR);
|
|
14
|
+
if (!script) {
|
|
15
|
+
script = document.createElement('script');
|
|
16
|
+
script.src = TURNSTILE_SCRIPT_URL;
|
|
17
|
+
script.async = true;
|
|
18
|
+
script.defer = true;
|
|
19
|
+
script.dataset.turnstileApi = 'true';
|
|
20
|
+
document.head.appendChild(script);
|
|
21
|
+
}
|
|
22
|
+
script.addEventListener('load', () => {
|
|
23
|
+
var _a;
|
|
24
|
+
if ((_a = window.turnstile) === null || _a === void 0 ? void 0 : _a.render) {
|
|
25
|
+
resolve(window.turnstile);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
reject(new Error('Turnstile did not initialize.'));
|
|
29
|
+
}
|
|
30
|
+
}, { once: true });
|
|
31
|
+
script.addEventListener('error', () => {
|
|
32
|
+
reject(new Error('Turnstile could not be loaded.'));
|
|
33
|
+
}, { once: true });
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return turnstileScriptPromise;
|
|
37
|
+
}
|
|
38
|
+
export const TurnstileWidget = forwardRef(function TurnstileWidget({ siteKey, onToken }, ref) {
|
|
39
|
+
const containerRef = useRef(null);
|
|
40
|
+
const widgetIdRef = useRef(null);
|
|
41
|
+
const onTokenRef = useRef(onToken);
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
onTokenRef.current = onToken;
|
|
44
|
+
}, [onToken]);
|
|
45
|
+
const reset = useCallback(() => {
|
|
46
|
+
var _a;
|
|
47
|
+
if (widgetIdRef.current !== null && ((_a = window.turnstile) === null || _a === void 0 ? void 0 : _a.reset)) {
|
|
48
|
+
window.turnstile.reset(widgetIdRef.current);
|
|
49
|
+
}
|
|
50
|
+
onTokenRef.current('');
|
|
51
|
+
}, []);
|
|
52
|
+
useImperativeHandle(ref, () => ({ reset }), [reset]);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
if (!siteKey)
|
|
55
|
+
return undefined;
|
|
56
|
+
let cancelled = false;
|
|
57
|
+
loadTurnstileScript()
|
|
58
|
+
.then((turnstile) => {
|
|
59
|
+
if (cancelled || !containerRef.current)
|
|
60
|
+
return;
|
|
61
|
+
widgetIdRef.current = turnstile.render(containerRef.current, {
|
|
62
|
+
sitekey: siteKey,
|
|
63
|
+
callback: (token) => onTokenRef.current(token),
|
|
64
|
+
'expired-callback': reset,
|
|
65
|
+
'error-callback': () => onTokenRef.current(''),
|
|
66
|
+
});
|
|
67
|
+
})
|
|
68
|
+
.catch(() => {
|
|
69
|
+
if (!cancelled)
|
|
70
|
+
onTokenRef.current('');
|
|
71
|
+
});
|
|
72
|
+
return () => {
|
|
73
|
+
var _a;
|
|
74
|
+
cancelled = true;
|
|
75
|
+
if (widgetIdRef.current !== null && ((_a = window.turnstile) === null || _a === void 0 ? void 0 : _a.remove)) {
|
|
76
|
+
window.turnstile.remove(widgetIdRef.current);
|
|
77
|
+
}
|
|
78
|
+
widgetIdRef.current = null;
|
|
79
|
+
};
|
|
80
|
+
}, [siteKey, reset]);
|
|
81
|
+
if (!siteKey)
|
|
82
|
+
return null;
|
|
83
|
+
return _jsx("div", { ref: containerRef, "data-testid": "turnstile-widget" });
|
|
84
|
+
});
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export const notificationsTranslations = {
|
|
2
|
+
'NotificationBell.TITLE': { de: 'Benachrichtigungen', fr: 'Notifications', en: 'Notifications', sw: 'Arifa' },
|
|
3
|
+
'NotificationBell.EMPTY': { de: 'Keine Benachrichtigungen vorhanden.', fr: 'Aucune notification.', en: 'No notifications.', sw: 'Hakuna arifa.' },
|
|
4
|
+
'NotificationBell.MARK_ALL': { de: 'Alle als gelesen markieren', fr: 'Tout marquer comme lu', en: 'Mark all as read', sw: 'Weka zote alama kuwa zimesomwa' },
|
|
2
5
|
'NotificationSettings.TITLE': { de: 'Benachrichtigungen', fr: 'Notifications', en: 'Notifications', sw: 'Arifa' },
|
|
3
6
|
'NotificationSettings.SUBTITLE': { de: 'Lege fest, wie du informiert werden möchtest.', fr: 'Choisissez comment vous souhaitez être informé.', en: 'Choose how you want to be notified.', sw: 'Chagua jinsi unavyotaka kuarifiwa.' },
|
|
4
7
|
'NotificationSettings.LOAD_ERROR': { de: 'Benachrichtigungseinstellungen konnten nicht geladen werden.', fr: 'Impossible de charger les paramètres de notification.', en: 'Notification settings could not be loaded.', sw: 'Mipangilio ya arifa haikuweza kupakiwa.' },
|
package/dist/index.js
CHANGED
|
@@ -32,6 +32,9 @@ export { authTranslations } from './i18n/authTranslations';
|
|
|
32
32
|
// --- 7. Notifications ---
|
|
33
33
|
export { NotificationSettings } from './notifications/NotificationSettings';
|
|
34
34
|
export * from './notifications/api';
|
|
35
|
+
export { NotificationsProvider, useNotifications } from './notifications/NotificationsProvider';
|
|
36
|
+
export { NotificationBell } from './notifications/NotificationBell';
|
|
37
|
+
export { getNotificationFeed, getUnreadCount, markNotifications } from './notifications/feedApi';
|
|
35
38
|
// --- 8. Onboarding ---
|
|
36
39
|
export * from './onboarding/api';
|
|
37
40
|
export { selectActiveSteps } from './onboarding/stepSelection';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import NotificationsIcon from '@mui/icons-material/Notifications';
|
|
4
|
+
import { Badge, Box, Button, IconButton, List, ListItemButton, ListItemText, Menu, Typography, } from '@mui/material';
|
|
5
|
+
import { useTranslation } from 'react-i18next';
|
|
6
|
+
import { useNotifications } from './NotificationsProvider';
|
|
7
|
+
export function NotificationBell({ resolveLink }) {
|
|
8
|
+
const { t } = useTranslation();
|
|
9
|
+
const { notifications, unreadCount, markSeen } = useNotifications();
|
|
10
|
+
const [anchorEl, setAnchorEl] = useState(null);
|
|
11
|
+
const open = Boolean(anchorEl);
|
|
12
|
+
const unreadIds = notifications
|
|
13
|
+
.filter((notification) => !notification.seen && !notification.dismissed && notification.id != null)
|
|
14
|
+
.map((notification) => notification.id);
|
|
15
|
+
const handleItemClick = (notification) => {
|
|
16
|
+
var _a;
|
|
17
|
+
if (notification.id != null)
|
|
18
|
+
markSeen([notification.id]);
|
|
19
|
+
setAnchorEl(null);
|
|
20
|
+
if (resolveLink && ((_a = notification.content) === null || _a === void 0 ? void 0 : _a.link))
|
|
21
|
+
resolveLink(notification.content.link);
|
|
22
|
+
};
|
|
23
|
+
return (_jsxs(_Fragment, { children: [_jsx(IconButton, { "aria-label": t('NotificationBell.TITLE'), color: "inherit", onClick: (event) => setAnchorEl(event.currentTarget), children: _jsx(Badge, { badgeContent: unreadCount, color: "error", max: 99, children: _jsx(NotificationsIcon, {}) }) }), _jsx(Menu, { anchorEl: anchorEl, anchorOrigin: { horizontal: 'right', vertical: 'bottom' }, onClose: () => setAnchorEl(null), open: open, transformOrigin: { horizontal: 'right', vertical: 'top' }, children: _jsxs(Box, { sx: { minWidth: 320 }, children: [_jsxs(Box, { sx: { alignItems: 'center', display: 'flex', justifyContent: 'space-between', px: 2, py: 1 }, children: [_jsx(Typography, { variant: "subtitle1", children: t('NotificationBell.TITLE') }), unreadIds.length > 0 && (_jsx(Button, { onClick: () => markSeen(unreadIds), size: "small", children: t('NotificationBell.MARK_ALL') }))] }), notifications.length === 0 ? (_jsx(Typography, { sx: { px: 2, py: 2 }, variant: "body2", children: t('NotificationBell.EMPTY') })) : (_jsx(List, { dense: true, disablePadding: true, children: notifications.map((notification) => {
|
|
24
|
+
var _a, _b, _c, _d;
|
|
25
|
+
return (_jsx(ListItemButton, { onClick: () => handleItemClick(notification), children: _jsx(ListItemText, { primary: t((_a = notification.content) === null || _a === void 0 ? void 0 : _a.title_key, ((_b = notification.content) === null || _b === void 0 ? void 0 : _b.params) || {}), secondary: ((_c = notification.content) === null || _c === void 0 ? void 0 : _c.body_key)
|
|
26
|
+
? t(notification.content.body_key, notification.content.params || {})
|
|
27
|
+
: undefined, slotProps: { primary: { fontWeight: notification.seen || notification.dismissed ? 'regular' : 'bold' } } }) }, (_d = notification.id) !== null && _d !== void 0 ? _d : notification.notification_id));
|
|
28
|
+
}) }))] }) })] }));
|
|
29
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
3
|
+
import { AuthContext } from '../auth/AuthContext';
|
|
4
|
+
import { getNotificationFeed, getUnreadCount, markNotifications } from './feedApi';
|
|
5
|
+
const INITIAL_BACKOFF_MS = 1000;
|
|
6
|
+
const MAX_BACKOFF_MS = 30000;
|
|
7
|
+
const NotificationsContext = createContext(null);
|
|
8
|
+
function normalizeNotification(notification) {
|
|
9
|
+
var _a, _b, _c;
|
|
10
|
+
return Object.assign(Object.assign({}, notification), { content: notification.content || {}, seen: Boolean((_a = notification.seen) !== null && _a !== void 0 ? _a : notification.seen_at), dismissed: Boolean((_b = notification.dismissed) !== null && _b !== void 0 ? _b : notification.dismissed_at), done: Boolean((_c = notification.done) !== null && _c !== void 0 ? _c : notification.done_at) });
|
|
11
|
+
}
|
|
12
|
+
function normalizeNotificationPush(data) {
|
|
13
|
+
var _a, _b;
|
|
14
|
+
return normalizeNotification({
|
|
15
|
+
id: (_b = (_a = data.id) !== null && _a !== void 0 ? _a : data.recipient_id) !== null && _b !== void 0 ? _b : data.notification_id,
|
|
16
|
+
notification_id: data.notification_id,
|
|
17
|
+
notification_type: data.type,
|
|
18
|
+
content: data.content || {},
|
|
19
|
+
created_at: data.created_at,
|
|
20
|
+
seen_at: null,
|
|
21
|
+
dismissed_at: null,
|
|
22
|
+
done_at: null,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function getWebSocketUrl(wsUrlBase) {
|
|
26
|
+
if (wsUrlBase) {
|
|
27
|
+
return new URL('/ws/notifications/', wsUrlBase).toString();
|
|
28
|
+
}
|
|
29
|
+
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
|
30
|
+
return `${protocol}//${window.location.host}/ws/notifications/`;
|
|
31
|
+
}
|
|
32
|
+
function patchStatus(notification, status) {
|
|
33
|
+
const seen = Boolean(status.seen);
|
|
34
|
+
const dismissed = Boolean(status.dismissed);
|
|
35
|
+
const done = Boolean(status.done);
|
|
36
|
+
return Object.assign(Object.assign({}, notification), { seen,
|
|
37
|
+
dismissed,
|
|
38
|
+
done, seen_at: seen ? notification.seen_at || true : null, dismissed_at: dismissed ? notification.dismissed_at || true : null, done_at: done ? notification.done_at || true : null });
|
|
39
|
+
}
|
|
40
|
+
export function NotificationsProvider({ children, wsUrlBase }) {
|
|
41
|
+
const auth = useContext(AuthContext);
|
|
42
|
+
const authenticated = Boolean(auth === null || auth === void 0 ? void 0 : auth.user);
|
|
43
|
+
const [notifications, setNotifications] = useState([]);
|
|
44
|
+
const [unreadCount, setUnreadCount] = useState(0);
|
|
45
|
+
const notificationsRef = useRef([]);
|
|
46
|
+
const socketRef = useRef(null);
|
|
47
|
+
const replaceNotifications = useCallback((nextNotifications) => {
|
|
48
|
+
notificationsRef.current = nextNotifications;
|
|
49
|
+
setNotifications(nextNotifications);
|
|
50
|
+
}, []);
|
|
51
|
+
const refresh = useCallback(async () => {
|
|
52
|
+
if (!authenticated) {
|
|
53
|
+
replaceNotifications([]);
|
|
54
|
+
setUnreadCount(0);
|
|
55
|
+
return { notifications: [], unreadCount: 0 };
|
|
56
|
+
}
|
|
57
|
+
const [feed, unread] = await Promise.all([getNotificationFeed(), getUnreadCount()]);
|
|
58
|
+
const nextNotifications = (feed.results || []).map(normalizeNotification);
|
|
59
|
+
const nextUnreadCount = unread.count || 0;
|
|
60
|
+
replaceNotifications(nextNotifications);
|
|
61
|
+
setUnreadCount(nextUnreadCount);
|
|
62
|
+
return { notifications: nextNotifications, unreadCount: nextUnreadCount };
|
|
63
|
+
}, [authenticated, replaceNotifications]);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
if (!authenticated) {
|
|
66
|
+
replaceNotifications([]);
|
|
67
|
+
setUnreadCount(0);
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
let active = true;
|
|
71
|
+
let reconnectTimer = null;
|
|
72
|
+
let backoffMs = INITIAL_BACKOFF_MS;
|
|
73
|
+
const patchNotifications = (updater) => {
|
|
74
|
+
const nextNotifications = updater(notificationsRef.current);
|
|
75
|
+
replaceNotifications(nextNotifications);
|
|
76
|
+
};
|
|
77
|
+
const handleMessage = (event) => {
|
|
78
|
+
let data;
|
|
79
|
+
try {
|
|
80
|
+
data = JSON.parse(event.data);
|
|
81
|
+
}
|
|
82
|
+
catch (_a) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (data.type === 'notification.status') {
|
|
86
|
+
let unreadDelta = 0;
|
|
87
|
+
patchNotifications((current) => current.map((notification) => {
|
|
88
|
+
if (notification.notification_id !== data.notification_id)
|
|
89
|
+
return notification;
|
|
90
|
+
const wasUnread = !notification.seen && !notification.dismissed;
|
|
91
|
+
const patched = patchStatus(notification, data.status || {});
|
|
92
|
+
const isUnread = !patched.seen && !patched.dismissed;
|
|
93
|
+
if (wasUnread !== isUnread) {
|
|
94
|
+
unreadDelta += isUnread ? 1 : -1;
|
|
95
|
+
}
|
|
96
|
+
return patched;
|
|
97
|
+
}));
|
|
98
|
+
if (unreadDelta) {
|
|
99
|
+
setUnreadCount((count) => Math.max(0, count + unreadDelta));
|
|
100
|
+
}
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const pushedNotification = normalizeNotificationPush(data);
|
|
104
|
+
patchNotifications((current) => [pushedNotification, ...current]);
|
|
105
|
+
setUnreadCount((count) => count + 1);
|
|
106
|
+
};
|
|
107
|
+
const connect = () => {
|
|
108
|
+
if (!active || socketRef.current)
|
|
109
|
+
return;
|
|
110
|
+
const socket = new WebSocket(getWebSocketUrl(wsUrlBase));
|
|
111
|
+
socketRef.current = socket;
|
|
112
|
+
socket.onopen = () => {
|
|
113
|
+
backoffMs = INITIAL_BACKOFF_MS;
|
|
114
|
+
};
|
|
115
|
+
socket.onmessage = handleMessage;
|
|
116
|
+
socket.onerror = () => {
|
|
117
|
+
socket.close();
|
|
118
|
+
};
|
|
119
|
+
socket.onclose = () => {
|
|
120
|
+
if (socketRef.current === socket) {
|
|
121
|
+
socketRef.current = null;
|
|
122
|
+
}
|
|
123
|
+
if (!active || reconnectTimer)
|
|
124
|
+
return;
|
|
125
|
+
reconnectTimer = setTimeout(() => {
|
|
126
|
+
reconnectTimer = null;
|
|
127
|
+
connect();
|
|
128
|
+
}, backoffMs);
|
|
129
|
+
backoffMs = Math.min(backoffMs * 2, MAX_BACKOFF_MS);
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
const seedAndConnect = async () => {
|
|
133
|
+
try {
|
|
134
|
+
await refresh();
|
|
135
|
+
}
|
|
136
|
+
catch (_a) {
|
|
137
|
+
// The live stream remains useful if the initial REST request is temporarily unavailable.
|
|
138
|
+
}
|
|
139
|
+
if (active)
|
|
140
|
+
connect();
|
|
141
|
+
};
|
|
142
|
+
seedAndConnect();
|
|
143
|
+
return () => {
|
|
144
|
+
active = false;
|
|
145
|
+
clearTimeout(reconnectTimer);
|
|
146
|
+
const socket = socketRef.current;
|
|
147
|
+
socketRef.current = null;
|
|
148
|
+
if (socket)
|
|
149
|
+
socket.close();
|
|
150
|
+
};
|
|
151
|
+
}, [authenticated, refresh, replaceNotifications, wsUrlBase]);
|
|
152
|
+
const mark = useCallback((action, ids) => {
|
|
153
|
+
const selectedIds = Array.isArray(ids) ? ids : [];
|
|
154
|
+
const selected = new Set(selectedIds);
|
|
155
|
+
const current = notificationsRef.current;
|
|
156
|
+
let unreadDelta = 0;
|
|
157
|
+
const nextNotifications = current.map((notification) => {
|
|
158
|
+
if (!selected.has(notification.id))
|
|
159
|
+
return notification;
|
|
160
|
+
const wasUnread = !notification.seen && !notification.dismissed;
|
|
161
|
+
if ((action === 'seen' || action === 'dismissed') && wasUnread) {
|
|
162
|
+
unreadDelta += 1;
|
|
163
|
+
}
|
|
164
|
+
return patchStatus(notification, {
|
|
165
|
+
seen: action === 'seen' ? true : notification.seen,
|
|
166
|
+
dismissed: action === 'dismissed' ? true : notification.dismissed,
|
|
167
|
+
done: action === 'done' ? true : notification.done,
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
replaceNotifications(nextNotifications);
|
|
171
|
+
if (unreadDelta) {
|
|
172
|
+
setUnreadCount((count) => Math.max(0, count - unreadDelta));
|
|
173
|
+
}
|
|
174
|
+
return markNotifications({ action, ids: selectedIds });
|
|
175
|
+
}, [replaceNotifications]);
|
|
176
|
+
const markSeen = useCallback((ids) => mark('seen', ids), [mark]);
|
|
177
|
+
const markDismissed = useCallback((ids) => mark('dismissed', ids), [mark]);
|
|
178
|
+
const markDone = useCallback((ids) => mark('done', ids), [mark]);
|
|
179
|
+
return (_jsx(NotificationsContext.Provider, { value: {
|
|
180
|
+
notifications,
|
|
181
|
+
unreadCount,
|
|
182
|
+
markSeen,
|
|
183
|
+
markDismissed,
|
|
184
|
+
markDone,
|
|
185
|
+
refresh,
|
|
186
|
+
}, children: children }));
|
|
187
|
+
}
|
|
188
|
+
export function useNotifications() {
|
|
189
|
+
const context = useContext(NotificationsContext);
|
|
190
|
+
if (!context) {
|
|
191
|
+
throw new Error('useNotifications must be used within a NotificationsProvider');
|
|
192
|
+
}
|
|
193
|
+
return context;
|
|
194
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import apiClient from '../auth/apiClient';
|
|
2
|
+
export async function getNotificationFeed({ status } = {}) {
|
|
3
|
+
const response = await apiClient.get('/api/notifications/feed/', {
|
|
4
|
+
params: status ? { status } : undefined,
|
|
5
|
+
});
|
|
6
|
+
return response.data;
|
|
7
|
+
}
|
|
8
|
+
export async function getUnreadCount() {
|
|
9
|
+
const response = await apiClient.get('/api/notifications/feed/unread-count/');
|
|
10
|
+
return response.data;
|
|
11
|
+
}
|
|
12
|
+
export async function markNotifications({ action, ids }) {
|
|
13
|
+
const response = await apiClient.post('/api/notifications/feed/mark/', { action, ids });
|
|
14
|
+
return response.data;
|
|
15
|
+
}
|
package/dist/pages/SignUpPage.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import React, { useContext, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import React, { useContext, useEffect, useMemo, useRef, useState, } from 'react';
|
|
3
3
|
import { useNavigate, useLocation } from 'react-router-dom';
|
|
4
4
|
import { Alert, Box, Button, Stack, TextField, Typography, } from '@mui/material';
|
|
5
5
|
import { Helmet } from 'react-helmet';
|
|
@@ -7,6 +7,11 @@ import { useTranslation } from 'react-i18next';
|
|
|
7
7
|
import { NarrowPage } from '../layout/PageLayout';
|
|
8
8
|
import { AuthContext } from '../auth/AuthContext';
|
|
9
9
|
import { submitRegistrationRequest } from '../auth/authApi';
|
|
10
|
+
import { TurnstileWidget } from '../components/TurnstileWidget';
|
|
11
|
+
const TURNSTILE_SIGNUP_MODES = new Set([
|
|
12
|
+
'self_signup_open',
|
|
13
|
+
'self_signup_email_domain',
|
|
14
|
+
]);
|
|
10
15
|
const MODE_LABELS = {
|
|
11
16
|
self_signup_access_code: 'Auth.SIGNUP_ACCESS_CODE_TAB',
|
|
12
17
|
self_signup_open: 'Auth.SIGNUP_OPEN_TAB',
|
|
@@ -47,6 +52,10 @@ export function SignUpPage() {
|
|
|
47
52
|
const [submitting, setSubmitting] = useState(false);
|
|
48
53
|
const [successKey, setSuccessKey] = useState(null);
|
|
49
54
|
const [errorKey, setErrorKey] = useState(null);
|
|
55
|
+
const [turnstileToken, setTurnstileToken] = useState('');
|
|
56
|
+
const turnstileWidgetRef = useRef(null);
|
|
57
|
+
const turnstileRequired = Boolean(authMethods === null || authMethods === void 0 ? void 0 : authMethods.turnstile_site_key)
|
|
58
|
+
&& TURNSTILE_SIGNUP_MODES.has(mode);
|
|
50
59
|
const pageSubtitle = useMemo(() => {
|
|
51
60
|
if (mode === 'self_signup_qr' && tokenFromUrl) {
|
|
52
61
|
return t('Auth.PAGE_SIGNUP_SUBTITLE_QR_READY', 'Enter your email address to complete sign-up with the QR link you opened.');
|
|
@@ -56,7 +65,11 @@ export function SignUpPage() {
|
|
|
56
65
|
useEffect(() => {
|
|
57
66
|
setMode(initialMode);
|
|
58
67
|
}, [initialMode]);
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
setTurnstileToken('');
|
|
70
|
+
}, [mode, authMethods === null || authMethods === void 0 ? void 0 : authMethods.turnstile_site_key]);
|
|
59
71
|
const handleSubmit = async (event) => {
|
|
72
|
+
var _a;
|
|
60
73
|
event.preventDefault();
|
|
61
74
|
setSuccessKey(null);
|
|
62
75
|
setErrorKey(null);
|
|
@@ -72,6 +85,8 @@ export function SignUpPage() {
|
|
|
72
85
|
setErrorKey('Auth.SIGNUP_QR_INVALID');
|
|
73
86
|
return;
|
|
74
87
|
}
|
|
88
|
+
if (turnstileRequired && !turnstileToken)
|
|
89
|
+
return;
|
|
75
90
|
setSubmitting(true);
|
|
76
91
|
try {
|
|
77
92
|
await submitRegistrationRequest({
|
|
@@ -79,11 +94,16 @@ export function SignUpPage() {
|
|
|
79
94
|
mode,
|
|
80
95
|
accessCode,
|
|
81
96
|
registrationContextToken: mode === 'self_signup_qr' ? tokenFromUrl : null,
|
|
97
|
+
turnstileToken: turnstileRequired ? turnstileToken : undefined,
|
|
82
98
|
});
|
|
83
99
|
setSuccessKey('Auth.INVITE_REQUEST_SUCCESS');
|
|
84
100
|
}
|
|
85
101
|
catch (err) {
|
|
86
102
|
setErrorKey(err.code || 'Auth.INVITE_FAILED');
|
|
103
|
+
if (turnstileRequired) {
|
|
104
|
+
setTurnstileToken('');
|
|
105
|
+
(_a = turnstileWidgetRef.current) === null || _a === void 0 ? void 0 : _a.reset();
|
|
106
|
+
}
|
|
87
107
|
}
|
|
88
108
|
finally {
|
|
89
109
|
setSubmitting(false);
|
|
@@ -92,7 +112,7 @@ export function SignUpPage() {
|
|
|
92
112
|
const handleGoToLogin = () => {
|
|
93
113
|
navigate('/login');
|
|
94
114
|
};
|
|
95
|
-
return (_jsxs(NarrowPage, { title: t('Auth.LOGIN_SIGNUP_BUTTON'), subtitle: pageSubtitle, children: [_jsx(Helmet, { children: _jsxs("title", { children: [t('App.NAME'), " \u2013 ", t('Auth.LOGIN_SIGNUP_BUTTON')] }) }), successKey && (_jsx(Alert, { severity: "success", sx: { mb: 2 }, children: t(successKey, { email }) })), errorKey && (_jsx(Alert, { severity: "error", sx: { mb: 2 }, children: t(errorKey, t('Auth.INVITE_FAILED', 'Could not complete signup.')) })), signupModes.length > 1 && (_jsx(Stack, { spacing: 1, sx: { mb: 2 }, children: _jsx(Stack, { spacing: 1, children: signupModes.map((entry) => (_jsx(Button, { variant: mode === entry ? 'contained' : 'outlined', onClick: () => setMode(entry), disabled: submitting, fullWidth: true, children: t(MODE_LABELS[entry] || entry, entry) }, entry))) }) })), _jsxs(Box, { component: "form", onSubmit: handleSubmit, sx: { display: 'flex', flexDirection: 'column', gap: 2 }, children: [_jsx(TextField, { label: t('Auth.EMAIL_LABEL'), type: "email", required: true, fullWidth: true, value: email, onChange: (e) => setEmail(e.target.value), disabled: submitting }), mode === 'self_signup_access_code' && (_jsx(TextField, { label: t('Auth.ACCESS_CODE_LABEL'), type: "text", required: true, fullWidth: true, value: accessCode, onChange: (e) => setAccessCode(e.target.value), disabled: submitting })), _jsx(Button, { type: "submit", variant: "contained", disabled: submitting || signupModes.length === 0, children: submitting
|
|
115
|
+
return (_jsxs(NarrowPage, { title: t('Auth.LOGIN_SIGNUP_BUTTON'), subtitle: pageSubtitle, children: [_jsx(Helmet, { children: _jsxs("title", { children: [t('App.NAME'), " \u2013 ", t('Auth.LOGIN_SIGNUP_BUTTON')] }) }), successKey && (_jsx(Alert, { severity: "success", sx: { mb: 2 }, children: t(successKey, { email }) })), errorKey && (_jsx(Alert, { severity: "error", sx: { mb: 2 }, children: t(errorKey, t('Auth.INVITE_FAILED', 'Could not complete signup.')) })), signupModes.length > 1 && (_jsx(Stack, { spacing: 1, sx: { mb: 2 }, children: _jsx(Stack, { spacing: 1, children: signupModes.map((entry) => (_jsx(Button, { variant: mode === entry ? 'contained' : 'outlined', onClick: () => setMode(entry), disabled: submitting, fullWidth: true, children: t(MODE_LABELS[entry] || entry, entry) }, entry))) }) })), _jsxs(Box, { component: "form", onSubmit: handleSubmit, sx: { display: 'flex', flexDirection: 'column', gap: 2 }, children: [_jsx(TextField, { label: t('Auth.EMAIL_LABEL'), type: "email", required: true, fullWidth: true, value: email, onChange: (e) => setEmail(e.target.value), disabled: submitting }), mode === 'self_signup_access_code' && (_jsx(TextField, { label: t('Auth.ACCESS_CODE_LABEL'), type: "text", required: true, fullWidth: true, value: accessCode, onChange: (e) => setAccessCode(e.target.value), disabled: submitting })), turnstileRequired && (_jsx(TurnstileWidget, { ref: turnstileWidgetRef, siteKey: authMethods.turnstile_site_key, onToken: setTurnstileToken })), _jsx(Button, { type: "submit", variant: "contained", disabled: submitting || signupModes.length === 0 || (turnstileRequired && !turnstileToken), children: submitting
|
|
96
116
|
? t('Auth.SIGNUP_SUBMITTING')
|
|
97
117
|
: t('Auth.SIGNUP_SUBMIT') })] }), _jsx(Box, { sx: { mt: 3 }, children: _jsxs(Typography, { variant: "body2", children: [t('Auth.SIGNUP_ALREADY_HAVE_ACCOUNT'), ' ', _jsx(Button, { onClick: handleGoToLogin, variant: "text", size: "small", children: t('Auth.SIGNUP_GO_TO_LOGIN') })] }) })] }));
|
|
98
118
|
}
|
package/package.json
CHANGED
package/src/auth/AuthContext.jsx
CHANGED
package/src/auth/authApi.jsx
CHANGED
|
@@ -359,6 +359,7 @@ export async function submitRegistrationRequest({
|
|
|
359
359
|
accessCode,
|
|
360
360
|
registrationContextToken,
|
|
361
361
|
registrationContext,
|
|
362
|
+
turnstileToken,
|
|
362
363
|
}) {
|
|
363
364
|
const payload = { email, mode };
|
|
364
365
|
if (accessCode) payload.access_code = accessCode;
|
|
@@ -368,6 +369,7 @@ export async function submitRegistrationRequest({
|
|
|
368
369
|
if (registrationContext) {
|
|
369
370
|
payload.registration_context = registrationContext;
|
|
370
371
|
}
|
|
372
|
+
if (turnstileToken) payload.turnstile_token = turnstileToken;
|
|
371
373
|
|
|
372
374
|
try {
|
|
373
375
|
const res = await apiClient.post(`${USERS_BASE}/register-request/`, payload);
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
forwardRef,
|
|
3
|
+
useCallback,
|
|
4
|
+
useEffect,
|
|
5
|
+
useImperativeHandle,
|
|
6
|
+
useRef,
|
|
7
|
+
} from 'react';
|
|
8
|
+
|
|
9
|
+
const TURNSTILE_SCRIPT_URL = 'https://challenges.cloudflare.com/turnstile/v0/api.js';
|
|
10
|
+
const TURNSTILE_SCRIPT_SELECTOR = 'script[data-turnstile-api="true"]';
|
|
11
|
+
|
|
12
|
+
let turnstileScriptPromise;
|
|
13
|
+
|
|
14
|
+
function loadTurnstileScript() {
|
|
15
|
+
if (window.turnstile?.render) {
|
|
16
|
+
return Promise.resolve(window.turnstile);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!turnstileScriptPromise) {
|
|
20
|
+
turnstileScriptPromise = new Promise((resolve, reject) => {
|
|
21
|
+
let script = document.querySelector(TURNSTILE_SCRIPT_SELECTOR);
|
|
22
|
+
if (!script) {
|
|
23
|
+
script = document.createElement('script');
|
|
24
|
+
script.src = TURNSTILE_SCRIPT_URL;
|
|
25
|
+
script.async = true;
|
|
26
|
+
script.defer = true;
|
|
27
|
+
script.dataset.turnstileApi = 'true';
|
|
28
|
+
document.head.appendChild(script);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
script.addEventListener('load', () => {
|
|
32
|
+
if (window.turnstile?.render) {
|
|
33
|
+
resolve(window.turnstile);
|
|
34
|
+
} else {
|
|
35
|
+
reject(new Error('Turnstile did not initialize.'));
|
|
36
|
+
}
|
|
37
|
+
}, { once: true });
|
|
38
|
+
script.addEventListener('error', () => {
|
|
39
|
+
reject(new Error('Turnstile could not be loaded.'));
|
|
40
|
+
}, { once: true });
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return turnstileScriptPromise;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const TurnstileWidget = forwardRef(function TurnstileWidget(
|
|
48
|
+
{ siteKey, onToken },
|
|
49
|
+
ref,
|
|
50
|
+
) {
|
|
51
|
+
const containerRef = useRef(null);
|
|
52
|
+
const widgetIdRef = useRef(null);
|
|
53
|
+
const onTokenRef = useRef(onToken);
|
|
54
|
+
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
onTokenRef.current = onToken;
|
|
57
|
+
}, [onToken]);
|
|
58
|
+
|
|
59
|
+
const reset = useCallback(() => {
|
|
60
|
+
if (widgetIdRef.current !== null && window.turnstile?.reset) {
|
|
61
|
+
window.turnstile.reset(widgetIdRef.current);
|
|
62
|
+
}
|
|
63
|
+
onTokenRef.current('');
|
|
64
|
+
}, []);
|
|
65
|
+
|
|
66
|
+
useImperativeHandle(ref, () => ({ reset }), [reset]);
|
|
67
|
+
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
if (!siteKey) return undefined;
|
|
70
|
+
|
|
71
|
+
let cancelled = false;
|
|
72
|
+
loadTurnstileScript()
|
|
73
|
+
.then((turnstile) => {
|
|
74
|
+
if (cancelled || !containerRef.current) return;
|
|
75
|
+
|
|
76
|
+
widgetIdRef.current = turnstile.render(containerRef.current, {
|
|
77
|
+
sitekey: siteKey,
|
|
78
|
+
callback: (token) => onTokenRef.current(token),
|
|
79
|
+
'expired-callback': reset,
|
|
80
|
+
'error-callback': () => onTokenRef.current(''),
|
|
81
|
+
});
|
|
82
|
+
})
|
|
83
|
+
.catch(() => {
|
|
84
|
+
if (!cancelled) onTokenRef.current('');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
return () => {
|
|
88
|
+
cancelled = true;
|
|
89
|
+
if (widgetIdRef.current !== null && window.turnstile?.remove) {
|
|
90
|
+
window.turnstile.remove(widgetIdRef.current);
|
|
91
|
+
}
|
|
92
|
+
widgetIdRef.current = null;
|
|
93
|
+
};
|
|
94
|
+
}, [siteKey, reset]);
|
|
95
|
+
|
|
96
|
+
if (!siteKey) return null;
|
|
97
|
+
|
|
98
|
+
return <div ref={containerRef} data-testid="turnstile-widget" />;
|
|
99
|
+
});
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export const notificationsTranslations = {
|
|
2
|
+
'NotificationBell.TITLE': { de: 'Benachrichtigungen', fr: 'Notifications', en: 'Notifications', sw: 'Arifa' },
|
|
3
|
+
'NotificationBell.EMPTY': { de: 'Keine Benachrichtigungen vorhanden.', fr: 'Aucune notification.', en: 'No notifications.', sw: 'Hakuna arifa.' },
|
|
4
|
+
'NotificationBell.MARK_ALL': { de: 'Alle als gelesen markieren', fr: 'Tout marquer comme lu', en: 'Mark all as read', sw: 'Weka zote alama kuwa zimesomwa' },
|
|
2
5
|
'NotificationSettings.TITLE': { de: 'Benachrichtigungen', fr: 'Notifications', en: 'Notifications', sw: 'Arifa' },
|
|
3
6
|
'NotificationSettings.SUBTITLE': { de: 'Lege fest, wie du informiert werden möchtest.', fr: 'Choisissez comment vous souhaitez être informé.', en: 'Choose how you want to be notified.', sw: 'Chagua jinsi unavyotaka kuarifiwa.' },
|
|
4
7
|
'NotificationSettings.LOAD_ERROR': { de: 'Benachrichtigungseinstellungen konnten nicht geladen werden.', fr: 'Impossible de charger les paramètres de notification.', en: 'Notification settings could not be loaded.', sw: 'Mipangilio ya arifa haikuweza kupakiwa.' },
|
package/src/index.js
CHANGED
|
@@ -45,6 +45,9 @@ export { authTranslations } from './i18n/authTranslations';
|
|
|
45
45
|
// --- 7. Notifications ---
|
|
46
46
|
export { NotificationSettings } from './notifications/NotificationSettings';
|
|
47
47
|
export * from './notifications/api';
|
|
48
|
+
export { NotificationsProvider, useNotifications } from './notifications/NotificationsProvider';
|
|
49
|
+
export { NotificationBell } from './notifications/NotificationBell';
|
|
50
|
+
export { getNotificationFeed, getUnreadCount, markNotifications } from './notifications/feedApi';
|
|
48
51
|
|
|
49
52
|
// --- 8. Onboarding ---
|
|
50
53
|
export * from './onboarding/api';
|