@ttoss/react-notifications 2.4.39 → 2.5.0

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,219 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ import * as React from 'react';
3
+ var __defProp = Object.defineProperty;
4
+ var __name = (target, value) => __defProp(target, "name", {
5
+ value,
6
+ configurable: true
7
+ });
8
+
9
+ // src/NotificationsBox.tsx
10
+ import { NotificationCard } from "@ttoss/components/NotificationCard";
11
+ import { Stack } from "@ttoss/ui";
12
+
13
+ // src/Provider.tsx
14
+ import { toast, ToastContainer } from "@ttoss/components/Toast";
15
+ import { Flex, InfiniteLinearProgress } from "@ttoss/ui";
16
+ import * as React2 from "react";
17
+
18
+ // src/NotificationsHeader.tsx
19
+ var NotificationsHeader = /* @__PURE__ */__name(() => {
20
+ const {
21
+ notifications
22
+ } = useNotifications();
23
+ const headerNotifications = notifications?.filter(notification => {
24
+ return notification.viewType === "header";
25
+ });
26
+ return /* @__PURE__ */React.createElement(NotificationsBox, {
27
+ notifications: headerNotifications
28
+ });
29
+ }, "NotificationsHeader");
30
+
31
+ // src/NotificationsModal.tsx
32
+ import { Modal } from "@ttoss/components/Modal";
33
+ import { CloseButton } from "@ttoss/ui";
34
+ var NotificationsModal = /* @__PURE__ */__name(() => {
35
+ const {
36
+ notifications,
37
+ clearNotifications,
38
+ defaultViewType
39
+ } = useNotifications();
40
+ const modalNotifications = notifications?.filter(notification => {
41
+ if (defaultViewType === "modal" && !notification.viewType) {
42
+ return true;
43
+ }
44
+ return notification.viewType === "modal";
45
+ });
46
+ const isOpen = !!modalNotifications && modalNotifications.length > 0;
47
+ return /* @__PURE__ */React.createElement(Modal, {
48
+ isOpen,
49
+ style: {
50
+ content: {
51
+ minWidth: "30%",
52
+ display: "flex",
53
+ flexDirection: "column",
54
+ alignItems: "center",
55
+ gap: "4"
56
+ }
57
+ }
58
+ }, /* @__PURE__ */React.createElement(CloseButton, {
59
+ "aria-label": "Close",
60
+ sx: {
61
+ alignSelf: "flex-end"
62
+ },
63
+ onClick: /* @__PURE__ */__name(() => {
64
+ clearNotifications();
65
+ }, "onClick")
66
+ }), /* @__PURE__ */React.createElement(NotificationsBox, {
67
+ notifications: modalNotifications
68
+ }));
69
+ }, "NotificationsModal");
70
+
71
+ // src/Provider.tsx
72
+ var NotificationsContext = /* @__PURE__ */React2.createContext({
73
+ isLoading: false,
74
+ setLoading: /* @__PURE__ */__name(() => {
75
+ return void 0;
76
+ }, "setLoading"),
77
+ defaultViewType: "box",
78
+ addNotification: /* @__PURE__ */__name(() => {
79
+ return void 0;
80
+ }, "addNotification"),
81
+ removeNotification: /* @__PURE__ */__name(() => {
82
+ return void 0;
83
+ }, "removeNotification"),
84
+ clearNotifications: /* @__PURE__ */__name(() => {
85
+ return void 0;
86
+ }, "clearNotifications")
87
+ });
88
+ var NotificationsProvider = /* @__PURE__ */__name(props => {
89
+ const [isLoading, setLoading] = React2.useState(false);
90
+ const [notifications, setNotifications] = React2.useState();
91
+ const prefix = React2.useId();
92
+ const removeNotification = React2.useCallback(id => {
93
+ setNotifications(prevNotifications => {
94
+ return prevNotifications?.filter(notification => {
95
+ return notification.id !== id;
96
+ });
97
+ });
98
+ }, []);
99
+ const addNotification = React2.useCallback(notification => {
100
+ const newNotifications = (Array.isArray(notification) ? notification : [notification]).map(notification2 => {
101
+ const id = notification2.id || `${prefix}-${Math.random()}`;
102
+ return {
103
+ ...notification2,
104
+ id
105
+ };
106
+ }).filter((notification2, index, notifications2) => {
107
+ return notifications2.findIndex(n => {
108
+ return n.id === notification2.id;
109
+ }) === index;
110
+ });
111
+ const toastNotifications = newNotifications.filter(notification2 => {
112
+ if (notification2.viewType === "toast") {
113
+ return true;
114
+ }
115
+ if (!notification2.viewType && props.defaultViewType === "toast") {
116
+ return true;
117
+ }
118
+ return false;
119
+ });
120
+ toastNotifications.forEach(notification2 => {
121
+ toast(notification2.message, {
122
+ ...notification2.toast,
123
+ type: notification2.type,
124
+ onClose: /* @__PURE__ */__name(() => {
125
+ removeNotification(notification2.id);
126
+ notification2.toast?.onClose?.();
127
+ }, "onClose")
128
+ });
129
+ });
130
+ setNotifications((prevNotifications = []) => {
131
+ const nonToastNewNotifications = newNotifications.filter(notification2 => {
132
+ if (notification2.viewType === "toast") {
133
+ return false;
134
+ }
135
+ if (!notification2.viewType && props.defaultViewType === "toast") {
136
+ return false;
137
+ }
138
+ return true;
139
+ });
140
+ const oldNotifications = prevNotifications.filter(prevNotification => {
141
+ return !nonToastNewNotifications.some(newNotification => {
142
+ return newNotification.id === prevNotification.id;
143
+ });
144
+ });
145
+ return [...oldNotifications, ...nonToastNewNotifications];
146
+ });
147
+ }, [prefix, props.defaultViewType, removeNotification]);
148
+ const clearNotifications = React2.useCallback(() => {
149
+ setNotifications(prevNotifications => {
150
+ return prevNotifications?.filter(notification => {
151
+ return notification.persist === true;
152
+ });
153
+ });
154
+ }, []);
155
+ return /* @__PURE__ */React2.createElement(NotificationsContext.Provider, {
156
+ value: {
157
+ isLoading,
158
+ setLoading,
159
+ notifications,
160
+ addNotification,
161
+ removeNotification,
162
+ clearNotifications,
163
+ defaultViewType: props.defaultViewType || "box"
164
+ }
165
+ }, /* @__PURE__ */React2.createElement(ToastContainer, props.toast), /* @__PURE__ */React2.createElement(NotificationsModal, null), isLoading && /* @__PURE__ */React2.createElement(Flex, {
166
+ sx: {
167
+ position: "absolute",
168
+ width: "100%",
169
+ top: 0
170
+ }
171
+ }, /* @__PURE__ */React2.createElement(InfiniteLinearProgress, null)), /* @__PURE__ */React2.createElement(NotificationsHeader, null), props.children);
172
+ }, "NotificationsProvider");
173
+ var useNotifications = /* @__PURE__ */__name(() => {
174
+ return React2.useContext(NotificationsContext);
175
+ }, "useNotifications");
176
+
177
+ // src/NotificationsBox.tsx
178
+ var NotificationsBox = /* @__PURE__ */__name(props => {
179
+ const {
180
+ notifications,
181
+ removeNotification,
182
+ defaultViewType
183
+ } = useNotifications();
184
+ const boxNotifications = props.notifications || notifications?.filter(notification => {
185
+ if (notification.viewType !== "box" && defaultViewType !== "box") {
186
+ return false;
187
+ }
188
+ if (!props.id && notification.boxId) {
189
+ return false;
190
+ }
191
+ if (!props.id && !notification.boxId) {
192
+ return true;
193
+ }
194
+ return notification.boxId === props.id;
195
+ });
196
+ const hasBoxNotifications = Array.isArray(boxNotifications);
197
+ if (!hasBoxNotifications) {
198
+ return null;
199
+ }
200
+ return /* @__PURE__ */React.createElement(Stack, {
201
+ sx: {
202
+ width: "full",
203
+ gap: "1"
204
+ }
205
+ }, boxNotifications.map(notification => {
206
+ return /* @__PURE__ */React.createElement(NotificationCard, {
207
+ key: notification.id,
208
+ type: notification.type,
209
+ title: notification.title,
210
+ message: notification.message,
211
+ onClose: /* @__PURE__ */__name(() => {
212
+ if (notification.id) {
213
+ removeNotification(notification.id);
214
+ }
215
+ }, "onClose")
216
+ });
217
+ }));
218
+ }, "NotificationsBox");
219
+ export { NotificationsBox, NotificationsProvider, toast, useNotifications };
@@ -0,0 +1,38 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ToastOptions, ToastContainerProps } from '@ttoss/components/Toast';
3
+ export { toast } from '@ttoss/components/Toast';
4
+ import * as React from 'react';
5
+
6
+ type ViewType = 'toast' | 'modal' | 'box' | 'header';
7
+ type Notification = {
8
+ id?: string | number;
9
+ title?: string;
10
+ message: string;
11
+ type: 'warning' | 'error' | 'info' | 'success';
12
+ viewType?: ViewType;
13
+ toast?: ToastOptions;
14
+ boxId?: string | number;
15
+ persist?: boolean;
16
+ };
17
+ type NotificationsProviderProps = {
18
+ children: React.ReactNode;
19
+ defaultViewType?: ViewType;
20
+ toast?: ToastContainerProps;
21
+ };
22
+ declare const NotificationsProvider: (props: NotificationsProviderProps) => react_jsx_runtime.JSX.Element;
23
+ declare const useNotifications: () => {
24
+ isLoading: boolean;
25
+ setLoading: (arg: boolean) => void;
26
+ defaultViewType: ViewType;
27
+ notifications?: Notification[];
28
+ addNotification(notification: Notification | Notification[]): void;
29
+ removeNotification(id: string | number): void;
30
+ clearNotifications(): void;
31
+ };
32
+
33
+ declare const NotificationsBox: (props: {
34
+ id?: string | number;
35
+ notifications?: Notification[] | undefined;
36
+ }) => react_jsx_runtime.JSX.Element | null;
37
+
38
+ export { type Notification, NotificationsBox, NotificationsProvider, type NotificationsProviderProps, useNotifications };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/react-notifications",
3
- "version": "2.4.39",
3
+ "version": "2.5.0",
4
4
  "description": "ttoss notifications module for React apps.",
5
5
  "license": "MIT",
6
6
  "author": "ttoss",
@@ -25,19 +25,19 @@
25
25
  "sideEffects": false,
26
26
  "peerDependencies": {
27
27
  "react": ">=16.8.0",
28
- "@ttoss/components": "^2.10.2",
29
- "@ttoss/ui": "^6.1.0",
30
- "@ttoss/react-icons": "^0.5.6"
28
+ "@ttoss/react-icons": "^0.5.6",
29
+ "@ttoss/ui": "^6.2.0",
30
+ "@ttoss/components": "^2.11.0"
31
31
  },
32
32
  "devDependencies": {
33
33
  "@types/react": "^19.2.7",
34
34
  "jest": "^30.2.0",
35
35
  "react": "^19.2.1",
36
36
  "tsup": "^8.5.1",
37
- "@ttoss/config": "^1.35.12",
38
- "@ttoss/components": "^2.10.2",
37
+ "@ttoss/components": "^2.11.0",
39
38
  "@ttoss/react-icons": "^0.5.6",
40
- "@ttoss/ui": "^6.1.0",
39
+ "@ttoss/ui": "^6.2.0",
40
+ "@ttoss/config": "^1.35.12",
41
41
  "@ttoss/test-utils": "^4.0.2"
42
42
  },
43
43
  "keywords": [