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