@notificationapi/react 0.0.30 → 0.0.32

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,5 +1,5 @@
1
1
  import "react/jsx-runtime";
2
- import "../../assets/index3.js";
2
+ import "../../assets/presets.js";
3
3
  import "../Notifications/interface.js";
4
4
  import { a as r, g as m } from "../../assets/channelUtils.js";
5
5
  export {
@@ -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,134 +164,131 @@ 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) => {
219
- if (!a)
220
- 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)
224
- return [];
225
- const d = [...o];
222
+ [i.initialLoadMaxCount, O]
223
+ ), R = async (o) => {
224
+ if (!a) return;
225
+ const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((c) => o.includes(c.id) && !c.clicked).map((c) => c.id);
226
+ u.updateInAppNotifications({ ids: t, clicked: !0 }), f((c) => {
227
+ if (!c) return [];
228
+ const d = [...c];
226
229
  return d.filter((I) => t.includes(I.id)).forEach((I) => {
227
230
  I.clicked = r;
228
231
  }), d;
229
232
  });
230
233
  }, $ = async () => {
231
- if (!a)
232
- return;
233
- const i = (/* @__PURE__ */ new Date()).toISOString(), r = a.filter((t) => !t.opened || !t.seen).map((t) => t.id);
234
+ if (!a) return;
235
+ const o = (/* @__PURE__ */ new Date()).toISOString(), r = a.filter((t) => !t.opened || !t.seen).map((t) => t.id);
234
236
  r.length !== 0 && (u.updateInAppNotifications({
235
237
  ids: r,
236
238
  opened: !0
237
239
  }), f((t) => {
238
- if (!t)
239
- 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;
240
+ if (!t) return [];
241
+ const c = [...t];
242
+ return c.filter((d) => r.includes(d.id)).forEach((d) => {
243
+ d.opened = o, d.seen = !0;
244
+ }), c;
244
245
  }));
245
- }, j = async (i) => {
246
- if (!a)
247
- return;
248
- const r = a.filter((t) => t.archived && (i === "ALL" || i.includes(t.id))).map((t) => t.id);
246
+ }, j = async (o) => {
247
+ if (!a) return;
248
+ const r = a.filter((t) => t.archived && (o === "ALL" || o.includes(t.id))).map((t) => t.id);
249
249
  r.length !== 0 && (u.updateInAppNotifications({
250
250
  ids: r,
251
251
  archived: !1
252
252
  }), f((t) => {
253
- if (!t)
254
- return [];
255
- const o = [...t];
256
- return o.filter((d) => r.includes(d.id)).forEach((d) => {
253
+ if (!t) return [];
254
+ const c = [...t];
255
+ return c.filter((d) => r.includes(d.id)).forEach((d) => {
257
256
  d.archived = void 0;
258
- }), o;
257
+ }), c;
259
258
  }));
260
- }, T = async (i) => {
261
- if (!a)
262
- 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)
266
- return [];
267
- const d = [...o];
259
+ }, T = async (o) => {
260
+ if (!a) return;
261
+ const r = (/* @__PURE__ */ new Date()).toISOString(), t = a.filter((c) => !c.archived && (o === "ALL" || o.includes(c.id))).map((c) => c.id);
262
+ t.length !== 0 && (u.updateInAppNotifications({ ids: t, archived: !0 }), f((c) => {
263
+ if (!c) return [];
264
+ const d = [...c];
268
265
  return d.filter((I) => t.includes(I.id)).forEach((I) => {
269
266
  I.archived = r;
270
267
  }), d;
271
268
  }));
272
- }, W = (i, r, t, o) => L([
269
+ }, W = (o, r, t, c) => L([
273
270
  {
274
- notificationId: i,
271
+ notificationId: o,
275
272
  channel: r,
276
273
  delivery: t,
277
- subNotificationId: o
274
+ subNotificationId: c
278
275
  }
279
- ]), L = (i) => {
280
- u.rest.postPreferences(i).then(() => {
276
+ ]), L = (o) => {
277
+ u.rest.postPreferences(o).then(() => {
281
278
  u.getPreferences().then((r) => {
282
279
  p(r);
283
280
  });
284
281
  });
285
282
  };
286
283
  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);
284
+ f([]), l(!1), p(void 0), y((/* @__PURE__ */ new Date()).toISOString()), P(!0), A(!0), u.openWebSocket(), u.getPreferences().then((o) => {
285
+ p(o);
289
286
  });
290
- }, [u, k]);
287
+ }, [u, A]);
291
288
  const H = {
292
289
  notifications: a,
293
290
  preferences: w,
294
- loadNotifications: k,
291
+ loadNotifications: A,
295
292
  markAsOpened: $,
296
293
  markAsArchived: T,
297
294
  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.30",
4
+ "version": "0.0.32",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",
@@ -13,14 +13,14 @@
13
13
  "prepublishOnly": "npm run build"
14
14
  },
15
15
  "peerDependencies": {
16
- "react": "^16.14.0",
17
- "react-dom": "^16.14.0"
16
+ "react": "^17.0.2",
17
+ "react-dom": "^17.0.2"
18
18
  },
19
19
  "devDependencies": {
20
20
  "@ant-design/icons": "^5.3.6",
21
21
  "@types/node": "^20.12.7",
22
- "@types/react": "^16.14.0",
23
- "@types/react-dom": "^16.9.8",
22
+ "@types/react": "^17.0.38",
23
+ "@types/react-dom": "^17.0.11",
24
24
  "@typescript-eslint/eslint-plugin": "^7.2.0",
25
25
  "@typescript-eslint/parser": "^7.2.0",
26
26
  "@vitejs/plugin-react": "^4.2.1",