@ttoss/components 2.2.16 → 2.2.18
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,175 @@
|
|
|
1
|
+
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
+
import { Icon, NotificationCard } from "../chunk-ERHWYNDS.js";
|
|
3
|
+
|
|
4
|
+
// src/components/NotificationsMenu/NotificationsMenu.tsx
|
|
5
|
+
import { Box, Card, Flex, IconButton, Link, Text } from "@ttoss/ui";
|
|
6
|
+
import * as React from "react";
|
|
7
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
var renderMessage = (message, actions) => {
|
|
9
|
+
if (!actions || actions.length === 0) return message;
|
|
10
|
+
return /* @__PURE__ */jsxs(Flex, {
|
|
11
|
+
sx: {
|
|
12
|
+
flexDirection: "column",
|
|
13
|
+
gap: 2
|
|
14
|
+
},
|
|
15
|
+
children: [/* @__PURE__ */jsx(Text, {
|
|
16
|
+
children: message
|
|
17
|
+
}), actions.map((action, index) => {
|
|
18
|
+
if (action.action === "open_url") {
|
|
19
|
+
return /* @__PURE__ */jsx(Link, {
|
|
20
|
+
href: action.url,
|
|
21
|
+
target: "_blank",
|
|
22
|
+
rel: "noopener noreferrer",
|
|
23
|
+
sx: {
|
|
24
|
+
color: "action.text.accent.default",
|
|
25
|
+
cursor: "pointer"
|
|
26
|
+
},
|
|
27
|
+
children: action.label || "Acessar"
|
|
28
|
+
}, index);
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
})]
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var NotificationsMenu = ({
|
|
35
|
+
notifications,
|
|
36
|
+
defaultOpen = false,
|
|
37
|
+
hasMore = false,
|
|
38
|
+
onLoadMore,
|
|
39
|
+
onOpenChange,
|
|
40
|
+
unreadCount,
|
|
41
|
+
onClose
|
|
42
|
+
}) => {
|
|
43
|
+
const [isOpen, setIsOpen] = React.useState(defaultOpen);
|
|
44
|
+
const [openToLeft, setOpenToLeft] = React.useState(false);
|
|
45
|
+
const buttonRef = React.useRef(null);
|
|
46
|
+
const containerRef = React.useRef(null);
|
|
47
|
+
const loadMoreRef = React.useRef(null);
|
|
48
|
+
const unread = unreadCount ?? notifications.length;
|
|
49
|
+
const togglePanel = () => {
|
|
50
|
+
setIsOpen(prev => {
|
|
51
|
+
const next = !prev;
|
|
52
|
+
onOpenChange?.(next);
|
|
53
|
+
return next;
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
React.useEffect(() => {
|
|
57
|
+
if (!isOpen || !buttonRef.current) return;
|
|
58
|
+
const rect = buttonRef.current.getBoundingClientRect();
|
|
59
|
+
const spaceRight = window.innerWidth - rect.right;
|
|
60
|
+
const spaceLeft = rect.left;
|
|
61
|
+
setOpenToLeft(spaceRight < 400 && spaceLeft > spaceRight);
|
|
62
|
+
}, [isOpen]);
|
|
63
|
+
React.useEffect(() => {
|
|
64
|
+
if (!hasMore || !onLoadMore || !loadMoreRef.current) return;
|
|
65
|
+
const observer = new IntersectionObserver(entries => {
|
|
66
|
+
if (entries[0].isIntersecting) {
|
|
67
|
+
onLoadMore();
|
|
68
|
+
}
|
|
69
|
+
}, {
|
|
70
|
+
root: null,
|
|
71
|
+
rootMargin: "0px",
|
|
72
|
+
threshold: 1
|
|
73
|
+
});
|
|
74
|
+
observer.observe(loadMoreRef.current);
|
|
75
|
+
return () => {
|
|
76
|
+
if (loadMoreRef.current) {
|
|
77
|
+
observer.unobserve(loadMoreRef.current);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}, [hasMore, onLoadMore, notifications.length]);
|
|
81
|
+
return /* @__PURE__ */jsx(Flex, {
|
|
82
|
+
sx: {
|
|
83
|
+
position: "relative",
|
|
84
|
+
justifyContent: "flex-start"
|
|
85
|
+
},
|
|
86
|
+
children: /* @__PURE__ */jsxs(Box, {
|
|
87
|
+
sx: {
|
|
88
|
+
position: "relative"
|
|
89
|
+
},
|
|
90
|
+
children: [/* @__PURE__ */jsxs(IconButton, {
|
|
91
|
+
ref: buttonRef,
|
|
92
|
+
variant: "ghost",
|
|
93
|
+
sx: {
|
|
94
|
+
position: "relative",
|
|
95
|
+
borderRadius: "full",
|
|
96
|
+
padding: 2,
|
|
97
|
+
transition: "background-color 0.3s ease",
|
|
98
|
+
"&:hover": {
|
|
99
|
+
backgroundColor: "action.background.muted.default"
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
onClick: togglePanel,
|
|
103
|
+
children: [/* @__PURE__ */jsx(Icon, {
|
|
104
|
+
icon: "mdi:bell-outline",
|
|
105
|
+
width: 25,
|
|
106
|
+
height: 25
|
|
107
|
+
}), unread > 0 && /* @__PURE__ */jsx(Box, {
|
|
108
|
+
sx: {
|
|
109
|
+
position: "absolute",
|
|
110
|
+
top: "-4px",
|
|
111
|
+
right: "-4px",
|
|
112
|
+
minWidth: "16px",
|
|
113
|
+
height: "16px",
|
|
114
|
+
paddingX: 1,
|
|
115
|
+
borderRadius: "full",
|
|
116
|
+
backgroundColor: "action.background.negative.default",
|
|
117
|
+
color: "feedback.text.primary.default",
|
|
118
|
+
fontSize: "xs",
|
|
119
|
+
fontWeight: "bold",
|
|
120
|
+
display: "flex",
|
|
121
|
+
alignItems: "center",
|
|
122
|
+
justifyContent: "center",
|
|
123
|
+
lineHeight: 1
|
|
124
|
+
},
|
|
125
|
+
children: unread > 99 ? "99+" : unread
|
|
126
|
+
})]
|
|
127
|
+
}), isOpen && /* @__PURE__ */jsx(Card, {
|
|
128
|
+
sx: {
|
|
129
|
+
position: "absolute",
|
|
130
|
+
top: "calc(100% + 8px)",
|
|
131
|
+
left: openToLeft ? "auto" : 0,
|
|
132
|
+
right: openToLeft ? 0 : "auto",
|
|
133
|
+
width: ["90vw", "400px"],
|
|
134
|
+
maxHeight: "400px",
|
|
135
|
+
overflowY: "auto",
|
|
136
|
+
zIndex: 10,
|
|
137
|
+
padding: 0,
|
|
138
|
+
boxShadow: "xl",
|
|
139
|
+
borderRadius: "2xl",
|
|
140
|
+
backgroundColor: "display.background.secondary.default"
|
|
141
|
+
},
|
|
142
|
+
children: /* @__PURE__ */jsx(Box, {
|
|
143
|
+
ref: containerRef,
|
|
144
|
+
children: /* @__PURE__ */jsxs(Flex, {
|
|
145
|
+
sx: {
|
|
146
|
+
flexDirection: "column"
|
|
147
|
+
},
|
|
148
|
+
children: [notifications.length === 0 && /* @__PURE__ */jsx(Text, {
|
|
149
|
+
sx: {
|
|
150
|
+
color: "display.text.muted.default",
|
|
151
|
+
textAlign: "center",
|
|
152
|
+
p: 4
|
|
153
|
+
},
|
|
154
|
+
children: "Nenhuma notifica\xE7\xE3o"
|
|
155
|
+
}), notifications.map(notification => {
|
|
156
|
+
return /* @__PURE__ */jsx(NotificationCard, {
|
|
157
|
+
type: notification.type,
|
|
158
|
+
title: notification.title,
|
|
159
|
+
message: renderMessage(notification.message, notification.actions),
|
|
160
|
+
onClose,
|
|
161
|
+
metaInfo: notification.createdAt
|
|
162
|
+
}, notification.id);
|
|
163
|
+
}), hasMore && /* @__PURE__ */jsx("div", {
|
|
164
|
+
ref: loadMoreRef,
|
|
165
|
+
style: {
|
|
166
|
+
height: 1
|
|
167
|
+
}
|
|
168
|
+
})]
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
})]
|
|
172
|
+
})
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
export { NotificationsMenu };
|