@notificationapi/react 0.0.29 → 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 A, useMemo as F, useRef as y, useEffect as U, useContext as G } from "react";
3
- const J = async (e, s, c, a, f, w, p) => {
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
- )}/${c}`,
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, c) => btoa(c ? e + ":" + s + ":" + c : e + ":" + s), M = {
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, c) {
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
- c
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 c = JSON.parse(s.data);
89
- if (!(!c || !c.route) && c.route === "inapp_web/new_notifications") {
90
- const a = c;
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, c = e.oldestNeeded || n.config.getInAppDefaultOldest;
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 < c) && (p = !1);
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 c = {
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, m] = N((/* @__PURE__ */ new Date()).toISOString()), [b, P] = N(!0), D = A(() => {
168
- c.playSoundOnNewNotification && new Audio(c.newNotificationSoundPath).play().catch((r) => {
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
- }, [c.newNotificationSoundPath, c.playSoundOnNewNotification]), S = A((i) => {
171
+ }, [i.newNotificationSoundPath, i.playSoundOnNewNotification]), S = m((o) => {
172
172
  const r = (/* @__PURE__ */ new Date()).toISOString();
173
- f((t) => (i = i.filter((o) => !(o.expDate && new Date(o.expDate * 1e3).toISOString() > r || o.date > r)), t ? [
174
- ...i.filter((o) => !t.find((d) => d.id === o.id)),
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
- ] : i));
176
+ ] : o));
177
177
  }, []), u = F(() => {
178
- const i = n.init({
179
- clientId: c.clientId,
180
- userId: c.user.id,
181
- hashedUserId: c.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 i.identify(c.user), i;
186
+ return o.identify({
187
+ email: i.user.email,
188
+ number: i.user.number
189
+ }), o;
187
190
  }, [
188
- c.clientId,
189
- c.user,
190
- c.hashedUserId,
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 = A(
194
- async (i, r) => {
195
- const t = await u.rest.getNotifications(i, r);
196
- m(t.oldestReceived), P(t.couldLoadMore), S(t.notifications);
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 = y(b), C = y(h), x = y(g);
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 k = A(
204
- async (i) => {
205
- if (!(!i && (!v.current || C.current))) {
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
- i ? (/* @__PURE__ */ new Date()).toISOString() : x.current,
210
- i ? c.initialLoadMaxCount : 1e3
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
- [c.initialLoadMaxCount, O]
218
- ), R = async (i) => {
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((o) => i.includes(o.id) && !o.clicked).map((o) => o.id);
222
- u.updateInAppNotifications({ ids: t, clicked: !0 }), f((o) => {
223
- if (!o)
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 = [...o];
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 i = (/* @__PURE__ */ new Date()).toISOString(), r = a.filter((t) => !t.opened || !t.seen).map((t) => t.id);
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 o = [...t];
241
- return o.filter((d) => r.includes(d.id)).forEach((d) => {
242
- d.opened = i, d.seen = !0;
243
- }), o;
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 (i) => {
250
+ }, j = async (o) => {
246
251
  if (!a)
247
252
  return;
248
- const r = a.filter((t) => t.archived && (i === "ALL" || i.includes(t.id))).map((t) => t.id);
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 o = [...t];
256
- return o.filter((d) => r.includes(d.id)).forEach((d) => {
260
+ const c = [...t];
261
+ return c.filter((d) => r.includes(d.id)).forEach((d) => {
257
262
  d.archived = void 0;
258
- }), o;
263
+ }), c;
259
264
  }));
260
- }, T = async (i) => {
265
+ }, T = async (o) => {
261
266
  if (!a)
262
267
  return;
263
- const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((o) => !o.archived && (i === "ALL" || i.includes(o.id))).map((o) => o.id);
264
- t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((o) => {
265
- if (!o)
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 = [...o];
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 = (i, r, t, o) => L([
277
+ }, W = (o, r, t, c) => L([
273
278
  {
274
- notificationId: i,
279
+ notificationId: o,
275
280
  channel: r,
276
281
  delivery: t,
277
- subNotificationId: o
282
+ subNotificationId: c
278
283
  }
279
- ]), L = (i) => {
280
- u.rest.postPreferences(i).then(() => {
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), m((/* @__PURE__ */ new Date()).toISOString()), P(!0), k(!0), u.openWebSocket(), u.getPreferences().then((i) => {
288
- p(i);
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, k]);
295
+ }, [u, A]);
291
296
  const H = {
292
297
  notifications: a,
293
298
  preferences: w,
294
- loadNotifications: k,
299
+ loadNotifications: A,
295
300
  markAsOpened: $,
296
301
  markAsArchived: T,
297
302
  markAsUnarchived: j,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@notificationapi/react",
3
3
  "private": false,
4
- "version": "0.0.29",
4
+ "version": "0.0.31",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",
@@ -43,7 +43,7 @@
43
43
  "**/*.css"
44
44
  ],
45
45
  "dependencies": {
46
- "@notificationapi/core": "^0.0.9",
46
+ "@notificationapi/core": "^0.0.10",
47
47
  "antd": "^5.17.4",
48
48
  "javascript-time-ago": "^2.5.10",
49
49
  "liquidjs": "^10.14.0",