@notificationapi/react 0.0.30 → 0.0.31
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,10 +1,10 @@
|
|
|
1
1
|
import { jsx as B } from "react/jsx-runtime";
|
|
2
|
-
import { createContext as z, useState as N, useCallback as
|
|
3
|
-
const J = async (e, s,
|
|
2
|
+
import { createContext as z, useState as N, useCallback as m, useMemo as F, useRef as k, useEffect as U, useContext as G } from "react";
|
|
3
|
+
const J = async (e, s, i, a, f, w, p) => {
|
|
4
4
|
const h = _(a, f, w), l = await fetch(
|
|
5
5
|
`https://${s}/${a}/users/${encodeURIComponent(
|
|
6
6
|
f
|
|
7
|
-
)}/${
|
|
7
|
+
)}/${i}`,
|
|
8
8
|
{
|
|
9
9
|
method: e,
|
|
10
10
|
body: JSON.stringify(p),
|
|
@@ -18,7 +18,7 @@ const J = async (e, s, c, a, f, w, p) => {
|
|
|
18
18
|
} catch {
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
|
-
}, _ = (e, s,
|
|
21
|
+
}, _ = (e, s, i) => btoa(i ? e + ":" + s + ":" + i : e + ":" + s), M = {
|
|
22
22
|
host: "api.notificationapi.com",
|
|
23
23
|
websocketHost: "ws.notificationapi.com",
|
|
24
24
|
userId: "",
|
|
@@ -39,7 +39,7 @@ const J = async (e, s, c, a, f, w, p) => {
|
|
|
39
39
|
};
|
|
40
40
|
},
|
|
41
41
|
rest: {
|
|
42
|
-
generic: function(e, s,
|
|
42
|
+
generic: function(e, s, i) {
|
|
43
43
|
return J(
|
|
44
44
|
e,
|
|
45
45
|
n.config.host,
|
|
@@ -47,7 +47,7 @@ const J = async (e, s, c, a, f, w, p) => {
|
|
|
47
47
|
n.config.clientId,
|
|
48
48
|
n.config.userId,
|
|
49
49
|
n.config.hashedUserId,
|
|
50
|
-
|
|
50
|
+
i
|
|
51
51
|
);
|
|
52
52
|
},
|
|
53
53
|
// The functions below are nice wrappers over the generic
|
|
@@ -85,9 +85,9 @@ const J = async (e, s, c, a, f, w, p) => {
|
|
|
85
85
|
connect: function() {
|
|
86
86
|
let e = `wss://${n.config.websocketHost}?userId=${encodeURIComponent(n.config.userId)}&envId=${n.config.clientId}`;
|
|
87
87
|
return n.config.hashedUserId && (e += `&userIdHash=${encodeURIComponent(n.config.hashedUserId)}`), n.websocket.object = new WebSocket(e), n.websocket.object.onmessage = (s) => {
|
|
88
|
-
const
|
|
89
|
-
if (!(!
|
|
90
|
-
const a =
|
|
88
|
+
const i = JSON.parse(s.data);
|
|
89
|
+
if (!(!i || !i.route) && i.route === "inapp_web/new_notifications") {
|
|
90
|
+
const a = i;
|
|
91
91
|
n.config.onNewInAppNotifications && n.config.onNewInAppNotifications(
|
|
92
92
|
a.payload.notifications
|
|
93
93
|
);
|
|
@@ -115,7 +115,7 @@ const J = async (e, s, c, a, f, w, p) => {
|
|
|
115
115
|
// They may or may not do additional tasks.
|
|
116
116
|
// e.g. identify simply maps to postUsers
|
|
117
117
|
getInAppNotifications: async (e) => {
|
|
118
|
-
const s = e.maxCountNeeded || n.config.getInAppDefaultCount,
|
|
118
|
+
const s = e.maxCountNeeded || n.config.getInAppDefaultCount, i = e.oldestNeeded || n.config.getInAppDefaultOldest;
|
|
119
119
|
let a = [], f = e.before, w = !0, p = !0;
|
|
120
120
|
for (; p; ) {
|
|
121
121
|
const h = (await n.rest.getNotifications(
|
|
@@ -127,7 +127,7 @@ const J = async (e, s, c, a, f, w, p) => {
|
|
|
127
127
|
f = h.reduce(
|
|
128
128
|
(l, g) => l < g.date ? l : g.date,
|
|
129
129
|
e.before
|
|
130
|
-
), a = [...a, ...h], w = h.length > 0, p = !0, (!w || a.length >= s || f <
|
|
130
|
+
), a = [...a, ...h], w = h.length > 0, p = !0, (!w || a.length >= s || f < i) && (p = !1);
|
|
131
131
|
}
|
|
132
132
|
return {
|
|
133
133
|
items: a,
|
|
@@ -153,7 +153,7 @@ const J = async (e, s, c, a, f, w, p) => {
|
|
|
153
153
|
}, E = z(
|
|
154
154
|
void 0
|
|
155
155
|
), q = (e) => {
|
|
156
|
-
const
|
|
156
|
+
const i = {
|
|
157
157
|
...{
|
|
158
158
|
apiURL: "https://api.notificationapi.com",
|
|
159
159
|
wsURL: "wss://ws.notificationapi.com",
|
|
@@ -164,65 +164,70 @@ const J = async (e, s, c, a, f, w, p) => {
|
|
|
164
164
|
},
|
|
165
165
|
...e,
|
|
166
166
|
user: "userId" in e ? { id: e.userId } : e.user
|
|
167
|
-
}, [a, f] = N(), [w, p] = N(), [h, l] = N(!1), [g,
|
|
168
|
-
|
|
167
|
+
}, [a, f] = N(), [w, p] = N(), [h, l] = N(!1), [g, y] = N((/* @__PURE__ */ new Date()).toISOString()), [b, P] = N(!0), D = m(() => {
|
|
168
|
+
i.playSoundOnNewNotification && new Audio(i.newNotificationSoundPath).play().catch((r) => {
|
|
169
169
|
console.log("Failed to play new notification sound:", r);
|
|
170
170
|
});
|
|
171
|
-
}, [
|
|
171
|
+
}, [i.newNotificationSoundPath, i.playSoundOnNewNotification]), S = m((o) => {
|
|
172
172
|
const r = (/* @__PURE__ */ new Date()).toISOString();
|
|
173
|
-
f((t) => (
|
|
174
|
-
...
|
|
173
|
+
f((t) => (o = o.filter((c) => !(c.expDate && new Date(c.expDate * 1e3).toISOString() > r || c.date > r)), t ? [
|
|
174
|
+
...o.filter((c) => !t.find((d) => d.id === c.id)),
|
|
175
175
|
...t
|
|
176
|
-
] :
|
|
176
|
+
] : o));
|
|
177
177
|
}, []), u = F(() => {
|
|
178
|
-
const
|
|
179
|
-
clientId:
|
|
180
|
-
userId:
|
|
181
|
-
hashedUserId:
|
|
178
|
+
const o = n.init({
|
|
179
|
+
clientId: i.clientId,
|
|
180
|
+
userId: i.user.id,
|
|
181
|
+
hashedUserId: i.hashedUserId,
|
|
182
182
|
onNewInAppNotifications: (r) => {
|
|
183
183
|
D(), S(r);
|
|
184
184
|
}
|
|
185
185
|
});
|
|
186
|
-
return
|
|
186
|
+
return o.identify({
|
|
187
|
+
email: i.user.email,
|
|
188
|
+
number: i.user.number
|
|
189
|
+
}), o;
|
|
187
190
|
}, [
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
+
i.clientId,
|
|
192
|
+
i.user.id,
|
|
193
|
+
i.user.email,
|
|
194
|
+
i.user.number,
|
|
195
|
+
i.hashedUserId,
|
|
191
196
|
S,
|
|
192
197
|
D
|
|
193
|
-
]), O =
|
|
194
|
-
async (
|
|
195
|
-
const t = await u.rest.getNotifications(
|
|
196
|
-
|
|
198
|
+
]), O = m(
|
|
199
|
+
async (o, r) => {
|
|
200
|
+
const t = await u.rest.getNotifications(o, r);
|
|
201
|
+
y(t.oldestReceived), P(t.couldLoadMore), S(t.notifications);
|
|
197
202
|
},
|
|
198
203
|
[S, u.rest]
|
|
199
|
-
), v =
|
|
204
|
+
), v = k(b), C = k(h), x = k(g);
|
|
200
205
|
U(() => {
|
|
201
206
|
v.current = b, C.current = h, x.current = g;
|
|
202
207
|
}, [b, h, g]);
|
|
203
|
-
const
|
|
204
|
-
async (
|
|
205
|
-
if (!(!
|
|
208
|
+
const A = m(
|
|
209
|
+
async (o) => {
|
|
210
|
+
if (!(!o && (!v.current || C.current))) {
|
|
206
211
|
l(!0);
|
|
207
212
|
try {
|
|
208
213
|
await O(
|
|
209
|
-
|
|
210
|
-
|
|
214
|
+
o ? (/* @__PURE__ */ new Date()).toISOString() : x.current,
|
|
215
|
+
o ? i.initialLoadMaxCount : 1e3
|
|
211
216
|
);
|
|
212
217
|
} finally {
|
|
213
218
|
l(!1);
|
|
214
219
|
}
|
|
215
220
|
}
|
|
216
221
|
},
|
|
217
|
-
[
|
|
218
|
-
), R = async (
|
|
222
|
+
[i.initialLoadMaxCount, O]
|
|
223
|
+
), R = async (o) => {
|
|
219
224
|
if (!a)
|
|
220
225
|
return;
|
|
221
|
-
const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((
|
|
222
|
-
u.updateInAppNotifications({ ids: t, clicked: !0 }), f((
|
|
223
|
-
if (!
|
|
226
|
+
const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((c) => o.includes(c.id) && !c.clicked).map((c) => c.id);
|
|
227
|
+
u.updateInAppNotifications({ ids: t, clicked: !0 }), f((c) => {
|
|
228
|
+
if (!c)
|
|
224
229
|
return [];
|
|
225
|
-
const d = [...
|
|
230
|
+
const d = [...c];
|
|
226
231
|
return d.filter((I) => t.includes(I.id)).forEach((I) => {
|
|
227
232
|
I.clicked = r;
|
|
228
233
|
}), d;
|
|
@@ -230,68 +235,68 @@ const J = async (e, s, c, a, f, w, p) => {
|
|
|
230
235
|
}, $ = async () => {
|
|
231
236
|
if (!a)
|
|
232
237
|
return;
|
|
233
|
-
const
|
|
238
|
+
const o = (/* @__PURE__ */ new Date()).toISOString(), r = a.filter((t) => !t.opened || !t.seen).map((t) => t.id);
|
|
234
239
|
r.length !== 0 && (u.updateInAppNotifications({
|
|
235
240
|
ids: r,
|
|
236
241
|
opened: !0
|
|
237
242
|
}), f((t) => {
|
|
238
243
|
if (!t)
|
|
239
244
|
return [];
|
|
240
|
-
const
|
|
241
|
-
return
|
|
242
|
-
d.opened =
|
|
243
|
-
}),
|
|
245
|
+
const c = [...t];
|
|
246
|
+
return c.filter((d) => r.includes(d.id)).forEach((d) => {
|
|
247
|
+
d.opened = o, d.seen = !0;
|
|
248
|
+
}), c;
|
|
244
249
|
}));
|
|
245
|
-
}, j = async (
|
|
250
|
+
}, j = async (o) => {
|
|
246
251
|
if (!a)
|
|
247
252
|
return;
|
|
248
|
-
const r = a.filter((t) => t.archived && (
|
|
253
|
+
const r = a.filter((t) => t.archived && (o === "ALL" || o.includes(t.id))).map((t) => t.id);
|
|
249
254
|
r.length !== 0 && (u.updateInAppNotifications({
|
|
250
255
|
ids: r,
|
|
251
256
|
archived: !1
|
|
252
257
|
}), f((t) => {
|
|
253
258
|
if (!t)
|
|
254
259
|
return [];
|
|
255
|
-
const
|
|
256
|
-
return
|
|
260
|
+
const c = [...t];
|
|
261
|
+
return c.filter((d) => r.includes(d.id)).forEach((d) => {
|
|
257
262
|
d.archived = void 0;
|
|
258
|
-
}),
|
|
263
|
+
}), c;
|
|
259
264
|
}));
|
|
260
|
-
}, T = async (
|
|
265
|
+
}, T = async (o) => {
|
|
261
266
|
if (!a)
|
|
262
267
|
return;
|
|
263
|
-
const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((
|
|
264
|
-
t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((
|
|
265
|
-
if (!
|
|
268
|
+
const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((c) => !c.archived && (o === "ALL" || o.includes(c.id))).map((c) => c.id);
|
|
269
|
+
t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((c) => {
|
|
270
|
+
if (!c)
|
|
266
271
|
return [];
|
|
267
|
-
const d = [...
|
|
272
|
+
const d = [...c];
|
|
268
273
|
return d.filter((I) => t.includes(I.id)).forEach((I) => {
|
|
269
274
|
I.archived = r;
|
|
270
275
|
}), d;
|
|
271
276
|
}));
|
|
272
|
-
}, W = (
|
|
277
|
+
}, W = (o, r, t, c) => L([
|
|
273
278
|
{
|
|
274
|
-
notificationId:
|
|
279
|
+
notificationId: o,
|
|
275
280
|
channel: r,
|
|
276
281
|
delivery: t,
|
|
277
|
-
subNotificationId:
|
|
282
|
+
subNotificationId: c
|
|
278
283
|
}
|
|
279
|
-
]), L = (
|
|
280
|
-
u.rest.postPreferences(
|
|
284
|
+
]), L = (o) => {
|
|
285
|
+
u.rest.postPreferences(o).then(() => {
|
|
281
286
|
u.getPreferences().then((r) => {
|
|
282
287
|
p(r);
|
|
283
288
|
});
|
|
284
289
|
});
|
|
285
290
|
};
|
|
286
291
|
U(() => {
|
|
287
|
-
f([]), l(!1), p(void 0),
|
|
288
|
-
p(
|
|
292
|
+
f([]), l(!1), p(void 0), y((/* @__PURE__ */ new Date()).toISOString()), P(!0), A(!0), u.openWebSocket(), u.getPreferences().then((o) => {
|
|
293
|
+
p(o);
|
|
289
294
|
});
|
|
290
|
-
}, [u,
|
|
295
|
+
}, [u, A]);
|
|
291
296
|
const H = {
|
|
292
297
|
notifications: a,
|
|
293
298
|
preferences: w,
|
|
294
|
-
loadNotifications:
|
|
299
|
+
loadNotifications: A,
|
|
295
300
|
markAsOpened: $,
|
|
296
301
|
markAsArchived: T,
|
|
297
302
|
markAsUnarchived: j,
|