@notificationapi/react 0.0.8 → 0.0.9

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.
@@ -3,6 +3,7 @@ import { default as React, PropsWithChildren } from 'react';
3
3
  type Props = {
4
4
  clientId: string;
5
5
  userId: string;
6
+ hashedUserId?: string;
6
7
  apiURL?: string;
7
8
  wsURL?: string;
8
9
  initialLoadMaxCount?: number;
@@ -12,8 +12,8 @@ const j = {
12
12
  var C = /* @__PURE__ */ ((t) => (t.EMAIL = "EMAIL", t.INAPP_WEB = "INAPP_WEB", t.SMS = "SMS", t.CALL = "CALL", t.PUSH = "PUSH", t.WEB_PUSH = "WEB_PUSH", t))(C || {}), x = /* @__PURE__ */ ((t) => (t.OFF = "off", t.INSTANT = "instant", t.HOURLY = "hourly", t.DAILY = "daily", t.WEEKLY = "weekly", t.MONTHLY = "monthly", t))(x || {});
13
13
  const D = O(
14
14
  void 0
15
- ), p = (t) => {
16
- const r = {
15
+ ), J = (t) => {
16
+ const s = {
17
17
  ...{
18
18
  apiURL: "https://api.notificationapi.com",
19
19
  wsURL: "wss://ws.notificationapi.com",
@@ -21,55 +21,56 @@ const D = O(
21
21
  initialLoadMaxAge: new Date((/* @__PURE__ */ new Date()).setMonth((/* @__PURE__ */ new Date()).getMonth() - 3))
22
22
  },
23
23
  ...t
24
- }, [d, l] = f(), [g, w] = f(), [h, P] = f(!1), [E, m] = f((/* @__PURE__ */ new Date()).toISOString()), [M, k] = f(!0), N = (o) => {
24
+ }, [d, l] = f(), [L, A] = f(), [S, w] = f(!1), [U, E] = f((/* @__PURE__ */ new Date()).toISOString()), [m, M] = f(!0), P = (o) => {
25
25
  l((e) => e ? [
26
26
  ...o.filter((i) => !e.find((n) => n.id === i.id)),
27
27
  ...e
28
28
  ] : o);
29
- }, T = async (o, e) => (await u(
30
- r.apiURL,
29
+ }, k = async (o, e) => (await u(
30
+ s.apiURL,
31
31
  "GET",
32
32
  `notifications/INAPP_WEB?count=${o}&before=${e}`,
33
33
  t.clientId,
34
- t.userId
35
- )).notifications, U = async (o, e, i) => {
36
- let n = [], c = o, a = !0, s = !0;
37
- for (; s; ) {
38
- const A = (await T(
34
+ t.userId,
35
+ t.hashedUserId
36
+ )).notifications, T = async (o, e, i) => {
37
+ let n = [], a = o, c = !0, r = !0;
38
+ for (; r; ) {
39
+ const g = (await k(
39
40
  e,
40
- new Date(c).getTime()
41
+ new Date(a).getTime()
41
42
  )).filter(
42
- (I) => !n.find((L) => L.id === I.id)
43
+ (I) => !n.find((h) => h.id === I.id)
43
44
  );
44
- c = A.reduce(
45
- (I, L) => I < L.date ? I : L.date,
45
+ a = g.reduce(
46
+ (I, h) => I < h.date ? I : h.date,
46
47
  o
47
- ), n = [...n, ...A], a = A.length > 0, s = !0, (!a || n.length >= e || i && c < i) && (s = !1);
48
+ ), n = [...n, ...g], c = g.length > 0, r = !0, (!c || n.length >= e || i && a < i) && (r = !1);
48
49
  }
49
- return console.log(n.length, a, c), {
50
+ return console.log(n.length, c, a), {
50
51
  notifications: n,
51
- couldLoadMore: a,
52
- oldestReceived: c
52
+ couldLoadMore: c,
53
+ oldestReceived: a
53
54
  };
54
- }, S = async (o) => {
55
- if (!M || h)
55
+ }, N = async (o) => {
56
+ if (!m || S)
56
57
  return;
57
- P(!0);
58
- const e = await U(
59
- E,
60
- o ? r.initialLoadMaxCount : 1e3,
61
- o ? r.initialLoadMaxAge.toISOString() : void 0
58
+ w(!0);
59
+ const e = await T(
60
+ U,
61
+ o ? s.initialLoadMaxCount : 1e3,
62
+ o ? s.initialLoadMaxAge.toISOString() : void 0
62
63
  );
63
- m(e.oldestReceived), k(e.couldLoadMore), N(e.notifications), P(!1);
64
+ E(e.oldestReceived), M(e.couldLoadMore), P(e.notifications), w(!1);
64
65
  }, R = async (o) => {
65
66
  const e = (/* @__PURE__ */ new Date()).toISOString();
66
67
  u(
67
- r.apiURL,
68
+ s.apiURL,
68
69
  "PATCH",
69
70
  "notifications/INAPP_WEB",
70
71
  t.clientId,
71
72
  t.userId,
72
- "",
73
+ t.hashedUserId,
73
74
  {
74
75
  trackingIds: [o],
75
76
  clicked: e
@@ -77,20 +78,20 @@ const D = O(
77
78
  ), l((i) => {
78
79
  if (!i)
79
80
  return [];
80
- const n = [...i], c = n.find((a) => a.id === o);
81
- return c && (c.clicked = e), n;
81
+ const n = [...i], a = n.find((c) => c.id === o);
82
+ return a && (a.clicked = e), n;
82
83
  });
83
84
  }, W = async () => {
84
85
  if (!d)
85
86
  return;
86
87
  const o = (/* @__PURE__ */ new Date()).toISOString(), e = d.filter((i) => !i.opened || !i.seen).map((i) => i.id);
87
88
  e.length !== 0 && (u(
88
- r.apiURL,
89
+ s.apiURL,
89
90
  "PATCH",
90
91
  "notifications/INAPP_WEB",
91
92
  t.clientId,
92
93
  t.userId,
93
- "",
94
+ t.hashedUserId,
94
95
  {
95
96
  trackingIds: e,
96
97
  opened: o
@@ -99,8 +100,8 @@ const D = O(
99
100
  if (!i)
100
101
  return [];
101
102
  const n = [...i];
102
- return n.filter((c) => e.includes(c.id)).forEach((c) => {
103
- c.opened = o, c.seen = !0;
103
+ return n.filter((a) => e.includes(a.id)).forEach((a) => {
104
+ a.opened = o, a.seen = !0;
104
105
  }), n;
105
106
  }));
106
107
  }, _ = async (o) => {
@@ -108,12 +109,12 @@ const D = O(
108
109
  return;
109
110
  const e = (/* @__PURE__ */ new Date()).toISOString(), i = d.filter((n) => !n.archived && (o === "ALL" || o.includes(n.id))).map((n) => n.id);
110
111
  i.length !== 0 && (u(
111
- r.apiURL,
112
+ s.apiURL,
112
113
  "PATCH",
113
114
  "notifications/INAPP_WEB",
114
115
  t.clientId,
115
116
  t.userId,
116
- "",
117
+ t.hashedUserId,
117
118
  {
118
119
  trackingIds: i,
119
120
  archived: e
@@ -121,37 +122,42 @@ const D = O(
121
122
  ), l((n) => {
122
123
  if (!n)
123
124
  return [];
124
- const c = [...n];
125
- return c.filter((a) => i.includes(a.id)).forEach((a) => {
126
- a.archived = e;
127
- }), c;
125
+ const a = [...n];
126
+ return a.filter((c) => i.includes(c.id)).forEach((c) => {
127
+ c.archived = e;
128
+ }), a;
128
129
  }));
129
130
  }, H = (o, e, i, n) => {
130
- if (!g)
131
+ if (!L)
131
132
  return;
132
- const c = { ...g }, a = c.preferences.find(
133
- (s) => s.notificationId === o && s.subNotificationId === n && s.channel === e
133
+ const a = { ...L }, c = a.preferences.find(
134
+ (r) => r.notificationId === o && r.subNotificationId === n && r.channel === e
134
135
  );
135
- a && (a.delivery = i), w(c);
136
+ c && (c.delivery = i), A(a);
136
137
  };
137
138
  v(() => {
138
- S(!0);
139
+ N(!0);
139
140
  const o = new WebSocket(
140
- `${r.wsURL}?userId=${r.userId}&envId=${r.clientId}`
141
+ `${s.wsURL}?userId=${s.userId}&envId=${s.clientId}`
141
142
  );
142
143
  o.onmessage = (e) => {
143
144
  const i = JSON.parse(e.data);
144
- !i || !i.route || i.route === "inapp_web/new_notifications" && N(i.payload.notifications);
145
- }, u(r.apiURL, "GET", "preferences", t.clientId, t.userId).then(
146
- (e) => {
147
- w(e);
148
- }
149
- );
145
+ !i || !i.route || i.route === "inapp_web/new_notifications" && P(i.payload.notifications);
146
+ }, u(
147
+ s.apiURL,
148
+ "GET",
149
+ "preferences",
150
+ t.clientId,
151
+ t.userId,
152
+ t.hashedUserId
153
+ ).then((e) => {
154
+ A(e);
155
+ });
150
156
  }, []);
151
157
  const y = {
152
158
  notifications: d,
153
- preferences: g,
154
- loadNotifications: S,
159
+ preferences: L,
160
+ loadNotifications: N,
155
161
  markAsOpened: W,
156
162
  markAsArchived: _,
157
163
  markAsClicked: R,
@@ -164,5 +170,5 @@ export {
164
170
  x as DeliveryOptions,
165
171
  j as NOTIFICATION_ACTIONS,
166
172
  D as NotificationAPIContext,
167
- p as NotificationAPIProvider
173
+ J as NotificationAPIProvider
168
174
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@notificationapi/react",
3
3
  "private": false,
4
- "version": "0.0.8",
4
+ "version": "0.0.9",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",