@notificationapi/react 0.0.25 → 0.0.26

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.
@@ -523,59 +523,57 @@ function Le() {
523
523
  const e = ie(oe);
524
524
  if (!e || !e.preferences)
525
525
  return null;
526
- const n = e.preferences.notifications.map(
527
- (t) => {
528
- var f, r, s;
529
- const o = (f = e.preferences) == null ? void 0 : f.preferences.filter(
530
- (a) => a.notificationId === t.notificationId && !a.subNotificationId
531
- ), i = (r = e.preferences) == null ? void 0 : r.preferences.filter(
532
- (a) => a.notificationId === t.notificationId && a.subNotificationId
533
- ), c = (s = e.preferences) == null ? void 0 : s.subNotifications.filter(
534
- (a) => i == null ? void 0 : i.find(
535
- (v) => v.subNotificationId === a.subNotificationId
536
- )
537
- );
538
- return {
539
- label: t.title,
540
- key: t.notificationId,
541
- children: /* @__PURE__ */ ae(G, { children: [
542
- /* @__PURE__ */ B(
543
- L,
544
- {
545
- notification: t,
546
- preferences: o || [],
547
- updateDelivery: e.updateDelivery
548
- },
549
- t.notificationId
550
- ),
551
- c == null ? void 0 : c.map((a) => /* @__PURE__ */ B(
552
- q,
553
- {
554
- bordered: !1,
555
- items: [
556
- {
557
- label: a.title,
558
- key: a.subNotificationId,
559
- children: /* @__PURE__ */ B(
560
- L,
561
- {
562
- notification: t,
563
- preferences: i || [],
564
- updateDelivery: e.updateDelivery,
565
- subNotificationId: a.subNotificationId
566
- },
567
- a.subNotificationId
568
- )
569
- }
570
- ],
571
- defaultActiveKey: []
572
- },
573
- a.subNotificationId
574
- ))
575
- ] })
576
- };
577
- }
578
- );
526
+ const n = e.preferences.notifications.sort((t, o) => t.title.localeCompare(o.title)).map((t) => {
527
+ var f, r, s;
528
+ const o = (f = e.preferences) == null ? void 0 : f.preferences.filter(
529
+ (a) => a.notificationId === t.notificationId && !a.subNotificationId
530
+ ), i = (r = e.preferences) == null ? void 0 : r.preferences.filter(
531
+ (a) => a.notificationId === t.notificationId && a.subNotificationId
532
+ ), c = (s = e.preferences) == null ? void 0 : s.subNotifications.filter(
533
+ (a) => i == null ? void 0 : i.find(
534
+ (v) => v.subNotificationId === a.subNotificationId
535
+ )
536
+ );
537
+ return {
538
+ label: t.title,
539
+ key: t.notificationId,
540
+ children: /* @__PURE__ */ ae(G, { children: [
541
+ /* @__PURE__ */ B(
542
+ L,
543
+ {
544
+ notification: t,
545
+ preferences: o || [],
546
+ updateDelivery: e.updateDelivery
547
+ },
548
+ t.notificationId
549
+ ),
550
+ c == null ? void 0 : c.map((a) => /* @__PURE__ */ B(
551
+ q,
552
+ {
553
+ bordered: !1,
554
+ items: [
555
+ {
556
+ label: a.title,
557
+ key: a.subNotificationId,
558
+ children: /* @__PURE__ */ B(
559
+ L,
560
+ {
561
+ notification: t,
562
+ preferences: i || [],
563
+ updateDelivery: e.updateDelivery,
564
+ subNotificationId: a.subNotificationId
565
+ },
566
+ a.subNotificationId
567
+ )
568
+ }
569
+ ],
570
+ defaultActiveKey: []
571
+ },
572
+ a.subNotificationId
573
+ ))
574
+ ] })
575
+ };
576
+ });
579
577
  return /* @__PURE__ */ B(G, { children: /* @__PURE__ */ B(q, { items: n, defaultActiveKey: [] }) });
580
578
  }
581
579
  export {
@@ -10,6 +10,12 @@ export type Context = {
10
10
  markAsUnarchived: (ids: string[] | 'ALL') => void;
11
11
  markAsClicked: (ids: string[]) => void;
12
12
  updateDelivery: (notificationId: string, channel: Channels, delivery: DeliveryOptionsForEmail | DeliveryOptionsForInappWeb | BaseDeliveryOptions, subNotificationId?: string) => void;
13
+ updateDeliveries: (params: {
14
+ notificationId: string;
15
+ channel: Channels;
16
+ delivery: DeliveryOptionsForEmail | DeliveryOptionsForInappWeb | BaseDeliveryOptions;
17
+ subNotificationId?: string;
18
+ }[]) => void;
13
19
  };
14
20
  export declare const NotificationAPIContext: import('react').Context<Context | undefined>;
15
21
  type Props = {
@@ -1,10 +1,10 @@
1
- import { jsx as W } from "react/jsx-runtime";
2
- import { createContext as H, useState as N, useCallback as A, useMemo as B, useRef as m, useEffect as x, useContext as G } from "react";
3
- const J = async (e, c, d, r, f, I, h) => {
4
- const g = _(r, f, I), l = await fetch(
1
+ import { jsx as H } from "react/jsx-runtime";
2
+ import { createContext as B, useState as N, useCallback as A, useMemo as G, useRef as m, useEffect as L, useContext as J } from "react";
3
+ const _ = async (e, c, a, r, f, I, h) => {
4
+ const g = z(r, f, I), l = await fetch(
5
5
  `https://${c}/${r}/users/${encodeURIComponent(
6
6
  f
7
- )}/${d}`,
7
+ )}/${a}`,
8
8
  {
9
9
  method: e,
10
10
  body: JSON.stringify(h),
@@ -18,7 +18,7 @@ const J = async (e, c, d, r, f, I, h) => {
18
18
  } catch {
19
19
  return;
20
20
  }
21
- }, _ = (e, c, d) => btoa(d ? e + ":" + c + ":" + d : e + ":" + c), L = {
21
+ }, z = (e, c, a) => btoa(a ? e + ":" + c + ":" + a : e + ":" + c), M = {
22
22
  host: "api.notificationapi.com",
23
23
  websocketHost: "ws.notificationapi.com",
24
24
  userId: "",
@@ -32,22 +32,22 @@ const J = async (e, c, d, r, f, I, h) => {
32
32
  keepWebSocketAliveForSeconds: 86400
33
33
  // 24 hours
34
34
  }, i = {
35
- config: L,
35
+ config: M,
36
36
  init: function(e) {
37
- return this.config = { ...L, ...e }, {
37
+ return this.config = { ...M, ...e }, {
38
38
  ...this
39
39
  };
40
40
  },
41
41
  rest: {
42
- generic: function(e, c, d) {
43
- return J(
42
+ generic: function(e, c, a) {
43
+ return _(
44
44
  e,
45
45
  i.config.host,
46
46
  c,
47
47
  i.config.clientId,
48
48
  i.config.userId,
49
49
  i.config.hashedUserId,
50
- d
50
+ a
51
51
  );
52
52
  },
53
53
  getNotifications: function(e, c) {
@@ -79,9 +79,9 @@ const J = async (e, c, d, r, f, I, h) => {
79
79
  connect: function() {
80
80
  let e = `wss://${i.config.websocketHost}?userId=${encodeURIComponent(i.config.userId)}&envId=${i.config.clientId}`;
81
81
  return i.config.hashedUserId && (e += `&userIdHash=${encodeURIComponent(i.config.hashedUserId)}`), i.websocket.object = new WebSocket(e), i.websocket.object.onmessage = (c) => {
82
- const d = JSON.parse(c.data);
83
- if (!(!d || !d.route) && d.route === "inapp_web/new_notifications") {
84
- const r = d;
82
+ const a = JSON.parse(c.data);
83
+ if (!(!a || !a.route) && a.route === "inapp_web/new_notifications") {
84
+ const r = a;
85
85
  i.config.onNewInAppNotifications && i.config.onNewInAppNotifications(
86
86
  r.payload.notifications
87
87
  );
@@ -106,7 +106,7 @@ const J = async (e, c, d, r, f, I, h) => {
106
106
  });
107
107
  },
108
108
  getInAppNotifications: async (e) => {
109
- const c = e.maxCountNeeded || i.config.getInAppDefaultCount, d = e.oldestNeeded || i.config.getInAppDefaultOldest;
109
+ const c = e.maxCountNeeded || i.config.getInAppDefaultCount, a = e.oldestNeeded || i.config.getInAppDefaultOldest;
110
110
  let r = [], f = e.before, I = !0, h = !0;
111
111
  for (; h; ) {
112
112
  const g = (await i.rest.getNotifications(
@@ -118,7 +118,7 @@ const J = async (e, c, d, r, f, I, h) => {
118
118
  f = g.reduce(
119
119
  (l, p) => l < p.date ? l : p.date,
120
120
  e.before
121
- ), r = [...r, ...g], I = g.length > 0, h = !0, (!I || r.length >= c || f < d) && (h = !1);
121
+ ), r = [...r, ...g], I = g.length > 0, h = !0, (!I || r.length >= c || f < a) && (h = !1);
122
122
  }
123
123
  return {
124
124
  items: r,
@@ -134,10 +134,10 @@ const J = async (e, c, d, r, f, I, h) => {
134
134
  },
135
135
  getPreferences: async () => i.rest.getPreferences(),
136
136
  updateDeliveryOption: async (e) => i.rest.postPreferences([e])
137
- }, M = H(
137
+ }, U = B(
138
138
  void 0
139
- ), z = (e) => {
140
- const d = {
139
+ ), F = (e) => {
140
+ const a = {
141
141
  ...{
142
142
  apiURL: "https://api.notificationapi.com",
143
143
  wsURL: "wss://ws.notificationapi.com",
@@ -148,10 +148,10 @@ const J = async (e, c, d, r, f, I, h) => {
148
148
  }, [r, f] = N(), [I, h] = N(), [g, l] = N(!1), [p, D] = N((/* @__PURE__ */ new Date()).toISOString()), [b, v] = N(!0), S = A((o) => {
149
149
  const s = (/* @__PURE__ */ new Date()).toISOString();
150
150
  f((t) => (o = o.filter((n) => !(n.expDate && new Date(n.expDate * 1e3).toISOString() > s || n.date > s)), t ? [
151
- ...o.filter((n) => !t.find((a) => a.id === n.id)),
151
+ ...o.filter((n) => !t.find((d) => d.id === n.id)),
152
152
  ...t
153
153
  ] : o));
154
- }, []), u = B(() => i.init({
154
+ }, []), u = G(() => i.init({
155
155
  clientId: e.clientId,
156
156
  userId: e.userId,
157
157
  hashedUserId: e.hashedUserId,
@@ -163,44 +163,44 @@ const J = async (e, c, d, r, f, I, h) => {
163
163
  e.userId,
164
164
  e.hashedUserId,
165
165
  S
166
- ]), y = A(
166
+ ]), P = A(
167
167
  async (o, s) => {
168
168
  const t = await u.rest.getNotifications(o, s);
169
169
  D(t.oldestReceived), v(t.couldLoadMore), S(t.notifications);
170
170
  },
171
171
  [S, u.rest]
172
- ), O = m(b), P = m(g), C = m(p);
173
- x(() => {
174
- O.current = b, P.current = g, C.current = p;
172
+ ), y = m(b), C = m(g), O = m(p);
173
+ L(() => {
174
+ y.current = b, C.current = g, O.current = p;
175
175
  }, [b, g, p]);
176
176
  const k = A(
177
177
  async (o) => {
178
- if (!(!o && (!O.current || P.current))) {
178
+ if (!(!o && (!y.current || C.current))) {
179
179
  l(!0);
180
180
  try {
181
- await y(
182
- o ? (/* @__PURE__ */ new Date()).toISOString() : C.current,
183
- o ? d.initialLoadMaxCount : 1e3
181
+ await P(
182
+ o ? (/* @__PURE__ */ new Date()).toISOString() : O.current,
183
+ o ? a.initialLoadMaxCount : 1e3
184
184
  );
185
185
  } finally {
186
186
  l(!1);
187
187
  }
188
188
  }
189
189
  },
190
- [d.initialLoadMaxCount, y]
191
- ), U = async (o) => {
190
+ [a.initialLoadMaxCount, P]
191
+ ), R = async (o) => {
192
192
  if (!r)
193
193
  return;
194
194
  const s = (/* @__PURE__ */ new Date()).toISOString(), t = r.filter((n) => o.includes(n.id) && !n.clicked).map((n) => n.id);
195
195
  u.updateInAppNotifications({ ids: t, clicked: !0 }), f((n) => {
196
196
  if (!n)
197
197
  return [];
198
- const a = [...n];
199
- return a.filter((w) => t.includes(w.id)).forEach((w) => {
198
+ const d = [...n];
199
+ return d.filter((w) => t.includes(w.id)).forEach((w) => {
200
200
  w.clicked = s;
201
- }), a;
201
+ }), d;
202
202
  });
203
- }, R = async () => {
203
+ }, $ = async () => {
204
204
  if (!r)
205
205
  return;
206
206
  const o = (/* @__PURE__ */ new Date()).toISOString(), s = r.filter((t) => !t.opened || !t.seen).map((t) => t.id);
@@ -211,11 +211,11 @@ const J = async (e, c, d, r, f, I, h) => {
211
211
  if (!t)
212
212
  return [];
213
213
  const n = [...t];
214
- return n.filter((a) => s.includes(a.id)).forEach((a) => {
215
- a.opened = o, a.seen = !0;
214
+ return n.filter((d) => s.includes(d.id)).forEach((d) => {
215
+ d.opened = o, d.seen = !0;
216
216
  }), n;
217
217
  }));
218
- }, $ = async (o) => {
218
+ }, E = async (o) => {
219
219
  if (!r)
220
220
  return;
221
221
  const s = r.filter((t) => t.archived && (o === "ALL" || o.includes(t.id))).map((t) => t.id);
@@ -226,58 +226,61 @@ const J = async (e, c, d, r, f, I, h) => {
226
226
  if (!t)
227
227
  return [];
228
228
  const n = [...t];
229
- return n.filter((a) => s.includes(a.id)).forEach((a) => {
230
- a.archived = void 0;
229
+ return n.filter((d) => s.includes(d.id)).forEach((d) => {
230
+ d.archived = void 0;
231
231
  }), n;
232
232
  }));
233
- }, E = async (o) => {
233
+ }, j = async (o) => {
234
234
  if (!r)
235
235
  return;
236
236
  const s = (/* @__PURE__ */ new Date()).toISOString(), t = r.filter((n) => !n.archived && (o === "ALL" || o.includes(n.id))).map((n) => n.id);
237
237
  t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((n) => {
238
238
  if (!n)
239
239
  return [];
240
- const a = [...n];
241
- return a.filter((w) => t.includes(w.id)).forEach((w) => {
240
+ const d = [...n];
241
+ return d.filter((w) => t.includes(w.id)).forEach((w) => {
242
242
  w.archived = s;
243
- }), a;
243
+ }), d;
244
244
  }));
245
- }, j = (o, s, t, n) => {
246
- u.updateDeliveryOption({
245
+ }, T = (o, s, t, n) => x([
246
+ {
247
247
  notificationId: o,
248
248
  channel: s,
249
249
  delivery: t,
250
250
  subNotificationId: n
251
- }).then(() => {
252
- u.getPreferences().then((a) => {
253
- h(a);
251
+ }
252
+ ]), x = (o) => {
253
+ u.rest.postPreferences(o).then(() => {
254
+ u.getPreferences().then((s) => {
255
+ h(s);
254
256
  });
255
257
  });
256
258
  };
257
- x(() => {
259
+ L(() => {
258
260
  f([]), l(!1), h(void 0), D((/* @__PURE__ */ new Date()).toISOString()), v(!0), k(!0), u.openWebSocket(), u.getPreferences().then((o) => {
259
261
  h(o);
260
262
  });
261
263
  }, [u, k]);
262
- const T = {
264
+ const W = {
263
265
  notifications: r,
264
266
  preferences: I,
265
267
  loadNotifications: k,
266
- markAsOpened: R,
267
- markAsArchived: E,
268
- markAsUnarchived: $,
269
- markAsClicked: U,
270
- updateDelivery: j
268
+ markAsOpened: $,
269
+ markAsArchived: j,
270
+ markAsUnarchived: E,
271
+ markAsClicked: R,
272
+ updateDelivery: T,
273
+ updateDeliveries: x
271
274
  };
272
- return /* @__PURE__ */ W(M.Provider, { value: T, children: e.children });
273
- }, F = () => {
274
- const e = G(M);
275
+ return /* @__PURE__ */ H(U.Provider, { value: W, children: e.children });
276
+ }, q = () => {
277
+ const e = J(U);
275
278
  if (!e)
276
279
  throw new Error("useMyContext must be used within a MyProvider");
277
280
  return e;
278
281
  };
279
- z.useNotificationAPIContext = F;
282
+ F.useNotificationAPIContext = q;
280
283
  export {
281
- M as NotificationAPIContext,
282
- z as NotificationAPIProvider
284
+ U as NotificationAPIContext,
285
+ F as NotificationAPIProvider
283
286
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@notificationapi/react",
3
3
  "private": false,
4
- "version": "0.0.25",
4
+ "version": "0.0.26",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",