@notificationapi/react 0.0.32 → 0.0.34
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.
|
@@ -33,6 +33,7 @@ type Props = ({
|
|
|
33
33
|
initialLoadMaxAge?: Date;
|
|
34
34
|
playSoundOnNewNotification?: boolean;
|
|
35
35
|
newNotificationSoundPath?: string;
|
|
36
|
+
client?: typeof NotificationAPIClientSDK;
|
|
36
37
|
};
|
|
37
38
|
export declare const NotificationAPIProvider: React.FunctionComponent<PropsWithChildren<Props>> & {
|
|
38
39
|
useNotificationAPIContext: typeof useNotificationAPIContext;
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { createContext as z, useState as N, useCallback as m, useMemo as
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
`https://${
|
|
1
|
+
import { jsx as F } from "react/jsx-runtime";
|
|
2
|
+
import { createContext as z, useState as N, useCallback as m, useMemo as G, useRef as y, useEffect as M, useContext as J } from "react";
|
|
3
|
+
const _ = async (e, c, i, a, f, I, h) => {
|
|
4
|
+
const g = q(a, f, I), l = await fetch(
|
|
5
|
+
`https://${c}/${a}/users/${encodeURIComponent(
|
|
6
6
|
f
|
|
7
7
|
)}/${i}`,
|
|
8
8
|
{
|
|
9
9
|
method: e,
|
|
10
|
-
body: JSON.stringify(
|
|
10
|
+
body: JSON.stringify(h),
|
|
11
11
|
headers: {
|
|
12
|
-
Authorization: `Basic ${
|
|
12
|
+
Authorization: `Basic ${g}`
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
);
|
|
@@ -18,7 +18,7 @@ const J = async (e, s, i, a, f, w, p) => {
|
|
|
18
18
|
} catch {
|
|
19
19
|
return;
|
|
20
20
|
}
|
|
21
|
-
},
|
|
21
|
+
}, q = (e, c, i) => btoa(i ? e + ":" + c + ":" + i : e + ":" + c), E = {
|
|
22
22
|
host: "api.notificationapi.com",
|
|
23
23
|
websocketHost: "ws.notificationapi.com",
|
|
24
24
|
userId: "",
|
|
@@ -32,18 +32,18 @@ const J = async (e, s, i, a, f, w, p) => {
|
|
|
32
32
|
keepWebSocketAliveForSeconds: 86400
|
|
33
33
|
// 24 hours
|
|
34
34
|
}, n = {
|
|
35
|
-
config:
|
|
35
|
+
config: E,
|
|
36
36
|
init: function(e) {
|
|
37
|
-
return this.config = { ...
|
|
37
|
+
return this.config = { ...E, ...e }, {
|
|
38
38
|
...this
|
|
39
39
|
};
|
|
40
40
|
},
|
|
41
41
|
rest: {
|
|
42
|
-
generic: function(e,
|
|
43
|
-
return
|
|
42
|
+
generic: function(e, c, i) {
|
|
43
|
+
return _(
|
|
44
44
|
e,
|
|
45
45
|
n.config.host,
|
|
46
|
-
|
|
46
|
+
c,
|
|
47
47
|
n.config.clientId,
|
|
48
48
|
n.config.userId,
|
|
49
49
|
n.config.hashedUserId,
|
|
@@ -53,10 +53,10 @@ const J = async (e, s, i, a, f, w, p) => {
|
|
|
53
53
|
// The functions below are nice wrappers over the generic
|
|
54
54
|
// rest api function above. They must follow REST API naming:
|
|
55
55
|
// Method + Resource, representing the end-point.
|
|
56
|
-
getNotifications: function(e,
|
|
56
|
+
getNotifications: function(e, c) {
|
|
57
57
|
return n.rest.generic(
|
|
58
58
|
"GET",
|
|
59
|
-
`notifications/INAPP_WEB?count=${
|
|
59
|
+
`notifications/INAPP_WEB?count=${c}&before=${e}`
|
|
60
60
|
);
|
|
61
61
|
},
|
|
62
62
|
patchNotifications: function(e) {
|
|
@@ -84,8 +84,8 @@ const J = async (e, s, i, a, f, w, p) => {
|
|
|
84
84
|
object: void 0,
|
|
85
85
|
connect: function() {
|
|
86
86
|
let e = `wss://${n.config.websocketHost}?userId=${encodeURIComponent(n.config.userId)}&envId=${n.config.clientId}`;
|
|
87
|
-
return n.config.hashedUserId && (e += `&userIdHash=${encodeURIComponent(n.config.hashedUserId)}`), n.websocket.object = new WebSocket(e), n.websocket.object.onmessage = (
|
|
88
|
-
const i = JSON.parse(
|
|
87
|
+
return n.config.hashedUserId && (e += `&userIdHash=${encodeURIComponent(n.config.hashedUserId)}`), n.websocket.object = new WebSocket(e), n.websocket.object.onmessage = (c) => {
|
|
88
|
+
const i = JSON.parse(c.data);
|
|
89
89
|
if (!(!i || !i.route) && i.route === "inapp_web/new_notifications") {
|
|
90
90
|
const a = i;
|
|
91
91
|
n.config.onNewInAppNotifications && n.config.onNewInAppNotifications(
|
|
@@ -95,8 +95,8 @@ const J = async (e, s, i, a, f, w, p) => {
|
|
|
95
95
|
}, n.websocket.object;
|
|
96
96
|
},
|
|
97
97
|
disconnect: function(e) {
|
|
98
|
-
var
|
|
99
|
-
n.websocket.object && ((
|
|
98
|
+
var c;
|
|
99
|
+
n.websocket.object && ((c = n.websocket.object) == null || c.close(), e && e(n.websocket.object));
|
|
100
100
|
}
|
|
101
101
|
},
|
|
102
102
|
openWebSocket: function() {
|
|
@@ -115,31 +115,31 @@ const J = async (e, s, i, 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
|
|
119
|
-
let a = [], f = e.before,
|
|
120
|
-
for (;
|
|
121
|
-
const
|
|
118
|
+
const c = e.maxCountNeeded || n.config.getInAppDefaultCount, i = e.oldestNeeded || n.config.getInAppDefaultOldest;
|
|
119
|
+
let a = [], f = e.before, I = !0, h = !0;
|
|
120
|
+
for (; h; ) {
|
|
121
|
+
const g = (await n.rest.getNotifications(
|
|
122
122
|
f,
|
|
123
|
-
|
|
123
|
+
c
|
|
124
124
|
)).notifications.filter(
|
|
125
|
-
(l) => !a.find((
|
|
125
|
+
(l) => !a.find((w) => w.id === l.id)
|
|
126
126
|
);
|
|
127
|
-
f =
|
|
128
|
-
(l,
|
|
127
|
+
f = g.reduce(
|
|
128
|
+
(l, w) => l < w.date ? l : w.date,
|
|
129
129
|
e.before
|
|
130
|
-
), a = [...a, ...
|
|
130
|
+
), a = [...a, ...g], I = g.length > 0, h = !0, (!I || a.length >= c || f < i) && (h = !1);
|
|
131
131
|
}
|
|
132
132
|
return {
|
|
133
133
|
items: a,
|
|
134
|
-
hasMore:
|
|
134
|
+
hasMore: I,
|
|
135
135
|
oldestReceived: f
|
|
136
136
|
};
|
|
137
137
|
},
|
|
138
138
|
updateInAppNotifications: async (e) => {
|
|
139
|
-
const
|
|
139
|
+
const c = {
|
|
140
140
|
trackingIds: e.ids
|
|
141
141
|
};
|
|
142
|
-
return e.archived === !0 ?
|
|
142
|
+
return e.archived === !0 ? c.archived = (/* @__PURE__ */ new Date()).toISOString() : e.archived === !1 && (c.archived = null), e.clicked === !0 ? c.clicked = (/* @__PURE__ */ new Date()).toISOString() : e.clicked === !1 && (c.clicked = null), e.opened === !0 ? c.opened = (/* @__PURE__ */ new Date()).toISOString() : e.opened === !1 && (c.opened = null), n.rest.patchNotifications(c);
|
|
143
143
|
},
|
|
144
144
|
getPreferences: async () => n.rest.getPreferences(),
|
|
145
145
|
updateDeliveryOption: async (e) => n.rest.postPreferences([e]),
|
|
@@ -150,9 +150,9 @@ const J = async (e, s, i, a, f, w, p) => {
|
|
|
150
150
|
);
|
|
151
151
|
return n.rest.postUser(e);
|
|
152
152
|
}
|
|
153
|
-
},
|
|
153
|
+
}, R = z(
|
|
154
154
|
void 0
|
|
155
|
-
),
|
|
155
|
+
), K = (e) => {
|
|
156
156
|
const i = {
|
|
157
157
|
...{
|
|
158
158
|
apiURL: "https://api.notificationapi.com",
|
|
@@ -164,23 +164,26 @@ const J = async (e, s, i, a, f, w, p) => {
|
|
|
164
164
|
},
|
|
165
165
|
...e,
|
|
166
166
|
user: "userId" in e ? { id: e.userId } : e.user
|
|
167
|
-
}, [a, f] = N(), [
|
|
168
|
-
i.playSoundOnNewNotification && new Audio(i.newNotificationSoundPath).play().catch((
|
|
169
|
-
console.log("Failed to play new notification sound:",
|
|
167
|
+
}, [a, f] = N(), [I, h] = N(), [g, l] = N(!1), [w, D] = N((/* @__PURE__ */ new Date()).toISOString()), [b, P] = N(!0), O = m(() => {
|
|
168
|
+
i.playSoundOnNewNotification && new Audio(i.newNotificationSoundPath).play().catch((s) => {
|
|
169
|
+
console.log("Failed to play new notification sound:", s);
|
|
170
170
|
});
|
|
171
171
|
}, [i.newNotificationSoundPath, i.playSoundOnNewNotification]), S = m((o) => {
|
|
172
|
-
const
|
|
173
|
-
f((t) => (o = o.filter((
|
|
174
|
-
|
|
172
|
+
const s = (/* @__PURE__ */ new Date()).toISOString();
|
|
173
|
+
f((t) => (o = o.filter((d) => {
|
|
174
|
+
const p = d.expDate && new Date(d.expDate * 1e3).toISOString() < s, k = new Date(d.date).getTime() > new Date(s).getTime() + 1e3;
|
|
175
|
+
return !p && !k;
|
|
176
|
+
}), t ? [
|
|
177
|
+
...o.filter((d) => !t.find((k) => k.id === d.id)),
|
|
175
178
|
...t
|
|
176
179
|
] : o));
|
|
177
|
-
}, []), u =
|
|
178
|
-
const o = n.init({
|
|
180
|
+
}, []), u = G(() => {
|
|
181
|
+
const o = e.client ? e.client : n.init({
|
|
179
182
|
clientId: i.clientId,
|
|
180
183
|
userId: i.user.id,
|
|
181
184
|
hashedUserId: i.hashedUserId,
|
|
182
|
-
onNewInAppNotifications: (
|
|
183
|
-
|
|
185
|
+
onNewInAppNotifications: (s) => {
|
|
186
|
+
O(), S(s);
|
|
184
187
|
}
|
|
185
188
|
});
|
|
186
189
|
return o.identify({
|
|
@@ -194,24 +197,25 @@ const J = async (e, s, i, a, f, w, p) => {
|
|
|
194
197
|
i.user.number,
|
|
195
198
|
i.hashedUserId,
|
|
196
199
|
S,
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
200
|
+
O,
|
|
201
|
+
e.client
|
|
202
|
+
]), v = m(
|
|
203
|
+
async (o, s) => {
|
|
204
|
+
const t = await u.rest.getNotifications(o, s);
|
|
205
|
+
D(t.oldestReceived), P(t.couldLoadMore), S(t.notifications);
|
|
202
206
|
},
|
|
203
207
|
[S, u.rest]
|
|
204
|
-
),
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}, [b,
|
|
208
|
+
), C = y(b), x = y(g), L = y(w);
|
|
209
|
+
M(() => {
|
|
210
|
+
C.current = b, x.current = g, L.current = w;
|
|
211
|
+
}, [b, g, w]);
|
|
208
212
|
const A = m(
|
|
209
213
|
async (o) => {
|
|
210
|
-
if (!(!o && (!
|
|
214
|
+
if (!(!o && (!C.current || x.current))) {
|
|
211
215
|
l(!0);
|
|
212
216
|
try {
|
|
213
|
-
await
|
|
214
|
-
o ? (/* @__PURE__ */ new Date()).toISOString() :
|
|
217
|
+
await v(
|
|
218
|
+
o ? (/* @__PURE__ */ new Date()).toISOString() : L.current,
|
|
215
219
|
o ? i.initialLoadMaxCount : 1e3
|
|
216
220
|
);
|
|
217
221
|
} finally {
|
|
@@ -219,93 +223,93 @@ const J = async (e, s, i, a, f, w, p) => {
|
|
|
219
223
|
}
|
|
220
224
|
}
|
|
221
225
|
},
|
|
222
|
-
[i.initialLoadMaxCount,
|
|
223
|
-
),
|
|
226
|
+
[i.initialLoadMaxCount, v]
|
|
227
|
+
), $ = async (o) => {
|
|
224
228
|
if (!a) return;
|
|
225
|
-
const
|
|
226
|
-
u.updateInAppNotifications({ ids: t, clicked: !0 }), f((
|
|
227
|
-
if (!
|
|
228
|
-
const d = [...
|
|
229
|
-
return d.filter((
|
|
230
|
-
|
|
229
|
+
const s = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((r) => o.includes(r.id) && !r.clicked).map((r) => r.id);
|
|
230
|
+
u.updateInAppNotifications({ ids: t, clicked: !0 }), f((r) => {
|
|
231
|
+
if (!r) return [];
|
|
232
|
+
const d = [...r];
|
|
233
|
+
return d.filter((p) => t.includes(p.id)).forEach((p) => {
|
|
234
|
+
p.clicked = s;
|
|
231
235
|
}), d;
|
|
232
236
|
});
|
|
233
|
-
},
|
|
237
|
+
}, T = async () => {
|
|
234
238
|
if (!a) return;
|
|
235
|
-
const o = (/* @__PURE__ */ new Date()).toISOString(),
|
|
236
|
-
|
|
237
|
-
ids:
|
|
239
|
+
const o = (/* @__PURE__ */ new Date()).toISOString(), s = a.filter((t) => !t.opened || !t.seen).map((t) => t.id);
|
|
240
|
+
s.length !== 0 && (u.updateInAppNotifications({
|
|
241
|
+
ids: s,
|
|
238
242
|
opened: !0
|
|
239
243
|
}), f((t) => {
|
|
240
244
|
if (!t) return [];
|
|
241
|
-
const
|
|
242
|
-
return
|
|
245
|
+
const r = [...t];
|
|
246
|
+
return r.filter((d) => s.includes(d.id)).forEach((d) => {
|
|
243
247
|
d.opened = o, d.seen = !0;
|
|
244
|
-
}),
|
|
248
|
+
}), r;
|
|
245
249
|
}));
|
|
246
250
|
}, j = async (o) => {
|
|
247
251
|
if (!a) return;
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
ids:
|
|
252
|
+
const s = a.filter((t) => t.archived && (o === "ALL" || o.includes(t.id))).map((t) => t.id);
|
|
253
|
+
s.length !== 0 && (u.updateInAppNotifications({
|
|
254
|
+
ids: s,
|
|
251
255
|
archived: !1
|
|
252
256
|
}), f((t) => {
|
|
253
257
|
if (!t) return [];
|
|
254
|
-
const
|
|
255
|
-
return
|
|
258
|
+
const r = [...t];
|
|
259
|
+
return r.filter((d) => s.includes(d.id)).forEach((d) => {
|
|
256
260
|
d.archived = void 0;
|
|
257
|
-
}),
|
|
261
|
+
}), r;
|
|
258
262
|
}));
|
|
259
|
-
},
|
|
263
|
+
}, W = async (o) => {
|
|
260
264
|
if (!a) return;
|
|
261
|
-
const
|
|
262
|
-
t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((
|
|
263
|
-
if (!
|
|
264
|
-
const d = [...
|
|
265
|
-
return d.filter((
|
|
266
|
-
|
|
265
|
+
const s = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((r) => !r.archived && (o === "ALL" || o.includes(r.id))).map((r) => r.id);
|
|
266
|
+
t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((r) => {
|
|
267
|
+
if (!r) return [];
|
|
268
|
+
const d = [...r];
|
|
269
|
+
return d.filter((p) => t.includes(p.id)).forEach((p) => {
|
|
270
|
+
p.archived = s;
|
|
267
271
|
}), d;
|
|
268
272
|
}));
|
|
269
|
-
},
|
|
273
|
+
}, H = (o, s, t, r) => U([
|
|
270
274
|
{
|
|
271
275
|
notificationId: o,
|
|
272
|
-
channel:
|
|
276
|
+
channel: s,
|
|
273
277
|
delivery: t,
|
|
274
|
-
subNotificationId:
|
|
278
|
+
subNotificationId: r
|
|
275
279
|
}
|
|
276
|
-
]),
|
|
280
|
+
]), U = (o) => {
|
|
277
281
|
u.rest.postPreferences(o).then(() => {
|
|
278
|
-
u.getPreferences().then((
|
|
279
|
-
|
|
282
|
+
u.getPreferences().then((s) => {
|
|
283
|
+
h(s);
|
|
280
284
|
});
|
|
281
285
|
});
|
|
282
286
|
};
|
|
283
|
-
|
|
284
|
-
f([]), l(!1),
|
|
285
|
-
|
|
287
|
+
M(() => {
|
|
288
|
+
f([]), l(!1), h(void 0), D((/* @__PURE__ */ new Date()).toISOString()), P(!0), A(!0), u.openWebSocket(), u.getPreferences().then((o) => {
|
|
289
|
+
h(o);
|
|
286
290
|
});
|
|
287
291
|
}, [u, A]);
|
|
288
|
-
const
|
|
292
|
+
const B = {
|
|
289
293
|
notifications: a,
|
|
290
|
-
preferences:
|
|
294
|
+
preferences: I,
|
|
291
295
|
loadNotifications: A,
|
|
292
|
-
markAsOpened:
|
|
293
|
-
markAsArchived:
|
|
296
|
+
markAsOpened: T,
|
|
297
|
+
markAsArchived: W,
|
|
294
298
|
markAsUnarchived: j,
|
|
295
|
-
markAsClicked:
|
|
296
|
-
updateDelivery:
|
|
297
|
-
updateDeliveries:
|
|
299
|
+
markAsClicked: $,
|
|
300
|
+
updateDelivery: H,
|
|
301
|
+
updateDeliveries: U,
|
|
298
302
|
getClient: () => u
|
|
299
303
|
};
|
|
300
|
-
return /* @__PURE__ */
|
|
301
|
-
},
|
|
302
|
-
const e =
|
|
304
|
+
return /* @__PURE__ */ F(R.Provider, { value: B, children: e.children });
|
|
305
|
+
}, Q = () => {
|
|
306
|
+
const e = J(R);
|
|
303
307
|
if (!e)
|
|
304
308
|
throw new Error("useMyContext must be used within a MyProvider");
|
|
305
309
|
return e;
|
|
306
310
|
};
|
|
307
|
-
|
|
311
|
+
K.useNotificationAPIContext = Q;
|
|
308
312
|
export {
|
|
309
|
-
|
|
310
|
-
|
|
313
|
+
R as NotificationAPIContext,
|
|
314
|
+
K as NotificationAPIProvider
|
|
311
315
|
};
|