@notificationapi/react 0.0.18 → 0.0.19

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