@notificationapi/react 0.0.19 → 0.0.20

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
- import { jsx as _ } from "react/jsx-runtime";
2
- import { createContext as y, useState as u, useEffect as D, useContext as B } from "react";
1
+ import { jsx as W } from "react/jsx-runtime";
2
+ import { createContext as _, useState as u, useEffect as y, useContext as B } from "react";
3
3
  import { api as d } from "../../api.js";
4
4
  const z = {
5
5
  opened: "opened",
@@ -10,10 +10,10 @@ const z = {
10
10
  actioned2: "actioned2"
11
11
  };
12
12
  var $ = /* @__PURE__ */ ((e) => (e.EMAIL = "EMAIL", e.INAPP_WEB = "INAPP_WEB", e.SMS = "SMS", e.CALL = "CALL", e.PUSH = "PUSH", e.WEB_PUSH = "WEB_PUSH", e))($ || {}), b = /* @__PURE__ */ ((e) => (e.OFF = "off", e.INSTANT = "instant", e.HOURLY = "hourly", e.DAILY = "daily", e.WEEKLY = "weekly", e.MONTHLY = "monthly", e))(b || {});
13
- const S = y(
13
+ const N = _(
14
14
  void 0
15
15
  ), Y = (e) => {
16
- const c = {
16
+ const r = {
17
17
  ...{
18
18
  apiURL: "https://api.notificationapi.com",
19
19
  wsURL: "wss://ws.notificationapi.com",
@@ -21,119 +21,119 @@ const S = y(
21
21
  initialLoadMaxAge: new Date((/* @__PURE__ */ new Date()).setMonth((/* @__PURE__ */ new Date()).getMonth() - 3))
22
22
  },
23
23
  ...e
24
- }, [s, f] = u(), [U, w] = u(), [N, A] = u(!1), [E, m] = u((/* @__PURE__ */ new Date()).toISOString()), [M, k] = u(!0), P = (a) => {
25
- const i = (/* @__PURE__ */ new Date()).toISOString();
26
- f((t) => (a = a.filter((n) => !(n.expDate && new Date(n.expDate * 1e3).toISOString() > i || n.date > i)), t ? [
27
- ...a.filter((n) => !t.find((o) => o.id === n.id)),
24
+ }, [s, f] = u(), [E, h] = u(), [m, L] = u(!1), [M, A] = u((/* @__PURE__ */ new Date()).toISOString()), [k, g] = u(!0), P = (i) => {
25
+ const n = (/* @__PURE__ */ new Date()).toISOString();
26
+ f((t) => (i = i.filter((o) => !(o.expDate && new Date(o.expDate * 1e3).toISOString() > n || o.date > n)), t ? [
27
+ ...i.filter((o) => !t.find((a) => a.id === o.id)),
28
28
  ...t
29
- ] : a));
30
- }, T = async (a, i) => (await d(
31
- c.apiURL,
29
+ ] : i));
30
+ }, T = async (i, n) => (await d(
31
+ r.apiURL,
32
32
  "GET",
33
- `notifications/INAPP_WEB?count=${a}&before=${i}`,
33
+ `notifications/INAPP_WEB?count=${i}&before=${n}`,
34
34
  e.clientId,
35
35
  e.userId,
36
36
  e.hashedUserId
37
- )).notifications, R = async (a, i, t) => {
38
- let n = [], o = a, r = !0, h = !0;
39
- for (; h; ) {
40
- const L = (await T(
41
- i,
42
- new Date(o).getTime()
37
+ )).notifications, R = async (i, n, t) => {
38
+ let o = [], a = i, c = !0, w = !0;
39
+ for (; w; ) {
40
+ const S = (await T(
41
+ n,
42
+ new Date(a).getTime()
43
43
  )).filter(
44
- (I) => !n.find((l) => l.id === I.id)
44
+ (I) => !o.find((l) => l.id === I.id)
45
45
  );
46
- o = L.reduce(
46
+ a = S.reduce(
47
47
  (I, l) => I < l.date ? I : l.date,
48
- a
49
- ), n = [...n, ...L], r = L.length > 0, h = !0, (!r || n.length >= i || t && o < t) && (h = !1);
48
+ i
49
+ ), o = [...o, ...S], c = S.length > 0, w = !0, (!c || o.length >= n || t && a < t) && (w = !1);
50
50
  }
51
51
  return {
52
- notifications: n,
53
- couldLoadMore: r,
54
- oldestReceived: o
52
+ notifications: o,
53
+ couldLoadMore: c,
54
+ oldestReceived: a
55
55
  };
56
- }, g = async (a) => {
57
- if (!M || N)
56
+ }, U = async (i) => {
57
+ if (!i && !k || !i && m)
58
58
  return;
59
- A(!0);
60
- const i = await R(
61
- E,
62
- a ? c.initialLoadMaxCount : 1e3,
63
- a ? c.initialLoadMaxAge.toISOString() : void 0
59
+ L(!0);
60
+ const n = await R(
61
+ i ? (/* @__PURE__ */ new Date()).toISOString() : M,
62
+ i ? r.initialLoadMaxCount : 1e3,
63
+ i ? r.initialLoadMaxAge.toISOString() : void 0
64
64
  );
65
- m(i.oldestReceived), k(i.couldLoadMore), P(i.notifications), A(!1);
66
- }, x = async (a) => {
67
- const i = (/* @__PURE__ */ new Date()).toISOString();
65
+ A(n.oldestReceived), g(n.couldLoadMore), P(n.notifications), L(!1);
66
+ }, v = async (i) => {
67
+ const n = (/* @__PURE__ */ new Date()).toISOString();
68
68
  d(
69
- c.apiURL,
69
+ r.apiURL,
70
70
  "PATCH",
71
71
  "notifications/INAPP_WEB",
72
72
  e.clientId,
73
73
  e.userId,
74
74
  e.hashedUserId,
75
75
  {
76
- trackingIds: [a],
77
- clicked: i
76
+ trackingIds: [i],
77
+ clicked: n
78
78
  }
79
79
  ), f((t) => {
80
80
  if (!t)
81
81
  return [];
82
- const n = [...t], o = n.find((r) => r.id === a);
83
- return o && (o.clicked = i), n;
82
+ const o = [...t], a = o.find((c) => c.id === i);
83
+ return a && (a.clicked = n), o;
84
84
  });
85
- }, v = async () => {
85
+ }, x = async () => {
86
86
  if (!s)
87
87
  return;
88
- const a = (/* @__PURE__ */ new Date()).toISOString(), i = s.filter((t) => !t.opened || !t.seen).map((t) => t.id);
89
- i.length !== 0 && (d(
90
- c.apiURL,
88
+ const i = (/* @__PURE__ */ new Date()).toISOString(), n = s.filter((t) => !t.opened || !t.seen).map((t) => t.id);
89
+ n.length !== 0 && (d(
90
+ r.apiURL,
91
91
  "PATCH",
92
92
  "notifications/INAPP_WEB",
93
93
  e.clientId,
94
94
  e.userId,
95
95
  e.hashedUserId,
96
96
  {
97
- trackingIds: i,
98
- opened: a
97
+ trackingIds: n,
98
+ opened: i
99
99
  }
100
100
  ), f((t) => {
101
101
  if (!t)
102
102
  return [];
103
- const n = [...t];
104
- return n.filter((o) => i.includes(o.id)).forEach((o) => {
105
- o.opened = a, o.seen = !0;
106
- }), n;
103
+ const o = [...t];
104
+ return o.filter((a) => n.includes(a.id)).forEach((a) => {
105
+ a.opened = i, a.seen = !0;
106
+ }), o;
107
107
  }));
108
- }, C = async (a) => {
108
+ }, O = async (i) => {
109
109
  if (!s)
110
110
  return;
111
- const i = s.filter((t) => t.archived && (a === "ALL" || a.includes(t.id))).map((t) => t.id);
112
- i.length !== 0 && (d(
113
- c.apiURL,
111
+ const n = s.filter((t) => t.archived && (i === "ALL" || i.includes(t.id))).map((t) => t.id);
112
+ n.length !== 0 && (d(
113
+ r.apiURL,
114
114
  "PATCH",
115
115
  "notifications/INAPP_WEB",
116
116
  e.clientId,
117
117
  e.userId,
118
118
  e.hashedUserId,
119
119
  {
120
- trackingIds: i,
120
+ trackingIds: n,
121
121
  archived: null
122
122
  }
123
123
  ), f((t) => {
124
124
  if (!t)
125
125
  return [];
126
- const n = [...t];
127
- return n.filter((o) => i.includes(o.id)).forEach((o) => {
128
- o.archived = void 0;
129
- }), n;
126
+ const o = [...t];
127
+ return o.filter((a) => n.includes(a.id)).forEach((a) => {
128
+ a.archived = void 0;
129
+ }), o;
130
130
  }));
131
- }, H = async (a) => {
131
+ }, C = async (i) => {
132
132
  if (!s)
133
133
  return;
134
- const i = (/* @__PURE__ */ new Date()).toISOString(), t = s.filter((n) => !n.archived && (a === "ALL" || a.includes(n.id))).map((n) => n.id);
134
+ const n = (/* @__PURE__ */ new Date()).toISOString(), t = s.filter((o) => !o.archived && (i === "ALL" || i.includes(o.id))).map((o) => o.id);
135
135
  t.length !== 0 && (d(
136
- c.apiURL,
136
+ r.apiURL,
137
137
  "PATCH",
138
138
  "notifications/INAPP_WEB",
139
139
  e.clientId,
@@ -141,19 +141,19 @@ const S = y(
141
141
  e.hashedUserId,
142
142
  {
143
143
  trackingIds: t,
144
- archived: i
144
+ archived: n
145
145
  }
146
- ), f((n) => {
147
- if (!n)
146
+ ), f((o) => {
147
+ if (!o)
148
148
  return [];
149
- const o = [...n];
150
- return o.filter((r) => t.includes(r.id)).forEach((r) => {
151
- r.archived = i;
152
- }), o;
149
+ const a = [...o];
150
+ return a.filter((c) => t.includes(c.id)).forEach((c) => {
151
+ c.archived = n;
152
+ }), a;
153
153
  }));
154
- }, O = (a, i, t, n) => {
154
+ }, D = (i, n, t, o) => {
155
155
  d(
156
- c.apiURL,
156
+ r.apiURL,
157
157
  "POST",
158
158
  "preferences",
159
159
  e.clientId,
@@ -161,57 +161,57 @@ const S = y(
161
161
  e.hashedUserId,
162
162
  [
163
163
  {
164
- notificationId: a,
165
- subNotificationId: n,
166
- channel: i,
164
+ notificationId: i,
165
+ subNotificationId: o,
166
+ channel: n,
167
167
  delivery: t
168
168
  }
169
169
  ]
170
170
  ).then(() => {
171
171
  d(
172
- c.apiURL,
172
+ r.apiURL,
173
173
  "GET",
174
174
  "preferences",
175
175
  e.clientId,
176
176
  e.userId,
177
177
  e.hashedUserId
178
- ).then((o) => {
179
- w(o);
178
+ ).then((a) => {
179
+ h(a);
180
180
  });
181
181
  });
182
182
  };
183
- D(() => {
184
- g(!0);
185
- const a = new WebSocket(
186
- c.hashedUserId ? `${c.wsURL}?userId=${c.userId}&envId=${c.clientId}&userIdHash=${c.hashedUserId}` : `${c.wsURL}?userId=${c.userId}&envId=${c.clientId}`
183
+ y(() => {
184
+ f([]), L(!1), h(void 0), A((/* @__PURE__ */ new Date()).toISOString()), g(!0), U(!0);
185
+ const i = new WebSocket(
186
+ r.hashedUserId ? `${r.wsURL}?userId=${r.userId}&envId=${r.clientId}&userIdHash=${r.hashedUserId}` : `${r.wsURL}?userId=${r.userId}&envId=${r.clientId}`
187
187
  );
188
- a.onmessage = (i) => {
189
- const t = JSON.parse(i.data);
188
+ i.onmessage = (n) => {
189
+ const t = JSON.parse(n.data);
190
190
  !t || !t.route || t.route === "inapp_web/new_notifications" && P(t.payload.notifications);
191
191
  }, d(
192
- c.apiURL,
192
+ r.apiURL,
193
193
  "GET",
194
194
  "preferences",
195
195
  e.clientId,
196
196
  e.userId,
197
197
  e.hashedUserId
198
- ).then((i) => {
199
- w(i);
198
+ ).then((n) => {
199
+ h(n);
200
200
  });
201
- }, []);
202
- const W = {
201
+ }, [e]);
202
+ const H = {
203
203
  notifications: s,
204
- preferences: U,
205
- loadNotifications: g,
206
- markAsOpened: v,
207
- markAsArchived: H,
208
- markAsUnarchived: C,
209
- markAsClicked: x,
210
- updateDelivery: O
204
+ preferences: E,
205
+ loadNotifications: U,
206
+ markAsOpened: x,
207
+ markAsArchived: C,
208
+ markAsUnarchived: O,
209
+ markAsClicked: v,
210
+ updateDelivery: D
211
211
  };
212
- return /* @__PURE__ */ _(S.Provider, { value: W, children: e.children });
212
+ return /* @__PURE__ */ W(N.Provider, { value: H, children: e.children });
213
213
  }, F = () => {
214
- const e = B(S);
214
+ const e = B(N);
215
215
  if (!e)
216
216
  throw new Error("useMyContext must be used within a MyProvider");
217
217
  return e;
@@ -221,6 +221,6 @@ export {
221
221
  $ as Channels,
222
222
  b as DeliveryOptions,
223
223
  z as NOTIFICATION_ACTIONS,
224
- S as NotificationAPIContext,
224
+ N as NotificationAPIContext,
225
225
  Y as NotificationAPIProvider
226
226
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@notificationapi/react",
3
3
  "private": false,
4
- "version": "0.0.19",
4
+ "version": "0.0.20",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",