@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
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
)
|
|
532
|
-
|
|
533
|
-
)
|
|
534
|
-
(
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
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
|
|
2
|
-
import { createContext as
|
|
3
|
-
const
|
|
4
|
-
const g =
|
|
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
|
-
)}/${
|
|
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
|
-
},
|
|
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:
|
|
35
|
+
config: M,
|
|
36
36
|
init: function(e) {
|
|
37
|
-
return this.config = { ...
|
|
37
|
+
return this.config = { ...M, ...e }, {
|
|
38
38
|
...this
|
|
39
39
|
};
|
|
40
40
|
},
|
|
41
41
|
rest: {
|
|
42
|
-
generic: function(e, c,
|
|
43
|
-
return
|
|
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
|
-
|
|
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
|
|
83
|
-
if (!(!
|
|
84
|
-
const r =
|
|
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,
|
|
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 <
|
|
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
|
-
},
|
|
137
|
+
}, U = B(
|
|
138
138
|
void 0
|
|
139
|
-
),
|
|
140
|
-
const
|
|
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((
|
|
151
|
+
...o.filter((n) => !t.find((d) => d.id === n.id)),
|
|
152
152
|
...t
|
|
153
153
|
] : o));
|
|
154
|
-
}, []), u =
|
|
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
|
-
]),
|
|
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
|
-
),
|
|
173
|
-
|
|
174
|
-
|
|
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 && (!
|
|
178
|
+
if (!(!o && (!y.current || C.current))) {
|
|
179
179
|
l(!0);
|
|
180
180
|
try {
|
|
181
|
-
await
|
|
182
|
-
o ? (/* @__PURE__ */ new Date()).toISOString() :
|
|
183
|
-
o ?
|
|
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
|
-
[
|
|
191
|
-
),
|
|
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
|
|
199
|
-
return
|
|
198
|
+
const d = [...n];
|
|
199
|
+
return d.filter((w) => t.includes(w.id)).forEach((w) => {
|
|
200
200
|
w.clicked = s;
|
|
201
|
-
}),
|
|
201
|
+
}), d;
|
|
202
202
|
});
|
|
203
|
-
},
|
|
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((
|
|
215
|
-
|
|
214
|
+
return n.filter((d) => s.includes(d.id)).forEach((d) => {
|
|
215
|
+
d.opened = o, d.seen = !0;
|
|
216
216
|
}), n;
|
|
217
217
|
}));
|
|
218
|
-
},
|
|
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((
|
|
230
|
-
|
|
229
|
+
return n.filter((d) => s.includes(d.id)).forEach((d) => {
|
|
230
|
+
d.archived = void 0;
|
|
231
231
|
}), n;
|
|
232
232
|
}));
|
|
233
|
-
},
|
|
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
|
|
241
|
-
return
|
|
240
|
+
const d = [...n];
|
|
241
|
+
return d.filter((w) => t.includes(w.id)).forEach((w) => {
|
|
242
242
|
w.archived = s;
|
|
243
|
-
}),
|
|
243
|
+
}), d;
|
|
244
244
|
}));
|
|
245
|
-
},
|
|
246
|
-
|
|
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
|
-
}
|
|
252
|
-
|
|
253
|
-
|
|
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
|
-
|
|
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
|
|
264
|
+
const W = {
|
|
263
265
|
notifications: r,
|
|
264
266
|
preferences: I,
|
|
265
267
|
loadNotifications: k,
|
|
266
|
-
markAsOpened:
|
|
267
|
-
markAsArchived:
|
|
268
|
-
markAsUnarchived:
|
|
269
|
-
markAsClicked:
|
|
270
|
-
updateDelivery:
|
|
268
|
+
markAsOpened: $,
|
|
269
|
+
markAsArchived: j,
|
|
270
|
+
markAsUnarchived: E,
|
|
271
|
+
markAsClicked: R,
|
|
272
|
+
updateDelivery: T,
|
|
273
|
+
updateDeliveries: x
|
|
271
274
|
};
|
|
272
|
-
return /* @__PURE__ */
|
|
273
|
-
},
|
|
274
|
-
const e =
|
|
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
|
-
|
|
282
|
+
F.useNotificationAPIContext = q;
|
|
280
283
|
export {
|
|
281
|
-
|
|
282
|
-
|
|
284
|
+
U as NotificationAPIContext,
|
|
285
|
+
F as NotificationAPIProvider
|
|
283
286
|
};
|