@micha.bigler/ui-core-micha 4.40.2 → 4.40.3
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/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 4.40.3 - UCM-NOTIF-17
|
|
4
|
+
|
|
5
|
+
Notification feed, unread count and pagination state are cleared before the
|
|
6
|
+
first committed render for a different account. Delayed operations from the
|
|
7
|
+
previous account cannot restore its state or request further feed pages.
|
|
8
|
+
|
|
3
9
|
## 4.40.2 - UCM-SHELL-10
|
|
4
10
|
|
|
5
11
|
Notification and account menu buttons include their visible unread count or
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useCallback, useContext, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useInsertionEffect, useRef, useState } from 'react';
|
|
3
3
|
import { AuthContext } from '../auth/AuthContext';
|
|
4
4
|
import { getNotificationFeed, getUnreadCount, markNotifications, markAllNotificationsSeen } from './feedApi';
|
|
5
5
|
import { DEFAULT_ENVELOPE, RealtimeContext, useRealtimeCore } from './realtime';
|
|
@@ -43,14 +43,9 @@ function NotificationsScope({ children, wsUrlBase }) {
|
|
|
43
43
|
const auth = useContext(AuthContext);
|
|
44
44
|
const authenticated = Boolean(auth === null || auth === void 0 ? void 0 : auth.user);
|
|
45
45
|
const accountKey = (auth === null || auth === void 0 ? void 0 : auth.user) ? `user:${auth.user.id}` : 'logged-out';
|
|
46
|
-
const
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
accountRef.current = accountKey;
|
|
50
|
-
generation.current += 1;
|
|
51
|
-
}
|
|
52
|
-
const accountGeneration = generation.current;
|
|
53
|
-
const isCurrent = () => alive.current && generation.current === accountGeneration;
|
|
46
|
+
const [account, setAccount] = useState(() => ({ key: accountKey }));
|
|
47
|
+
const accountRef = useRef(account);
|
|
48
|
+
const isCurrent = () => alive.current && accountRef.current === account;
|
|
54
49
|
const bootstrapSeed = auth === null || auth === void 0 ? void 0 : auth.bootstrapNotificationSeed;
|
|
55
50
|
const consumeBootstrapSeed = auth === null || auth === void 0 ? void 0 : auth.consumeBootstrapNotificationSeed;
|
|
56
51
|
const [notifications, setNotifications] = useState([]);
|
|
@@ -69,6 +64,31 @@ function NotificationsScope({ children, wsUrlBase }) {
|
|
|
69
64
|
const [feedError, setFeedError] = useState(false);
|
|
70
65
|
const [markError, setMarkError] = useState(false);
|
|
71
66
|
const [marking, setMarking] = useState(false);
|
|
67
|
+
const [seeded, setSeeded] = useState(false);
|
|
68
|
+
if (account.key !== accountKey) {
|
|
69
|
+
setAccount({ key: accountKey });
|
|
70
|
+
setNotifications([]);
|
|
71
|
+
setUnreadCount(0);
|
|
72
|
+
setNext(null);
|
|
73
|
+
setCapable(false);
|
|
74
|
+
setLoadingMore(false);
|
|
75
|
+
setFeedError(false);
|
|
76
|
+
setMarkError(false);
|
|
77
|
+
setMarking(false);
|
|
78
|
+
setSeeded(false);
|
|
79
|
+
}
|
|
80
|
+
const accountGeneration = account;
|
|
81
|
+
useInsertionEffect(() => {
|
|
82
|
+
if (accountRef.current === account)
|
|
83
|
+
return;
|
|
84
|
+
accountRef.current = account;
|
|
85
|
+
notificationsRef.current = [];
|
|
86
|
+
nextRef.current = null;
|
|
87
|
+
loadingRef.current = false;
|
|
88
|
+
pageCount.current = 1;
|
|
89
|
+
pageVersion.current += 1;
|
|
90
|
+
pageEpoch.current += 1;
|
|
91
|
+
}, [account]);
|
|
72
92
|
useEffect(() => { alive.current = true; return () => { alive.current = false; }; }, []);
|
|
73
93
|
const setContinuation = (value) => { nextRef.current = value || null; setNext(value || null); };
|
|
74
94
|
const replaceNotifications = useCallback((nextNotifications) => {
|
|
@@ -77,6 +97,8 @@ function NotificationsScope({ children, wsUrlBase }) {
|
|
|
77
97
|
}, []);
|
|
78
98
|
const refresh = useCallback(async () => {
|
|
79
99
|
var _a;
|
|
100
|
+
if (!isCurrent())
|
|
101
|
+
return;
|
|
80
102
|
if (!authenticated) {
|
|
81
103
|
replaceNotifications([]);
|
|
82
104
|
setUnreadCount(0);
|
|
@@ -89,6 +111,8 @@ function NotificationsScope({ children, wsUrlBase }) {
|
|
|
89
111
|
let continuation = feed.next;
|
|
90
112
|
const rows = [...(feed.results || [])];
|
|
91
113
|
for (let page = 1; page < targetPages && continuation; page += 1) {
|
|
114
|
+
if (!isCurrent())
|
|
115
|
+
return;
|
|
92
116
|
const result = await getNotificationFeed({ next: continuation });
|
|
93
117
|
rows.push(...(result.results || []));
|
|
94
118
|
continuation = result.next;
|
|
@@ -117,7 +141,6 @@ function NotificationsScope({ children, wsUrlBase }) {
|
|
|
117
141
|
// Preserved here rather than connecting immediately, since `refresh()`'s
|
|
118
142
|
// `replaceNotifications(...)` is unconditional and would silently drop a message that
|
|
119
143
|
// arrived and patched state before the initial REST response landed.
|
|
120
|
-
const [seeded, setSeeded] = useState(false);
|
|
121
144
|
const { subscribe, onReconnect } = useRealtimeCore({
|
|
122
145
|
active: authenticated && seeded,
|
|
123
146
|
wsUrl: getWebSocketUrl(wsUrlBase),
|
|
@@ -221,6 +244,8 @@ function NotificationsScope({ children, wsUrlBase }) {
|
|
|
221
244
|
};
|
|
222
245
|
}, [authenticated, accountGeneration, refresh, replaceNotifications, consumeBootstrapSeed]);
|
|
223
246
|
const mark = useCallback((action, ids) => {
|
|
247
|
+
if (!isCurrent())
|
|
248
|
+
return Promise.resolve();
|
|
224
249
|
const selectedIds = Array.isArray(ids) ? ids : [];
|
|
225
250
|
const selected = new Set(selectedIds);
|
|
226
251
|
const current = notificationsRef.current;
|
|
@@ -273,7 +298,7 @@ function NotificationsScope({ children, wsUrlBase }) {
|
|
|
273
298
|
const markDismissed = useCallback((ids) => mark('dismissed', ids), [mark]);
|
|
274
299
|
const markDone = useCallback((ids) => mark('done', ids), [mark]);
|
|
275
300
|
const loadMore = useCallback(async () => {
|
|
276
|
-
if (!nextRef.current || loadingRef.current)
|
|
301
|
+
if (!isCurrent() || !nextRef.current || loadingRef.current)
|
|
277
302
|
return;
|
|
278
303
|
loadingRef.current = true;
|
|
279
304
|
setLoadingMore(true);
|
|
@@ -308,7 +333,7 @@ function NotificationsScope({ children, wsUrlBase }) {
|
|
|
308
333
|
}
|
|
309
334
|
}, [replaceNotifications, accountGeneration]);
|
|
310
335
|
const markAllSeen = useCallback(async () => {
|
|
311
|
-
if (!capable || marking)
|
|
336
|
+
if (!isCurrent() || !capable || marking)
|
|
312
337
|
return;
|
|
313
338
|
setMarking(true);
|
|
314
339
|
setMarkError(false);
|