@notificationapi/react 0.0.5 → 0.0.6

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.
@@ -0,0 +1,8 @@
1
+ import "react/jsx-runtime";
2
+ import "../Provider/index.js";
3
+ import { a as i } from "../../assets/Preferences.js";
4
+ import "react";
5
+ import "../../assets/index3.js";
6
+ export {
7
+ i as PreferenceGroup
8
+ };
@@ -0,0 +1,12 @@
1
+ import "react/jsx-runtime";
2
+ import "../Provider/index.js";
3
+ import "react";
4
+ import "../../assets/colors.js";
5
+ import { P as n, b as s, g } from "../../assets/Preferences.js";
6
+ import "../../assets/index3.js";
7
+ import "../../assets/BellOutlined.js";
8
+ export {
9
+ n as Preferences,
10
+ s as getChannelIcon,
11
+ g as getChannelLabel
12
+ };
@@ -0,0 +1,6 @@
1
+ import { NotificationPreferencesPopup as r } from "./NotificationPreferencesPopup.js";
2
+ import { NotificationPreferencesInline as f } from "./NotificationPreferencesInline.js";
3
+ export {
4
+ f as NotificationPreferencesInline,
5
+ r as NotificationPreferencesPopup
6
+ };
@@ -0,0 +1,168 @@
1
+ import { jsx as B } from "react/jsx-runtime";
2
+ import { createContext as O, useState as f, useEffect as v } from "react";
3
+ import { api as u } from "../../api.js";
4
+ const j = {
5
+ opened: "opened",
6
+ clicked: "clicked",
7
+ archived: "archived",
8
+ replied: "replied",
9
+ actioned1: "actioned1",
10
+ actioned2: "actioned2"
11
+ };
12
+ var C = /* @__PURE__ */ ((t) => (t.EMAIL = "EMAIL", t.INAPP_WEB = "INAPP_WEB", t.SMS = "SMS", t.CALL = "CALL", t.PUSH = "PUSH", t.WEB_PUSH = "WEB_PUSH", t))(C || {}), x = /* @__PURE__ */ ((t) => (t.OFF = "off", t.INSTANT = "instant", t.HOURLY = "hourly", t.DAILY = "daily", t.WEEKLY = "weekly", t.MONTHLY = "monthly", t))(x || {});
13
+ const D = O(
14
+ void 0
15
+ ), p = (t) => {
16
+ const r = {
17
+ ...{
18
+ apiURL: "https://api.notificationapi.com",
19
+ wsURL: "wss://ws.notificationapi.com",
20
+ initialLoadMaxCount: 1e3,
21
+ initialLoadMaxAge: new Date((/* @__PURE__ */ new Date()).setMonth((/* @__PURE__ */ new Date()).getMonth() - 3))
22
+ },
23
+ ...t
24
+ }, [d, l] = f(), [g, w] = f(), [h, P] = f(!1), [E, m] = f((/* @__PURE__ */ new Date()).toISOString()), [M, k] = f(!0), N = (o) => {
25
+ l((e) => e ? [
26
+ ...o.filter((i) => !e.find((n) => n.id === i.id)),
27
+ ...e
28
+ ] : o);
29
+ }, T = async (o, e) => (await u(
30
+ r.apiURL,
31
+ "GET",
32
+ `notifications/INAPP_WEB?count=${o}&before=${e}`,
33
+ t.clientId,
34
+ t.userId
35
+ )).notifications, U = async (o, e, i) => {
36
+ let n = [], c = o, a = !0, s = !0;
37
+ for (; s; ) {
38
+ const A = (await T(
39
+ e,
40
+ new Date(c).getTime()
41
+ )).filter(
42
+ (I) => !n.find((L) => L.id === I.id)
43
+ );
44
+ c = A.reduce(
45
+ (I, L) => I < L.date ? I : L.date,
46
+ o
47
+ ), n = [...n, ...A], a = A.length > 0, s = !0, (!a || n.length >= e || i && c < i) && (s = !1);
48
+ }
49
+ return console.log(n.length, a, c), {
50
+ notifications: n,
51
+ couldLoadMore: a,
52
+ oldestReceived: c
53
+ };
54
+ }, S = async (o) => {
55
+ if (!M || h)
56
+ return;
57
+ P(!0);
58
+ const e = await U(
59
+ E,
60
+ o ? r.initialLoadMaxCount : 1e3,
61
+ o ? r.initialLoadMaxAge.toISOString() : void 0
62
+ );
63
+ m(e.oldestReceived), k(e.couldLoadMore), N(e.notifications), P(!1);
64
+ }, R = async (o) => {
65
+ const e = (/* @__PURE__ */ new Date()).toISOString();
66
+ u(
67
+ r.apiURL,
68
+ "PATCH",
69
+ "notifications/INAPP_WEB",
70
+ t.clientId,
71
+ t.userId,
72
+ "",
73
+ {
74
+ trackingIds: [o],
75
+ clicked: e
76
+ }
77
+ ), l((i) => {
78
+ if (!i)
79
+ return [];
80
+ const n = [...i], c = n.find((a) => a.id === o);
81
+ return c && (c.clicked = e), n;
82
+ });
83
+ }, W = async () => {
84
+ if (!d)
85
+ return;
86
+ const o = (/* @__PURE__ */ new Date()).toISOString(), e = d.filter((i) => !i.opened || !i.seen).map((i) => i.id);
87
+ e.length !== 0 && (u(
88
+ r.apiURL,
89
+ "PATCH",
90
+ "notifications/INAPP_WEB",
91
+ t.clientId,
92
+ t.userId,
93
+ "",
94
+ {
95
+ trackingIds: e,
96
+ opened: o
97
+ }
98
+ ), l((i) => {
99
+ if (!i)
100
+ return [];
101
+ const n = [...i];
102
+ return n.filter((c) => e.includes(c.id)).forEach((c) => {
103
+ c.opened = o, c.seen = !0;
104
+ }), n;
105
+ }));
106
+ }, _ = async (o) => {
107
+ if (!d)
108
+ return;
109
+ const e = (/* @__PURE__ */ new Date()).toISOString(), i = d.filter((n) => !n.archived && (o === "ALL" || o.includes(n.id))).map((n) => n.id);
110
+ i.length !== 0 && (u(
111
+ r.apiURL,
112
+ "PATCH",
113
+ "notifications/INAPP_WEB",
114
+ t.clientId,
115
+ t.userId,
116
+ "",
117
+ {
118
+ trackingIds: i,
119
+ archived: e
120
+ }
121
+ ), l((n) => {
122
+ if (!n)
123
+ return [];
124
+ const c = [...n];
125
+ return c.filter((a) => i.includes(a.id)).forEach((a) => {
126
+ a.archived = e;
127
+ }), c;
128
+ }));
129
+ }, H = (o, e, i, n) => {
130
+ if (!g)
131
+ return;
132
+ const c = { ...g }, a = c.preferences.find(
133
+ (s) => s.notificationId === o && s.subNotificationId === n && s.channel === e
134
+ );
135
+ a && (a.delivery = i), w(c);
136
+ };
137
+ v(() => {
138
+ S(!0);
139
+ const o = new WebSocket(
140
+ `${r.wsURL}?userId=${r.userId}&envId=${r.clientId}`
141
+ );
142
+ o.onmessage = (e) => {
143
+ const i = JSON.parse(e.data);
144
+ !i || !i.route || i.route === "inapp_web/new_notifications" && N(i.payload.notifications);
145
+ }, u(r.apiURL, "GET", "preferences", t.clientId, t.userId).then(
146
+ (e) => {
147
+ w(e);
148
+ }
149
+ );
150
+ }, []);
151
+ const y = {
152
+ notifications: d,
153
+ preferences: g,
154
+ loadNotifications: S,
155
+ markAsOpened: W,
156
+ markAsArchived: _,
157
+ markAsClicked: R,
158
+ updateDelivery: H
159
+ };
160
+ return /* @__PURE__ */ B(D.Provider, { value: y, children: t.children });
161
+ };
162
+ export {
163
+ C as Channels,
164
+ x as DeliveryOptions,
165
+ j as NOTIFICATION_ACTIONS,
166
+ D as NotificationAPIContext,
167
+ p as NotificationAPIProvider
168
+ };