2022-09-23 05:17:44 +00:00
|
|
|
import clsx from 'clsx';
|
|
|
|
import {
|
|
|
|
Menu,
|
|
|
|
MenuButton,
|
|
|
|
MenuList,
|
|
|
|
MenuLink as ReachMenuLink,
|
|
|
|
} from '@reach/menu-button';
|
|
|
|
import { UISrefProps, useSref } from '@uirouter/react';
|
|
|
|
import Moment from 'moment';
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { useStore } from 'zustand';
|
|
|
|
|
|
|
|
import { AutomationTestingProps } from '@/types';
|
|
|
|
import { useUser } from '@/portainer/hooks/useUser';
|
|
|
|
import { ToastNotification } from '@/react/portainer/notifications/types';
|
|
|
|
|
|
|
|
import { Icon } from '@@/Icon';
|
|
|
|
import { Link } from '@@/Link';
|
|
|
|
import { Button } from '@@/buttons';
|
|
|
|
|
|
|
|
import { notificationsStore } from '../../portainer/notifications/notifications-store';
|
|
|
|
|
|
|
|
import headerStyles from './HeaderTitle.module.css';
|
|
|
|
import notificationStyles from './NotificationsMenu.module.css';
|
|
|
|
|
|
|
|
export function NotificationsMenu() {
|
|
|
|
const notificationsStoreState = useStore(notificationsStore);
|
|
|
|
const { removeNotification } = notificationsStoreState;
|
|
|
|
const { clearUserNotifications } = notificationsStoreState;
|
|
|
|
|
|
|
|
const { user } = useUser();
|
|
|
|
const userNotifications: ToastNotification[] = useStore(
|
|
|
|
notificationsStore,
|
|
|
|
(state) => state.userNotifications[user.Id]
|
|
|
|
);
|
|
|
|
|
2022-10-11 01:05:53 +00:00
|
|
|
if (userNotifications && userNotifications.length > 1) {
|
|
|
|
userNotifications.sort(
|
|
|
|
(a, b) =>
|
|
|
|
new Date(b.timeStamp).getTime() - new Date(a.timeStamp).getTime()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-23 05:17:44 +00:00
|
|
|
const [badge, setBadge] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (userNotifications?.length > 0) {
|
|
|
|
setBadge(true);
|
|
|
|
} else {
|
|
|
|
setBadge(false);
|
|
|
|
}
|
|
|
|
}, [userNotifications]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Menu>
|
|
|
|
<MenuButton
|
|
|
|
className={clsx(
|
|
|
|
'ml-auto flex items-center gap-1 self-start',
|
|
|
|
headerStyles.menuButton
|
|
|
|
)}
|
|
|
|
data-cy="notificationsMenu-button"
|
|
|
|
aria-label="Notifications menu toggle"
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
headerStyles.menuIcon,
|
|
|
|
'icon-badge text-lg !p-2 mr-1',
|
|
|
|
'text-gray-8',
|
|
|
|
'th-dark:text-gray-warm-7'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Icon icon="bell" feather />
|
2022-10-11 01:05:53 +00:00
|
|
|
<span className={badge ? notificationStyles.badge : ''} />
|
2022-09-23 05:17:44 +00:00
|
|
|
</div>
|
|
|
|
</MenuButton>
|
|
|
|
|
|
|
|
<MenuList
|
2022-10-11 01:05:53 +00:00
|
|
|
className={clsx(headerStyles.menuList, notificationStyles.root)}
|
2022-09-23 05:17:44 +00:00
|
|
|
aria-label="Notifications Menu"
|
|
|
|
data-cy="notificationsMenu"
|
|
|
|
>
|
|
|
|
<div>
|
2022-10-11 01:05:53 +00:00
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
notificationStyles.notificationContainer,
|
|
|
|
'vertical-center'
|
|
|
|
)}
|
|
|
|
>
|
2022-09-23 05:17:44 +00:00
|
|
|
<div>
|
|
|
|
<h4>Notifications</h4>
|
|
|
|
</div>
|
2022-10-11 01:05:53 +00:00
|
|
|
<div className={notificationStyles.itemLast}>
|
2022-09-23 05:17:44 +00:00
|
|
|
{userNotifications?.length > 0 && (
|
|
|
|
<Button
|
|
|
|
color="none"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
onClear();
|
|
|
|
}}
|
|
|
|
data-cy="notification-deleteButton"
|
|
|
|
>
|
|
|
|
Clear all
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{userNotifications?.length > 0 ? (
|
|
|
|
<>
|
2022-10-11 01:05:53 +00:00
|
|
|
<div className={notificationStyles.notifications}>
|
|
|
|
{userNotifications.map((notification) => (
|
|
|
|
<MenuLink
|
|
|
|
to="portainer.notifications"
|
|
|
|
params={{ id: notification.id }}
|
|
|
|
notification={notification}
|
|
|
|
key={notification.id}
|
|
|
|
onDelete={() => onDelete(notification.id)}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={notificationStyles.notificationLink}>
|
2022-09-23 05:17:44 +00:00
|
|
|
<Link to="portainer.notifications">View all notifications</Link>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : (
|
2022-10-11 01:05:53 +00:00
|
|
|
<div className="flex flex-col items-center">
|
2022-09-23 05:17:44 +00:00
|
|
|
<Icon icon="bell" feather size="xl" />
|
2022-10-11 01:05:53 +00:00
|
|
|
<p className="my-5">You have no notifications yet.</p>
|
2022-09-23 05:17:44 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</MenuList>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
|
|
|
|
function onDelete(notificationId: string) {
|
|
|
|
removeNotification(user.Id, notificationId);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onClear() {
|
|
|
|
clearUserNotifications(user.Id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MenuLinkProps extends AutomationTestingProps, UISrefProps {
|
|
|
|
notification: ToastNotification;
|
|
|
|
onDelete: () => void;
|
|
|
|
}
|
|
|
|
|
2022-10-11 01:05:53 +00:00
|
|
|
function MenuLink({ to, params, notification, onDelete }: MenuLinkProps) {
|
|
|
|
const anchorProps = useSref(to, params);
|
2022-09-23 05:17:44 +00:00
|
|
|
|
|
|
|
return (
|
2022-10-11 01:05:53 +00:00
|
|
|
<ReachMenuLink
|
|
|
|
href={anchorProps.href}
|
|
|
|
onClick={anchorProps.onClick}
|
|
|
|
className={clsx(headerStyles.menuLink, notificationStyles.notification)}
|
|
|
|
>
|
|
|
|
<div className={notificationStyles.container}>
|
|
|
|
<div className={notificationStyles.notificationIcon}>
|
2022-09-23 05:17:44 +00:00
|
|
|
{notification.type === 'success' ? (
|
|
|
|
<Icon icon="check-circle" feather size="lg" mode="success" />
|
|
|
|
) : (
|
|
|
|
<Icon icon="alert-circle" feather size="lg" mode="danger" />
|
|
|
|
)}
|
|
|
|
</div>
|
2022-10-11 01:05:53 +00:00
|
|
|
<div className={notificationStyles.notificationBody}>
|
|
|
|
<p className={notificationStyles.notificationTitle}>
|
2022-09-23 05:17:44 +00:00
|
|
|
{notification.title}
|
|
|
|
</p>
|
2022-10-11 01:05:53 +00:00
|
|
|
<p className={notificationStyles.notificationDetails}>
|
|
|
|
{notification.details}
|
|
|
|
</p>
|
2022-09-23 05:17:44 +00:00
|
|
|
<p className="small text-muted">
|
|
|
|
{formatTime(notification.timeStamp)}
|
|
|
|
</p>
|
|
|
|
</div>
|
2022-10-11 01:05:53 +00:00
|
|
|
<div className={notificationStyles.deleteButton}>
|
2022-09-23 05:17:44 +00:00
|
|
|
<Button
|
|
|
|
color="none"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
onDelete();
|
|
|
|
}}
|
|
|
|
data-cy="notification-deleteButton"
|
|
|
|
size="large"
|
|
|
|
>
|
|
|
|
<Icon icon="trash-2" feather />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</ReachMenuLink>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatTime(timeCreated: Date) {
|
|
|
|
const timeStamp = new Date(timeCreated).valueOf().toString();
|
|
|
|
|
|
|
|
const diff = Math.floor((Date.now() - parseInt(timeStamp, 10)) / 1000);
|
|
|
|
|
|
|
|
if (diff <= 86400) {
|
|
|
|
let interval = Math.floor(diff / 3600);
|
|
|
|
if (interval >= 1) {
|
|
|
|
return `${interval} hours ago`;
|
|
|
|
}
|
|
|
|
interval = Math.floor(diff / 60);
|
|
|
|
if (interval >= 1) {
|
|
|
|
return `${interval} min ago`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (diff > 86400) {
|
|
|
|
const formatDate = Moment(timeCreated).format('YYYY-MM-DD h:mm:ss');
|
|
|
|
return formatDate;
|
|
|
|
}
|
|
|
|
return 'Just now';
|
|
|
|
}
|