@notificationapi/react 0.0.12 → 0.0.13

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.
@@ -2,6 +2,6 @@ import { PropsWithChildren } from 'react';
2
2
  import { UnreadBadgeProps } from './UnreadBadge';
3
3
 
4
4
  export type NotificationCounterProps = Omit<UnreadBadgeProps, "count"> & {
5
- counting?: UnreadBadgeProps["count"];
5
+ count?: UnreadBadgeProps["count"];
6
6
  };
7
7
  export declare const NotificationCounter: React.FC<PropsWithChildren<NotificationCounterProps>>;
@@ -1,9 +1,9 @@
1
1
  import { jsx as n } from "react/jsx-runtime";
2
2
  import { UnreadBadge as r } from "./UnreadBadge.js";
3
- const N = (o) => {
4
- const t = o.counting || "COUNT_UNOPENED_NOTIFICATIONS";
3
+ const e = (o) => {
4
+ const t = o.count || "COUNT_UNOPENED_NOTIFICATIONS";
5
5
  return /* @__PURE__ */ n(r, { ...o, count: t });
6
6
  };
7
7
  export {
8
- N as NotificationCounter
8
+ e as NotificationCounter
9
9
  };
@@ -1,5 +1,6 @@
1
1
  import { PropsWithChildren } from 'react';
2
2
  import { NotificationPopupProps } from './NotificationPopup';
3
+ import { InAppNotification } from '../../interface';
3
4
 
4
5
  export type UnreadBadgeProps = {
5
6
  color?: "blue" | "purple" | "cyan" | "green" | "magenta" | "pink" | "red" | "orange" | "yellow" | "volcano" | "geekblue" | "lime" | "gold" | undefined;
@@ -8,7 +9,7 @@ export type UnreadBadgeProps = {
8
9
  showZero?: boolean;
9
10
  size?: "default" | "small";
10
11
  style?: React.CSSProperties;
11
- count?: keyof typeof COUNT_TYPE | ((notification: any) => boolean);
12
+ count?: keyof typeof COUNT_TYPE | ((notification: InAppNotification) => boolean);
12
13
  filter?: NotificationPopupProps["filter"];
13
14
  };
14
15
  export declare enum COUNT_TYPE {
@@ -1,15 +1,6 @@
1
1
  import { PropsWithChildren } from 'react';
2
2
  import { InAppNotification } from '../../interface';
3
3
 
4
- type Props = {
5
- clientId: string;
6
- userId: string;
7
- hashedUserId?: string;
8
- apiURL?: string;
9
- wsURL?: string;
10
- initialLoadMaxCount?: number;
11
- initialLoadMaxAge?: Date;
12
- };
13
4
  export declare const NOTIFICATION_ACTIONS: {
14
5
  opened: string;
15
6
  clicked: string;
@@ -109,5 +100,17 @@ export type Context = {
109
100
  updateDelivery: (notificationId: string, channel: Channels, delivery: DeliveryOptions, subNotificationId?: string) => void;
110
101
  };
111
102
  export declare const NotificationAPIContext: import('react').Context<Context | undefined>;
112
- export declare const NotificationAPIProvider: React.FunctionComponent<PropsWithChildren<Props>>;
103
+ type Props = {
104
+ clientId: string;
105
+ userId: string;
106
+ hashedUserId?: string;
107
+ apiURL?: string;
108
+ wsURL?: string;
109
+ initialLoadMaxCount?: number;
110
+ initialLoadMaxAge?: Date;
111
+ };
112
+ export declare const NotificationAPIProvider: React.FunctionComponent<PropsWithChildren<Props>> & {
113
+ useNotificationAPIContext: typeof useNotificationAPIContext;
114
+ };
115
+ declare const useNotificationAPIContext: () => Context;
113
116
  export {};
@@ -1,7 +1,7 @@
1
- import { jsx as _ } from "react/jsx-runtime";
2
- import { createContext as x, useState as f, useEffect as B } from "react";
3
- import { api as r } from "../../api.js";
4
- const j = {
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 = {
5
5
  opened: "opened",
6
6
  clicked: "clicked",
7
7
  archived: "archived",
@@ -9,10 +9,10 @@ const j = {
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))($ || {}), v = /* @__PURE__ */ ((e) => (e.OFF = "off", e.INSTANT = "instant", e.HOURLY = "hourly", e.DAILY = "daily", e.WEEKLY = "weekly", e.MONTHLY = "monthly", e))(v || {});
13
- const y = x(
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 = _(
14
14
  void 0
15
- ), J = (e) => {
15
+ ), b = (e) => {
16
16
  const c = {
17
17
  ...{
18
18
  apiURL: "https://api.notificationapi.com",
@@ -21,51 +21,51 @@ const y = x(
21
21
  initialLoadMaxAge: new Date((/* @__PURE__ */ new Date()).setMonth((/* @__PURE__ */ new Date()).getMonth() - 3))
22
22
  },
23
23
  ...e
24
- }, [s, u] = f(), [P, S] = f(), [U, w] = f(!1), [N, E] = f((/* @__PURE__ */ new Date()).toISOString()), [m, M] = f(!0), g = (a) => {
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
25
  const n = (/* @__PURE__ */ new Date()).toISOString();
26
- u((t) => (a = a.filter((i) => !(i.expDate && new Date(i.expDate * 1e3).toISOString() > n || i.date > n)), t ? [
27
- ...a.filter((i) => !t.find((o) => o.id === i.id)),
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)),
28
28
  ...t
29
- ] : a));
30
- }, T = async (a, n) => (await r(
29
+ ] : o));
30
+ }, k = async (o, n) => (await s(
31
31
  c.apiURL,
32
32
  "GET",
33
- `notifications/INAPP_WEB?count=${a}&before=${n}`,
33
+ `notifications/INAPP_WEB?count=${o}&before=${n}`,
34
34
  e.clientId,
35
35
  e.userId,
36
36
  e.hashedUserId
37
- )).notifications, k = async (a, n, t) => {
38
- let i = [], o = a, d = !0, h = !0;
37
+ )).notifications, x = async (o, n, t) => {
38
+ let i = [], a = o, r = !0, h = !0;
39
39
  for (; h; ) {
40
- const L = (await T(
40
+ const L = (await k(
41
41
  n,
42
- new Date(o).getTime()
42
+ new Date(a).getTime()
43
43
  )).filter(
44
44
  (I) => !i.find((l) => l.id === I.id)
45
45
  );
46
- o = L.reduce(
46
+ a = L.reduce(
47
47
  (I, l) => I < l.date ? I : l.date,
48
- a
49
- ), i = [...i, ...L], d = L.length > 0, h = !0, (!d || i.length >= n || t && o < t) && (h = !1);
48
+ o
49
+ ), i = [...i, ...L], r = L.length > 0, h = !0, (!r || i.length >= n || t && a < t) && (h = !1);
50
50
  }
51
51
  return {
52
52
  notifications: i,
53
- couldLoadMore: d,
54
- oldestReceived: o
53
+ couldLoadMore: r,
54
+ oldestReceived: a
55
55
  };
56
- }, A = async (a) => {
56
+ }, P = async (o) => {
57
57
  if (!m || U)
58
58
  return;
59
- w(!0);
60
- const n = await k(
61
- N,
62
- a ? c.initialLoadMaxCount : 1e3,
63
- a ? c.initialLoadMaxAge.toISOString() : void 0
59
+ S(!0);
60
+ const n = await x(
61
+ E,
62
+ o ? c.initialLoadMaxCount : 1e3,
63
+ o ? c.initialLoadMaxAge.toISOString() : void 0
64
64
  );
65
- E(n.oldestReceived), M(n.couldLoadMore), g(n.notifications), w(!1);
66
- }, R = async (a) => {
65
+ M(n.oldestReceived), T(n.couldLoadMore), A(n.notifications), S(!1);
66
+ }, R = async (o) => {
67
67
  const n = (/* @__PURE__ */ new Date()).toISOString();
68
- r(
68
+ s(
69
69
  c.apiURL,
70
70
  "PATCH",
71
71
  "notifications/INAPP_WEB",
@@ -73,20 +73,20 @@ const y = x(
73
73
  e.userId,
74
74
  e.hashedUserId,
75
75
  {
76
- trackingIds: [a],
76
+ trackingIds: [o],
77
77
  clicked: n
78
78
  }
79
79
  ), u((t) => {
80
80
  if (!t)
81
81
  return [];
82
- const i = [...t], o = i.find((d) => d.id === a);
83
- return o && (o.clicked = n), i;
82
+ const i = [...t], a = i.find((r) => r.id === o);
83
+ return a && (a.clicked = n), i;
84
84
  });
85
- }, O = async () => {
86
- if (!s)
85
+ }, C = async () => {
86
+ if (!d)
87
87
  return;
88
- const a = (/* @__PURE__ */ new Date()).toISOString(), n = s.filter((t) => !t.opened || !t.seen).map((t) => t.id);
89
- n.length !== 0 && (r(
88
+ const o = (/* @__PURE__ */ new Date()).toISOString(), n = d.filter((t) => !t.opened || !t.seen).map((t) => t.id);
89
+ n.length !== 0 && (s(
90
90
  c.apiURL,
91
91
  "PATCH",
92
92
  "notifications/INAPP_WEB",
@@ -95,21 +95,21 @@ const y = x(
95
95
  e.hashedUserId,
96
96
  {
97
97
  trackingIds: n,
98
- opened: a
98
+ opened: o
99
99
  }
100
100
  ), u((t) => {
101
101
  if (!t)
102
102
  return [];
103
103
  const i = [...t];
104
- return i.filter((o) => n.includes(o.id)).forEach((o) => {
105
- o.opened = a, o.seen = !0;
104
+ return i.filter((a) => n.includes(a.id)).forEach((a) => {
105
+ a.opened = o, a.seen = !0;
106
106
  }), i;
107
107
  }));
108
- }, D = async (a) => {
109
- if (!s)
108
+ }, O = async (o) => {
109
+ if (!d)
110
110
  return;
111
- const n = (/* @__PURE__ */ new Date()).toISOString(), t = s.filter((i) => !i.archived && (a === "ALL" || a.includes(i.id))).map((i) => i.id);
112
- t.length !== 0 && (r(
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(
113
113
  c.apiURL,
114
114
  "PATCH",
115
115
  "notifications/INAPP_WEB",
@@ -123,13 +123,13 @@ const y = x(
123
123
  ), u((i) => {
124
124
  if (!i)
125
125
  return [];
126
- const o = [...i];
127
- return o.filter((d) => t.includes(d.id)).forEach((d) => {
128
- d.archived = n;
129
- }), o;
126
+ const a = [...i];
127
+ return a.filter((r) => t.includes(r.id)).forEach((r) => {
128
+ r.archived = n;
129
+ }), a;
130
130
  }));
131
- }, H = (a, n, t, i) => {
132
- r(
131
+ }, D = (o, n, t, i) => {
132
+ s(
133
133
  c.apiURL,
134
134
  "POST",
135
135
  "preferences",
@@ -138,34 +138,34 @@ const y = x(
138
138
  e.hashedUserId,
139
139
  [
140
140
  {
141
- notificationId: a,
141
+ notificationId: o,
142
142
  subNotificationId: i,
143
143
  channel: n,
144
144
  delivery: t
145
145
  }
146
146
  ]
147
147
  ).then(() => {
148
- r(
148
+ s(
149
149
  c.apiURL,
150
150
  "GET",
151
151
  "preferences",
152
152
  e.clientId,
153
153
  e.userId,
154
154
  e.hashedUserId
155
- ).then((o) => {
156
- S(o);
155
+ ).then((a) => {
156
+ w(a);
157
157
  });
158
158
  });
159
159
  };
160
- B(() => {
161
- A(!0);
162
- const a = new WebSocket(
160
+ y(() => {
161
+ P(!0);
162
+ const o = new WebSocket(
163
163
  c.hashedUserId ? `${c.wsURL}?userId=${c.userId}&envId=${c.clientId}&userIdHash=${c.hashedUserId}` : `${c.wsURL}?userId=${c.userId}&envId=${c.clientId}`
164
164
  );
165
- a.onmessage = (n) => {
165
+ o.onmessage = (n) => {
166
166
  const t = JSON.parse(n.data);
167
- !t || !t.route || t.route === "inapp_web/new_notifications" && g(t.payload.notifications);
168
- }, r(
167
+ !t || !t.route || t.route === "inapp_web/new_notifications" && A(t.payload.notifications);
168
+ }, s(
169
169
  c.apiURL,
170
170
  "GET",
171
171
  "preferences",
@@ -173,24 +173,30 @@ const y = x(
173
173
  e.userId,
174
174
  e.hashedUserId
175
175
  ).then((n) => {
176
- S(n);
176
+ w(n);
177
177
  });
178
178
  }, []);
179
- const W = {
180
- notifications: s,
181
- preferences: P,
182
- loadNotifications: A,
183
- markAsOpened: O,
184
- markAsArchived: D,
179
+ const H = {
180
+ notifications: d,
181
+ preferences: N,
182
+ loadNotifications: P,
183
+ markAsOpened: C,
184
+ markAsArchived: O,
185
185
  markAsClicked: R,
186
- updateDelivery: H
186
+ updateDelivery: D
187
187
  };
188
- return /* @__PURE__ */ _(y.Provider, { value: W, children: e.children });
188
+ return /* @__PURE__ */ W(g.Provider, { value: H, children: e.children });
189
+ }, Y = () => {
190
+ const e = v(g);
191
+ if (!e)
192
+ throw new Error("useMyContext must be used within a MyProvider");
193
+ return e;
189
194
  };
195
+ b.useNotificationAPIContext = Y;
190
196
  export {
191
- $ as Channels,
192
- v as DeliveryOptions,
193
- j as NOTIFICATION_ACTIONS,
194
- y as NotificationAPIContext,
195
- J as NotificationAPIProvider
197
+ B as Channels,
198
+ $ as DeliveryOptions,
199
+ q as NOTIFICATION_ACTIONS,
200
+ g as NotificationAPIContext,
201
+ b as NotificationAPIProvider
196
202
  };
@@ -1,5 +1,6 @@
1
1
  export interface InAppNotification {
2
2
  id: string;
3
+ notificationId: string;
3
4
  seen: boolean;
4
5
  title: string;
5
6
  redirectURL?: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@notificationapi/react",
3
3
  "private": false,
4
- "version": "0.0.12",
4
+ "version": "0.0.13",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",