@notificationapi/react 0.0.33 → 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.
- package/dist/components/Provider/index.js +104 -101
- package/package.json +1 -1
|
@@ -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
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, h) => {
|
|
|
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, h) => {
|
|
|
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, h) => {
|
|
|
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, h) => {
|
|
|
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, h) => {
|
|
|
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, h) => {
|
|
|
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,
|
|
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
120
|
for (; h; ) {
|
|
121
|
-
const
|
|
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, h) => {
|
|
|
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, h) => {
|
|
|
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 =
|
|
180
|
+
}, []), u = G(() => {
|
|
178
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,25 +197,25 @@ const J = async (e, s, i, a, f, w, h) => {
|
|
|
194
197
|
i.user.number,
|
|
195
198
|
i.hashedUserId,
|
|
196
199
|
S,
|
|
197
|
-
|
|
200
|
+
O,
|
|
198
201
|
e.client
|
|
199
|
-
]),
|
|
200
|
-
async (o,
|
|
201
|
-
const t = await u.rest.getNotifications(o,
|
|
202
|
-
|
|
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);
|
|
203
206
|
},
|
|
204
207
|
[S, u.rest]
|
|
205
|
-
),
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}, [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]);
|
|
209
212
|
const A = m(
|
|
210
213
|
async (o) => {
|
|
211
|
-
if (!(!o && (!
|
|
214
|
+
if (!(!o && (!C.current || x.current))) {
|
|
212
215
|
l(!0);
|
|
213
216
|
try {
|
|
214
|
-
await
|
|
215
|
-
o ? (/* @__PURE__ */ new Date()).toISOString() :
|
|
217
|
+
await v(
|
|
218
|
+
o ? (/* @__PURE__ */ new Date()).toISOString() : L.current,
|
|
216
219
|
o ? i.initialLoadMaxCount : 1e3
|
|
217
220
|
);
|
|
218
221
|
} finally {
|
|
@@ -220,93 +223,93 @@ const J = async (e, s, i, a, f, w, h) => {
|
|
|
220
223
|
}
|
|
221
224
|
}
|
|
222
225
|
},
|
|
223
|
-
[i.initialLoadMaxCount,
|
|
224
|
-
),
|
|
226
|
+
[i.initialLoadMaxCount, v]
|
|
227
|
+
), $ = async (o) => {
|
|
225
228
|
if (!a) return;
|
|
226
|
-
const
|
|
227
|
-
u.updateInAppNotifications({ ids: t, clicked: !0 }), f((
|
|
228
|
-
if (!
|
|
229
|
-
const d = [...
|
|
230
|
-
return d.filter((
|
|
231
|
-
|
|
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;
|
|
232
235
|
}), d;
|
|
233
236
|
});
|
|
234
|
-
},
|
|
237
|
+
}, T = async () => {
|
|
235
238
|
if (!a) return;
|
|
236
|
-
const o = (/* @__PURE__ */ new Date()).toISOString(),
|
|
237
|
-
|
|
238
|
-
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,
|
|
239
242
|
opened: !0
|
|
240
243
|
}), f((t) => {
|
|
241
244
|
if (!t) return [];
|
|
242
|
-
const
|
|
243
|
-
return
|
|
245
|
+
const r = [...t];
|
|
246
|
+
return r.filter((d) => s.includes(d.id)).forEach((d) => {
|
|
244
247
|
d.opened = o, d.seen = !0;
|
|
245
|
-
}),
|
|
248
|
+
}), r;
|
|
246
249
|
}));
|
|
247
250
|
}, j = async (o) => {
|
|
248
251
|
if (!a) return;
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
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,
|
|
252
255
|
archived: !1
|
|
253
256
|
}), f((t) => {
|
|
254
257
|
if (!t) return [];
|
|
255
|
-
const
|
|
256
|
-
return
|
|
258
|
+
const r = [...t];
|
|
259
|
+
return r.filter((d) => s.includes(d.id)).forEach((d) => {
|
|
257
260
|
d.archived = void 0;
|
|
258
|
-
}),
|
|
261
|
+
}), r;
|
|
259
262
|
}));
|
|
260
|
-
},
|
|
263
|
+
}, W = async (o) => {
|
|
261
264
|
if (!a) return;
|
|
262
|
-
const
|
|
263
|
-
t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((
|
|
264
|
-
if (!
|
|
265
|
-
const d = [...
|
|
266
|
-
return d.filter((
|
|
267
|
-
|
|
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;
|
|
268
271
|
}), d;
|
|
269
272
|
}));
|
|
270
|
-
},
|
|
273
|
+
}, H = (o, s, t, r) => U([
|
|
271
274
|
{
|
|
272
275
|
notificationId: o,
|
|
273
|
-
channel:
|
|
276
|
+
channel: s,
|
|
274
277
|
delivery: t,
|
|
275
|
-
subNotificationId:
|
|
278
|
+
subNotificationId: r
|
|
276
279
|
}
|
|
277
|
-
]),
|
|
280
|
+
]), U = (o) => {
|
|
278
281
|
u.rest.postPreferences(o).then(() => {
|
|
279
|
-
u.getPreferences().then((
|
|
280
|
-
h(
|
|
282
|
+
u.getPreferences().then((s) => {
|
|
283
|
+
h(s);
|
|
281
284
|
});
|
|
282
285
|
});
|
|
283
286
|
};
|
|
284
|
-
|
|
285
|
-
f([]), l(!1), h(void 0),
|
|
287
|
+
M(() => {
|
|
288
|
+
f([]), l(!1), h(void 0), D((/* @__PURE__ */ new Date()).toISOString()), P(!0), A(!0), u.openWebSocket(), u.getPreferences().then((o) => {
|
|
286
289
|
h(o);
|
|
287
290
|
});
|
|
288
291
|
}, [u, A]);
|
|
289
|
-
const
|
|
292
|
+
const B = {
|
|
290
293
|
notifications: a,
|
|
291
|
-
preferences:
|
|
294
|
+
preferences: I,
|
|
292
295
|
loadNotifications: A,
|
|
293
|
-
markAsOpened:
|
|
294
|
-
markAsArchived:
|
|
296
|
+
markAsOpened: T,
|
|
297
|
+
markAsArchived: W,
|
|
295
298
|
markAsUnarchived: j,
|
|
296
|
-
markAsClicked:
|
|
297
|
-
updateDelivery:
|
|
298
|
-
updateDeliveries:
|
|
299
|
+
markAsClicked: $,
|
|
300
|
+
updateDelivery: H,
|
|
301
|
+
updateDeliveries: U,
|
|
299
302
|
getClient: () => u
|
|
300
303
|
};
|
|
301
|
-
return /* @__PURE__ */
|
|
302
|
-
},
|
|
303
|
-
const e =
|
|
304
|
+
return /* @__PURE__ */ F(R.Provider, { value: B, children: e.children });
|
|
305
|
+
}, Q = () => {
|
|
306
|
+
const e = J(R);
|
|
304
307
|
if (!e)
|
|
305
308
|
throw new Error("useMyContext must be used within a MyProvider");
|
|
306
309
|
return e;
|
|
307
310
|
};
|
|
308
|
-
|
|
311
|
+
K.useNotificationAPIContext = Q;
|
|
309
312
|
export {
|
|
310
|
-
|
|
311
|
-
|
|
313
|
+
R as NotificationAPIContext,
|
|
314
|
+
K as NotificationAPIProvider
|
|
312
315
|
};
|