@notificationapi/react 0.0.19 → 0.0.21

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.
@@ -14,7 +14,7 @@ export type InboxProps = {
14
14
  imageShape: keyof typeof ImageShape;
15
15
  pageSize: number;
16
16
  pagePosition: NotificationPopupProps["pagePosition"];
17
- notificationRenderer: ((notification: InAppNotification) => JSX.Element) | undefined;
17
+ notificationRenderer: ((notification: InAppNotification[]) => JSX.Element) | undefined;
18
18
  header?: InboxHeaderProps;
19
19
  };
20
20
  export declare const Inbox: React.FC<InboxProps>;
@@ -5,10 +5,10 @@ export declare enum ImageShape {
5
5
  circle = "circle"
6
6
  }
7
7
  export type NotificationProps = {
8
- notification: InAppNotification;
8
+ notifications: InAppNotification[];
9
9
  markAsArchived: (ids: string[] | "ALL") => void;
10
- markAsClicked: (id: string) => void;
10
+ markAsClicked: (ids: string[]) => void;
11
11
  imageShape: keyof typeof ImageShape;
12
- renderer?: (notification: InAppNotification) => JSX.Element;
12
+ renderer?: (notification: InAppNotification[]) => JSX.Element;
13
13
  };
14
14
  export declare const Notification: (props: NotificationProps) => JSX.Element;
@@ -97,7 +97,7 @@ export type Context = {
97
97
  markAsOpened: () => void;
98
98
  markAsArchived: (ids: string[] | "ALL") => void;
99
99
  markAsUnarchived: (ids: string[] | "ALL") => void;
100
- markAsClicked: (id: string) => void;
100
+ markAsClicked: (ids: string[]) => void;
101
101
  updateDelivery: (notificationId: string, channel: Channels, delivery: DeliveryOptions, subNotificationId?: string) => void;
102
102
  };
103
103
  export declare const NotificationAPIContext: import('react').Context<Context | undefined>;
@@ -1,7 +1,7 @@
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 B } from "react/jsx-runtime";
2
+ import { createContext as $, useState as u, useEffect as b, useContext as Y } from "react";
3
3
  import { api as d } from "../../api.js";
4
- const z = {
4
+ const X = {
5
5
  opened: "opened",
6
6
  clicked: "clicked",
7
7
  archived: "archived",
@@ -9,10 +9,10 @@ const z = {
9
9
  actioned1: "actioned1",
10
10
  actioned2: "actioned2"
11
11
  };
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(
12
+ var F = /* @__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))(F || {}), G = /* @__PURE__ */ ((e) => (e.OFF = "off", e.INSTANT = "instant", e.HOURLY = "hourly", e.DAILY = "daily", e.WEEKLY = "weekly", e.MONTHLY = "monthly", e))(G || {});
13
+ const E = $(
14
14
  void 0
15
- ), Y = (e) => {
15
+ ), j = (e) => {
16
16
  const c = {
17
17
  ...{
18
18
  apiURL: "https://api.notificationapi.com",
@@ -21,50 +21,52 @@ 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)),
28
- ...t
29
- ] : a));
30
- }, T = async (a, i) => (await d(
24
+ }, [s, f] = u(), [m, h] = u(), [k, L] = u(!1), [M, g] = u((/* @__PURE__ */ new Date()).toISOString()), [T, A] = u(!0), P = (t) => {
25
+ const o = (/* @__PURE__ */ new Date()).toISOString();
26
+ f((n) => (t = t.filter((i) => !(i.expDate && new Date(i.expDate * 1e3).toISOString() > o || i.date > o)), n ? [
27
+ ...t.filter((i) => !n.find((a) => a.id === i.id)),
28
+ ...n
29
+ ] : t));
30
+ }, O = async (t, o) => (await d(
31
31
  c.apiURL,
32
32
  "GET",
33
- `notifications/INAPP_WEB?count=${a}&before=${i}`,
33
+ `notifications/INAPP_WEB?count=${t}&before=${o}`,
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 (t, o, n) => {
38
+ let i = [], a = t, r = !0, w = !0;
39
+ for (; w; ) {
40
+ const S = (await O(
41
+ o,
42
+ new Date(a).getTime()
43
43
  )).filter(
44
- (I) => !n.find((l) => l.id === I.id)
44
+ (l) => !i.find((I) => I.id === l.id)
45
45
  );
46
- o = L.reduce(
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);
46
+ a = S.reduce(
47
+ (l, I) => l < I.date ? l : I.date,
48
+ t
49
+ ), i = [...i, ...S], r = S.length > 0, w = !0, (!r || i.length >= o || n && a < n) && (w = !1);
50
50
  }
51
51
  return {
52
- notifications: n,
52
+ notifications: i,
53
53
  couldLoadMore: r,
54
- oldestReceived: o
54
+ oldestReceived: a
55
55
  };
56
- }, g = async (a) => {
57
- if (!M || N)
56
+ }, U = async (t) => {
57
+ if (!t && !T || !t && k)
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 o = await R(
61
+ t ? (/* @__PURE__ */ new Date()).toISOString() : M,
62
+ t ? c.initialLoadMaxCount : 1e3,
63
+ t ? c.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
+ g(o.oldestReceived), A(o.couldLoadMore), P(o.notifications), L(!1);
66
+ }, v = async (t) => {
67
+ if (!s)
68
+ return;
69
+ const o = (/* @__PURE__ */ new Date()).toISOString(), n = s.filter((i) => t.includes(i.id) && !i.clicked).map((i) => i.id);
68
70
  d(
69
71
  c.apiURL,
70
72
  "PATCH",
@@ -73,20 +75,22 @@ const S = y(
73
75
  e.userId,
74
76
  e.hashedUserId,
75
77
  {
76
- trackingIds: [a],
77
- clicked: i
78
+ trackingIds: n,
79
+ clicked: o
78
80
  }
79
- ), f((t) => {
80
- if (!t)
81
+ ), f((i) => {
82
+ if (!i)
81
83
  return [];
82
- const n = [...t], o = n.find((r) => r.id === a);
83
- return o && (o.clicked = i), n;
84
+ const a = [...i];
85
+ return a.filter((r) => n.includes(r.id)).forEach((r) => {
86
+ r.clicked = o;
87
+ }), a;
84
88
  });
85
- }, v = async () => {
89
+ }, x = async () => {
86
90
  if (!s)
87
91
  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(
92
+ const t = (/* @__PURE__ */ new Date()).toISOString(), o = s.filter((n) => !n.opened || !n.seen).map((n) => n.id);
93
+ o.length !== 0 && (d(
90
94
  c.apiURL,
91
95
  "PATCH",
92
96
  "notifications/INAPP_WEB",
@@ -94,22 +98,22 @@ const S = y(
94
98
  e.userId,
95
99
  e.hashedUserId,
96
100
  {
97
- trackingIds: i,
98
- opened: a
101
+ trackingIds: o,
102
+ opened: t
99
103
  }
100
- ), f((t) => {
101
- if (!t)
104
+ ), f((n) => {
105
+ if (!n)
102
106
  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;
107
+ const i = [...n];
108
+ return i.filter((a) => o.includes(a.id)).forEach((a) => {
109
+ a.opened = t, a.seen = !0;
110
+ }), i;
107
111
  }));
108
- }, C = async (a) => {
112
+ }, C = async (t) => {
109
113
  if (!s)
110
114
  return;
111
- const i = s.filter((t) => t.archived && (a === "ALL" || a.includes(t.id))).map((t) => t.id);
112
- i.length !== 0 && (d(
115
+ const o = s.filter((n) => n.archived && (t === "ALL" || t.includes(n.id))).map((n) => n.id);
116
+ o.length !== 0 && (d(
113
117
  c.apiURL,
114
118
  "PATCH",
115
119
  "notifications/INAPP_WEB",
@@ -117,22 +121,22 @@ const S = y(
117
121
  e.userId,
118
122
  e.hashedUserId,
119
123
  {
120
- trackingIds: i,
124
+ trackingIds: o,
121
125
  archived: null
122
126
  }
123
- ), f((t) => {
124
- if (!t)
127
+ ), f((n) => {
128
+ if (!n)
125
129
  return [];
126
- const n = [...t];
127
- return n.filter((o) => i.includes(o.id)).forEach((o) => {
128
- o.archived = void 0;
129
- }), n;
130
+ const i = [...n];
131
+ return i.filter((a) => o.includes(a.id)).forEach((a) => {
132
+ a.archived = void 0;
133
+ }), i;
130
134
  }));
131
- }, H = async (a) => {
135
+ }, H = async (t) => {
132
136
  if (!s)
133
137
  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(
138
+ const o = (/* @__PURE__ */ new Date()).toISOString(), n = s.filter((i) => !i.archived && (t === "ALL" || t.includes(i.id))).map((i) => i.id);
139
+ n.length !== 0 && (d(
136
140
  c.apiURL,
137
141
  "PATCH",
138
142
  "notifications/INAPP_WEB",
@@ -140,18 +144,18 @@ const S = y(
140
144
  e.userId,
141
145
  e.hashedUserId,
142
146
  {
143
- trackingIds: t,
144
- archived: i
147
+ trackingIds: n,
148
+ archived: o
145
149
  }
146
- ), f((n) => {
147
- if (!n)
150
+ ), f((i) => {
151
+ if (!i)
148
152
  return [];
149
- const o = [...n];
150
- return o.filter((r) => t.includes(r.id)).forEach((r) => {
151
- r.archived = i;
152
- }), o;
153
+ const a = [...i];
154
+ return a.filter((r) => n.includes(r.id)).forEach((r) => {
155
+ r.archived = o;
156
+ }), a;
153
157
  }));
154
- }, O = (a, i, t, n) => {
158
+ }, D = (t, o, n, i) => {
155
159
  d(
156
160
  c.apiURL,
157
161
  "POST",
@@ -161,10 +165,10 @@ const S = y(
161
165
  e.hashedUserId,
162
166
  [
163
167
  {
164
- notificationId: a,
165
- subNotificationId: n,
166
- channel: i,
167
- delivery: t
168
+ notificationId: t,
169
+ subNotificationId: i,
170
+ channel: o,
171
+ delivery: n
168
172
  }
169
173
  ]
170
174
  ).then(() => {
@@ -175,52 +179,57 @@ const S = y(
175
179
  e.clientId,
176
180
  e.userId,
177
181
  e.hashedUserId
178
- ).then((o) => {
179
- w(o);
182
+ ).then((a) => {
183
+ h(a);
180
184
  });
181
185
  });
182
- };
183
- D(() => {
184
- g(!0);
185
- const a = new WebSocket(
186
+ }, W = (t) => {
187
+ const o = JSON.parse(t.data);
188
+ !o || !o.route || o.route === "inapp_web/new_notifications" && P(o.payload.notifications);
189
+ }, _ = (t) => {
190
+ setTimeout(() => {
191
+ t.onclose = () => N(), t.close();
192
+ }, 9 * 60 * 1e3);
193
+ }, N = () => {
194
+ const t = new WebSocket(
186
195
  c.hashedUserId ? `${c.wsURL}?userId=${c.userId}&envId=${c.clientId}&userIdHash=${c.hashedUserId}` : `${c.wsURL}?userId=${c.userId}&envId=${c.clientId}`
187
196
  );
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(
197
+ t.onopen = () => _(t), t.onmessage = W;
198
+ };
199
+ b(() => {
200
+ f([]), L(!1), h(void 0), g((/* @__PURE__ */ new Date()).toISOString()), A(!0), U(!0), N(), d(
192
201
  c.apiURL,
193
202
  "GET",
194
203
  "preferences",
195
204
  e.clientId,
196
205
  e.userId,
197
206
  e.hashedUserId
198
- ).then((i) => {
199
- w(i);
207
+ ).then((t) => {
208
+ h(t);
200
209
  });
201
- }, []);
202
- const W = {
210
+ }, [e]);
211
+ const y = {
203
212
  notifications: s,
204
- preferences: U,
205
- loadNotifications: g,
206
- markAsOpened: v,
213
+ preferences: m,
214
+ loadNotifications: U,
215
+ markAsOpened: x,
207
216
  markAsArchived: H,
208
217
  markAsUnarchived: C,
209
- markAsClicked: x,
210
- updateDelivery: O
218
+ markAsClicked: v,
219
+ updateDelivery: D
211
220
  };
212
- return /* @__PURE__ */ _(S.Provider, { value: W, children: e.children });
213
- }, F = () => {
214
- const e = B(S);
221
+ return /* @__PURE__ */ B(E.Provider, { value: y, children: e.children });
222
+ }, J = () => {
223
+ const e = Y(E);
215
224
  if (!e)
216
225
  throw new Error("useMyContext must be used within a MyProvider");
217
226
  return e;
218
227
  };
219
- Y.useNotificationAPIContext = F;
228
+ j.useNotificationAPIContext = J;
220
229
  export {
221
- $ as Channels,
222
- b as DeliveryOptions,
223
- z as NOTIFICATION_ACTIONS,
224
- S as NotificationAPIContext,
225
- Y as NotificationAPIProvider
230
+ F as Channels,
231
+ G as DeliveryOptions,
232
+ X as NOTIFICATION_ACTIONS,
233
+ E as NotificationAPIContext,
234
+ j as NotificationAPIProvider
226
235
  };
@@ -1,3 +1,19 @@
1
+ export declare enum DeliveryOptionsForInappWeb {
2
+ OFF = "off",
3
+ INSTANT = "instant"
4
+ }
5
+ export interface Template {
6
+ instant: {
7
+ title: string;
8
+ redirectURL: string;
9
+ imageURL: string;
10
+ };
11
+ batch: {
12
+ title: string;
13
+ redirectURL: string;
14
+ imageURL: string;
15
+ };
16
+ }
1
17
  export interface InAppNotification {
2
18
  id: string;
3
19
  notificationId: string;
@@ -6,6 +22,19 @@ export interface InAppNotification {
6
22
  redirectURL?: string;
7
23
  imageURL?: string;
8
24
  date: string;
25
+ deliveryOptions?: {
26
+ defaultDeliveryOption: DeliveryOptionsForInappWeb;
27
+ [DeliveryOptionsForInappWeb.OFF]?: {
28
+ enabled: boolean;
29
+ };
30
+ [DeliveryOptionsForInappWeb.INSTANT]?: {
31
+ enabled: boolean;
32
+ batching?: boolean;
33
+ batchingKey?: string;
34
+ batchingWindow?: number;
35
+ };
36
+ };
37
+ template?: Template;
9
38
  parameters?: Record<string, unknown>;
10
39
  expDate?: number;
11
40
  opened?: string;
package/dist/interface.js CHANGED
@@ -1 +1,4 @@
1
-
1
+ var f = /* @__PURE__ */ ((t) => (t.OFF = "off", t.INSTANT = "instant", t))(f || {});
2
+ export {
3
+ f as DeliveryOptionsForInappWeb
4
+ };
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.21",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",
@@ -42,6 +42,7 @@
42
42
  "dependencies": {
43
43
  "antd": "^5.17.4",
44
44
  "javascript-time-ago": "^2.5.10",
45
+ "liquidjs": "^10.14.0",
45
46
  "rc-virtual-list": "^3.11.5",
46
47
  "react-time-ago": "^7.3.3",
47
48
  "styled-components": "^6.1.9"