Skip to content

Commit 4caa0a1

Browse files
committed
refactor: clean up GlobalSnackbar
1 parent 5cfbc8e commit 4caa0a1

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

site/src/components/GlobalSnackbar/GlobalSnackbar.tsx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,37 @@ const variantFromMsgType = (type: MsgType) => {
2424
};
2525

2626
export const GlobalSnackbar: FC = () => {
27-
const [open, setOpen] = useState<boolean>(false);
28-
const [notification, setNotification] = useState<NotificationMsg>();
29-
27+
const [notificationMsg, setNotificationMsg] = useState<NotificationMsg>();
3028
useCustomEvent<NotificationMsg>(SnackbarEventType, (event) => {
31-
setNotification(event.detail);
32-
setOpen(true);
29+
setNotificationMsg(event.detail);
3330
});
3431

35-
if (!notification) {
32+
const hasNotification = notificationMsg !== undefined;
33+
if (!hasNotification) {
3634
return null;
3735
}
3836

3937
return (
4038
<EnterpriseSnackbar
41-
key={notification.msg}
42-
open={open}
43-
variant={variantFromMsgType(notification.msgType)}
44-
onClose={() => setOpen(false)}
45-
autoHideDuration={notification.msgType === MsgType.Error ? 22000 : 6000}
39+
key={notificationMsg.msg}
40+
open={hasNotification}
41+
variant={variantFromMsgType(notificationMsg.msgType)}
42+
onClose={() => setNotificationMsg(undefined)}
43+
autoHideDuration={
44+
notificationMsg.msgType === MsgType.Error ? 22000 : 6000
45+
}
4646
anchorOrigin={{ vertical: "bottom", horizontal: "right" }}
4747
message={
4848
<div css={{ display: "flex" }}>
49-
{notification.msgType === MsgType.Error && (
49+
{notificationMsg.msgType === MsgType.Error && (
5050
<ErrorIcon css={styles.errorIcon} />
5151
)}
5252

5353
<div css={{ maxWidth: 670 }}>
54-
<span css={styles.messageTitle}>{notification.msg}</span>
54+
<span css={styles.messageTitle}>{notificationMsg.msg}</span>
5555

56-
{notification.additionalMsgs &&
57-
notification.additionalMsgs.map((msg, index) => (
56+
{notificationMsg.additionalMsgs &&
57+
notificationMsg.additionalMsgs.map((msg, index) => (
5858
<AdditionalMessageDisplay key={index} message={msg} />
5959
))}
6060
</div>

0 commit comments

Comments
 (0)