@burdenoff/microfe-notification 2026.1117.1 → 2026.1118.1

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.
@@ -1,14 +1,15 @@
1
1
  import { useNotification as e } from "../providers/NotificationProvider.js";
2
2
  import { USER_NOTIFICATION_CONTEXT as t, getGlobalNotificationClient as n } from "../graphql/global-client.js";
3
3
  import { useNotificationStore as r } from "../store/notificationStore.js";
4
- import { ArchiveNotificationDocument as i, ArchiveNotificationsDocument as a, GetMyUnreadNotificationCountDocument as o, GetNotificationsDocument as s, MarkAllMyNotificationsAsReadDocument as c, MarkNotificationAsReadDocument as l, NotificationReceivedDocument as u, RecordNotificationActionDocument as d, UnarchiveNotificationDocument as f, UnreadCountUpdatedDocument as p } from "../generated/global-operations.js";
5
- import { useCallback as m, useEffect as h, useMemo as g, useState as _ } from "react";
4
+ import { appendNotificationPage as i, mergeRefreshedFirstPage as ee, nextNotificationWindow as a } from "./notificationPaging.js";
5
+ import { ArchiveNotificationDocument as o, ArchiveNotificationsDocument as s, GetMyUnreadNotificationCountDocument as c, GetNotificationsDocument as l, MarkAllMyNotificationsAsReadDocument as te, MarkNotificationAsReadDocument as ne, NotificationReceivedDocument as u, RecordNotificationActionDocument as d, UnarchiveNotificationDocument as re, UnreadCountUpdatedDocument as ie } from "../generated/global-operations.js";
6
+ import { useCallback as f, useEffect as p, useMemo as m, useRef as h, useState as g } from "react";
6
7
  // src/hooks/useNotifications.ts
7
- function v(e) {
8
+ function _(e) {
8
9
  return {
9
10
  id: e.id,
10
- type: y(e.type),
11
- priority: b(e.priority),
11
+ type: v(e.type),
12
+ priority: y(e.priority),
12
13
  title: e.title,
13
14
  body: e.message,
14
15
  actionUrl: e.redirectUrl ?? void 0,
@@ -45,13 +46,13 @@ function v(e) {
45
46
  timezone: e.scheduledNotification.timezone ?? void 0
46
47
  } : void 0,
47
48
  sentAt: e.sentAt ?? void 0,
48
- status: x(e.status),
49
+ status: b(e.status),
49
50
  readAt: e.readAt ?? void 0,
50
51
  createdAt: e.createdAt,
51
52
  channels: [{
52
53
  id: e.id,
53
54
  channel: e.channel,
54
- status: x(e.status),
55
+ status: b(e.status),
55
56
  sentAt: e.sentAt ?? void 0,
56
57
  deliveredAt: e.deliveredAt ?? void 0,
57
58
  failedAt: e.failedAt ?? void 0,
@@ -63,12 +64,12 @@ function v(e) {
63
64
  id: e.topic.id,
64
65
  name: e.topic.name,
65
66
  slug: e.topic.slug,
66
- type: y(e.topic.type)
67
+ type: v(e.topic.type)
67
68
  } : void 0,
68
69
  topicId: e.topicId ?? void 0
69
70
  };
70
71
  }
71
- function y(e) {
72
+ function v(e) {
72
73
  return {
73
74
  SYSTEM: "SYSTEM",
74
75
  TRANSACTIONAL: "TRANSACTIONAL",
@@ -76,7 +77,7 @@ function y(e) {
76
77
  ALERT: "ALERT"
77
78
  }[e] || "SYSTEM";
78
79
  }
79
- function b(e) {
80
+ function y(e) {
80
81
  return {
81
82
  LOW: "LOW",
82
83
  MEDIUM: "NORMAL",
@@ -84,7 +85,7 @@ function b(e) {
84
85
  URGENT: "URGENT"
85
86
  }[e] || "NORMAL";
86
87
  }
87
- function x(e) {
88
+ function b(e) {
88
89
  return {
89
90
  DRAFT: "PENDING",
90
91
  PENDING: "PENDING",
@@ -98,7 +99,7 @@ function x(e) {
98
99
  EXPIRED: "ARCHIVED"
99
100
  }[e] || "PENDING";
100
101
  }
101
- function S(e) {
102
+ function x(e) {
102
103
  let t = {};
103
104
  return e.type !== "ALL" && (t.type = {
104
105
  SYSTEM: "SYSTEM",
@@ -114,7 +115,7 @@ function S(e) {
114
115
  URGENT: "URGENT"
115
116
  }[e.priority] || e.priority), e.isRead !== null && e.isRead !== void 0 && (t.isRead = e.isRead), e.topicId && (t.topicId = e.topicId), e.channel && e.channel !== "ALL" && (t.channel = e.channel), e.archived !== null && e.archived !== void 0 && (t.archived = e.archived), e.search && (t.search = e.search), e.dateRange.start && (t.startDate = e.dateRange.start.toISOString()), e.dateRange.end && (t.endDate = e.dateRange.end.toISOString()), Object.keys(t).length > 0 ? t : void 0;
116
117
  }
117
- function C(e, t) {
118
+ function S(e, t) {
118
119
  return t ? {
119
120
  ...e,
120
121
  ...t,
@@ -124,194 +125,240 @@ function C(e, t) {
124
125
  }
125
126
  } : e;
126
127
  }
127
- function w(y = {}) {
128
- let { authToken: b, apiGatewayUrl: x, currentUser: w, eventBus: T } = e(), { notificationFilter: E, updateNotificationFilter: D, resetNotificationFilter: O, unreadCount: k, setUnreadCount: A, setIsLoadingNotifications: j } = r(), [M, N] = _([]), [P, F] = _(null), [I, L] = _(!1), [R, z] = _(null), [B, V] = _(!1), [H, U] = _(0), [W, G] = _(0), K = g(() => C(E, y.baseFilter), [E, y.baseFilter]), [q, J] = _(K);
129
- h(() => {
130
- let e = window.setTimeout(() => J(K), 250);
128
+ function C(v = {}) {
129
+ let { authToken: y, apiGatewayUrl: b, currentUser: C, eventBus: w } = e(), { notificationFilter: T, updateNotificationFilter: ae, resetNotificationFilter: E, unreadCount: D, setUnreadCount: O, setIsLoadingNotifications: k } = r(), [A, j] = g([]), [M, N] = g(null), [P, F] = g(!1), [oe, I] = g(null), [L, R] = g(!1), [se, z] = g(0), [B, V] = g(!1), H = h(!1), U = h(0), W = h(/* @__PURE__ */ new Set()), G = h(0), K = h(!1), q = h([]);
130
+ p(() => {
131
+ q.current = A;
132
+ }, [A]);
133
+ let J = m(() => S(T, v.baseFilter), [T, v.baseFilter]), [Y, ce] = g(J);
134
+ p(() => {
135
+ let e = window.setTimeout(() => ce(J), 250);
131
136
  return () => window.clearTimeout(e);
132
- }, [K]);
133
- let Y = m(() => n(x ?? void 0, b ?? void 0), [x, b]), X = m(async () => {
134
- L(!0), j(!0), z(null);
137
+ }, [J]);
138
+ let X = f(() => n(b ?? void 0, y ?? void 0), [b, y]), Z = f(async () => {
139
+ U.current += 1;
140
+ let e = U.current;
141
+ K.current = !0, F(!0), k(!0), I(null);
135
142
  try {
136
- let e = await Y().query({
143
+ let n = await X().query({
137
144
  context: t,
138
- query: s,
145
+ query: l,
139
146
  variables: {
140
- filter: S(q),
147
+ filter: x(Y),
141
148
  limit: 50,
142
149
  offset: 0
143
150
  },
144
151
  fetchPolicy: "network-only"
145
152
  });
146
- if (e.data?.notifications) {
147
- let t = e.data.notifications.items.map(v);
148
- N(t), U(e.data.notifications.total), V(e.data.notifications.hasMore), G(50);
153
+ if (n.data?.notifications && e === U.current) {
154
+ let e = n.data.notifications.items.map(_);
155
+ W.current = new Set(e.map((e) => e.id)), j(e), z(n.data.notifications.total), R(n.data.notifications.hasMore);
149
156
  }
150
- } catch (e) {
151
- let t = e instanceof Error ? e.message : "Failed to fetch notifications";
152
- console.error("[useNotifications] Failed to fetch notifications:", e), z(t);
157
+ } catch (t) {
158
+ let n = t instanceof Error ? t.message : "Failed to fetch notifications";
159
+ console.error("[useNotifications] Failed to fetch notifications:", t), I(n), e === U.current && R(!1);
153
160
  } finally {
154
- L(!1), j(!1);
161
+ e === U.current && (K.current = !1), F(!1), k(!1);
155
162
  }
156
163
  }, [
157
- q,
158
164
  Y,
159
- j
160
- ]), Z = m(async () => {
165
+ X,
166
+ k
167
+ ]), Q = f(async () => {
161
168
  try {
162
- let e = await Y().query({
169
+ let e = await X().query({
163
170
  context: t,
164
- query: o,
165
- variables: { filter: y.unreadFilter || { channel: "IN_APP" } },
171
+ query: c,
172
+ variables: { filter: v.unreadFilter || { channel: "IN_APP" } },
166
173
  fetchPolicy: "network-only"
167
174
  });
168
- e.data && A(e.data.myUnreadNotificationCount);
175
+ e.data && O(e.data.myUnreadNotificationCount);
169
176
  } catch (e) {
170
177
  console.error("[useNotifications] Failed to fetch unread count:", e);
171
178
  }
172
179
  }, [
173
- Y,
174
- y.unreadFilter,
175
- A
176
- ]), Q = m(async () => {
177
- if (B) try {
178
- let e = await Y().query({
180
+ X,
181
+ v.unreadFilter,
182
+ O
183
+ ]), le = f(async () => {
184
+ if (!L || H.current || K.current) return;
185
+ let e = a(A, W.current, Y);
186
+ if (!e) return;
187
+ H.current = !0, V(!0);
188
+ let n = U.current;
189
+ try {
190
+ let r = await X().query({
179
191
  context: t,
180
- query: s,
192
+ query: l,
181
193
  variables: {
182
- filter: S(q),
183
- limit: 50,
184
- offset: W
194
+ filter: x({
195
+ ...Y,
196
+ dateRange: {
197
+ ...Y.dateRange,
198
+ end: e.before
199
+ }
200
+ }),
201
+ limit: e.limit,
202
+ offset: e.offset
185
203
  },
186
204
  fetchPolicy: "network-only"
187
205
  });
188
- if (e.data?.notifications) {
189
- let t = e.data.notifications.items.map(v);
190
- N((e) => [...e, ...t]), V(e.data.notifications.hasMore), G((e) => e + 50);
206
+ if (n !== U.current) return;
207
+ if (r.data?.notifications) {
208
+ let e = r.data.notifications.items.map(_);
209
+ for (let t of e) W.current.add(t.id);
210
+ j((t) => i(t, e)), R(r.data.notifications.hasMore);
191
211
  }
192
212
  } catch (e) {
193
213
  let t = e instanceof Error ? e.message : "Failed to load more notifications";
194
- console.error("[useNotifications] Failed to load more:", e), z(t);
214
+ console.error("[useNotifications] Failed to load more:", e), I(t);
215
+ } finally {
216
+ H.current = !1, V(!1);
195
217
  }
196
218
  }, [
197
- q,
198
219
  Y,
199
- B,
200
- W
201
- ]), $ = m(async (e) => {
220
+ X,
221
+ L,
222
+ A
223
+ ]), $ = f(async () => {
224
+ let e = U.current;
225
+ G.current += 1;
226
+ let n = G.current;
202
227
  try {
203
- await Y().mutate({
228
+ let r = await X().query({
204
229
  context: t,
205
- mutation: l,
230
+ query: l,
231
+ variables: {
232
+ filter: x(Y),
233
+ limit: 50,
234
+ offset: 0
235
+ },
236
+ fetchPolicy: "network-only"
237
+ });
238
+ if (e !== U.current || n !== G.current || !r.data?.notifications) return;
239
+ let { items: i, total: a, hasMore: o } = r.data.notifications, s = i.map(_), c = ee(q.current, s, o, W.current);
240
+ if (c.continued) {
241
+ for (let e of s) W.current.add(e.id);
242
+ j(c.list);
243
+ } else U.current += 1, W.current = new Set(s.map((e) => e.id)), j(c.list), R(o);
244
+ z(a);
245
+ } catch (e) {
246
+ console.error("[useNotifications] Failed to refresh notifications:", e);
247
+ }
248
+ }, [Y, X]), ue = f(async (e) => {
249
+ try {
250
+ await X().mutate({
251
+ context: t,
252
+ mutation: ne,
206
253
  variables: { id: e }
207
- }), N((t) => t.map((t) => t.id === e ? {
254
+ }), j((t) => t.map((t) => t.id === e ? {
208
255
  ...t,
209
256
  readAt: (/* @__PURE__ */ new Date()).toISOString()
210
- } : t)), A(Math.max(0, k - 1));
257
+ } : t)), O(Math.max(0, D - 1));
211
258
  } catch (e) {
212
- console.error("[useNotifications] Failed to mark as read:", e), z("Failed to mark notification as read");
259
+ console.error("[useNotifications] Failed to mark as read:", e), I("Failed to mark notification as read");
213
260
  }
214
261
  }, [
215
- Y,
216
- A,
217
- k
218
- ]), ee = m(async () => {
262
+ X,
263
+ O,
264
+ D
265
+ ]), de = f(async () => {
219
266
  try {
220
- await Y().mutate({
267
+ await X().mutate({
221
268
  context: t,
222
- mutation: c
223
- }), N((e) => e.map((e) => ({
269
+ mutation: te
270
+ }), j((e) => e.map((e) => ({
224
271
  ...e,
225
272
  readAt: e.readAt || (/* @__PURE__ */ new Date()).toISOString()
226
- }))), A(0);
273
+ }))), O(0);
227
274
  } catch (e) {
228
- console.error("[useNotifications] Failed to mark all as read:", e), z("Failed to mark all notifications as read");
275
+ console.error("[useNotifications] Failed to mark all as read:", e), I("Failed to mark all notifications as read");
229
276
  }
230
- }, [Y, A]), te = m(async (e) => {
277
+ }, [X, O]), fe = f(async (e) => {
231
278
  try {
232
- await Y().mutate({
279
+ await X().mutate({
233
280
  context: t,
234
- mutation: i,
281
+ mutation: o,
235
282
  variables: { id: e }
236
- }), N((t) => t.map((t) => t.id === e ? {
283
+ }), j((t) => t.map((t) => t.id === e ? {
237
284
  ...t,
238
285
  archivedAt: (/* @__PURE__ */ new Date()).toISOString()
239
286
  } : t));
240
287
  } catch (e) {
241
- console.error("[useNotifications] Failed to archive notification:", e), z("Failed to archive notification");
288
+ console.error("[useNotifications] Failed to archive notification:", e), I("Failed to archive notification");
242
289
  }
243
- }, [Y]), ne = m(async (e) => {
290
+ }, [X]), pe = f(async (e) => {
244
291
  try {
245
- await Y().mutate({
292
+ await X().mutate({
246
293
  context: t,
247
- mutation: a,
294
+ mutation: s,
248
295
  variables: { notificationIds: e }
249
296
  });
250
297
  let n = new Set(e);
251
- N((e) => e.map((e) => n.has(e.id) ? {
298
+ j((e) => e.map((e) => n.has(e.id) ? {
252
299
  ...e,
253
300
  archivedAt: (/* @__PURE__ */ new Date()).toISOString()
254
301
  } : e));
255
302
  } catch (e) {
256
- console.error("[useNotifications] Failed to bulk archive:", e), z("Failed to archive notifications");
303
+ console.error("[useNotifications] Failed to bulk archive:", e), I("Failed to archive notifications");
257
304
  }
258
- }, [Y]), re = m(async (e) => {
305
+ }, [X]), me = f(async (e) => {
259
306
  try {
260
- await Y().mutate({
307
+ await X().mutate({
261
308
  context: t,
262
- mutation: f,
309
+ mutation: re,
263
310
  variables: { id: e }
264
- }), N((t) => t.map((t) => t.id === e ? {
311
+ }), j((t) => t.map((t) => t.id === e ? {
265
312
  ...t,
266
313
  archivedAt: void 0
267
314
  } : t));
268
315
  } catch (e) {
269
- console.error("[useNotifications] Failed to unarchive notification:", e), z("Failed to restore notification");
316
+ console.error("[useNotifications] Failed to unarchive notification:", e), I("Failed to restore notification");
270
317
  }
271
- }, [Y]), ie = m(async (e, n) => {
318
+ }, [X]), he = f(async (e, n) => {
272
319
  try {
273
- await Y().mutate({
320
+ await X().mutate({
274
321
  context: t,
275
322
  mutation: d,
276
323
  variables: { input: {
277
324
  notificationId: e,
278
325
  actionId: n
279
326
  } }
280
- }), N((t) => t.map((t) => t.id === e ? {
327
+ }), j((t) => t.map((t) => t.id === e ? {
281
328
  ...t,
282
329
  clickedAt: (/* @__PURE__ */ new Date()).toISOString(),
283
330
  readAt: t.readAt || (/* @__PURE__ */ new Date()).toISOString()
284
331
  } : t));
285
332
  } catch (e) {
286
- console.error("[useNotifications] Failed to record notification action:", e), z("Failed to record notification action");
333
+ console.error("[useNotifications] Failed to record notification action:", e), I("Failed to record notification action");
287
334
  }
288
- }, [Y]);
289
- return h(() => {
290
- X();
291
- }, [X]), h(() => {
335
+ }, [X]);
336
+ return p(() => {
292
337
  Z();
293
- }, [Z]), h(() => {
294
- if (!w?.id) return;
295
- let e = Y(), t = e.subscribe({
338
+ }, [Z]), p(() => {
339
+ Q();
340
+ }, [Q]), p(() => {
341
+ if (!C?.id) return;
342
+ let e = X(), t = e.subscribe({
296
343
  query: u,
297
- variables: { userId: w.id }
344
+ variables: { userId: C.id }
298
345
  }).subscribe({
299
346
  next: (e) => {
300
347
  let t = e.data?.notificationReceived;
301
348
  if (!t) return;
302
- let n = v(t);
303
- N((e) => [n, ...e.filter((e) => e.id !== n.id)]), T?.emit("notification:received", { notification: n });
349
+ let n = _(t);
350
+ j((e) => [n, ...e.filter((e) => e.id !== n.id)]), w?.emit("notification:received", { notification: n });
304
351
  },
305
352
  error: (e) => {
306
353
  console.error("[useNotifications] Notification subscription failed:", e);
307
354
  }
308
355
  }), n = e.subscribe({
309
- query: p,
310
- variables: { userId: w.id }
356
+ query: ie,
357
+ variables: { userId: C.id }
311
358
  }).subscribe({
312
359
  next: (e) => {
313
360
  let t = e.data?.unreadCountUpdated;
314
- typeof t == "number" && (A(t), T?.emit("notification:unread-count-updated", { unreadCount: t }));
361
+ typeof t == "number" && (O(t), w?.emit("notification:unread-count-updated", { unreadCount: t }));
315
362
  },
316
363
  error: (e) => {
317
364
  console.error("[useNotifications] Unread count subscription failed:", e);
@@ -321,38 +368,39 @@ function w(y = {}) {
321
368
  t.unsubscribe(), n.unsubscribe();
322
369
  };
323
370
  }, [
324
- w?.id,
325
- T,
326
- Y,
327
- A
328
- ]), h(() => {
371
+ C?.id,
372
+ w,
373
+ X,
374
+ O
375
+ ]), p(() => {
329
376
  let e = () => {
330
- document.visibilityState === "visible" && (X(), Z());
377
+ document.visibilityState === "visible" && ($(), Q());
331
378
  };
332
379
  return document.addEventListener("visibilitychange", e), () => document.removeEventListener("visibilitychange", e);
333
- }, [X, Z]), {
334
- notifications: g(() => M.filter((e) => e.deliveryStatus === "UNSUBSCRIBED" || e.status === "ARCHIVED" && !e.archivedAt ? !1 : K.archived === !0 ? !!e.archivedAt : K.archived !== !1 || !e.archivedAt), [K.archived, M]),
335
- allNotifications: M,
336
- unreadCount: k,
337
- selectedNotification: P,
338
- notificationFilter: K,
339
- isLoading: I,
340
- error: R,
341
- hasMore: B,
342
- total: H,
343
- markAsRead: $,
344
- markAllAsRead: ee,
345
- archiveNotification: te,
346
- bulkArchive: ne,
347
- unarchiveNotification: re,
348
- recordAction: ie,
349
- setSelectedNotification: F,
350
- updateFilter: D,
351
- resetFilter: O,
352
- refetch: X,
353
- loadMore: Q
380
+ }, [$, Q]), {
381
+ notifications: m(() => A.filter((e) => e.deliveryStatus === "UNSUBSCRIBED" || e.status === "ARCHIVED" && !e.archivedAt ? !1 : J.archived === !0 ? !!e.archivedAt : J.archived !== !1 || !e.archivedAt), [J.archived, A]),
382
+ allNotifications: A,
383
+ unreadCount: D,
384
+ selectedNotification: M,
385
+ notificationFilter: J,
386
+ isLoading: P,
387
+ isLoadingMore: B,
388
+ error: oe,
389
+ hasMore: L,
390
+ total: se,
391
+ markAsRead: ue,
392
+ markAllAsRead: de,
393
+ archiveNotification: fe,
394
+ bulkArchive: pe,
395
+ unarchiveNotification: me,
396
+ recordAction: he,
397
+ setSelectedNotification: N,
398
+ updateFilter: ae,
399
+ resetFilter: E,
400
+ refetch: Z,
401
+ loadMore: le
354
402
  };
355
403
  }
356
- export { w as useNotifications };
404
+ export { C as useNotifications };
357
405
 
358
406
  //# sourceMappingURL=useNotifications.js.map