import { IconBell } from '@tabler/icons'; import { useState } from 'react'; import StyledWrapper from './StyleWrapper'; import Modal from 'components/Modal/index'; import Portal from 'components/Portal'; import { useEffect } from 'react'; import { useApp } from 'providers/App'; import { fetchNotifications, markAllNotificationsAsRead, markNotificationAsRead } from 'providers/ReduxStore/slices/notifications'; import { useDispatch, useSelector } from 'react-redux'; import { humanizeDate, relativeDate } from 'utils/common'; import ToolHint from 'components/ToolHint'; import DOMPurify from 'dompurify'; const PAGE_SIZE = 5; const Notifications = () => { const dispatch = useDispatch(); const { version } = useApp(); const notifications = useSelector((state) => state.notifications.notifications); const [showNotificationsModal, setShowNotificationsModal] = useState(false); const [selectedNotification, setSelectedNotification] = useState(null); const [pageNumber, setPageNumber] = useState(1); const notificationsStartIndex = (pageNumber - 1) * PAGE_SIZE; const notificationsEndIndex = pageNumber * PAGE_SIZE; const totalPages = Math.ceil(notifications.length / PAGE_SIZE); const unreadNotifications = notifications.filter((notification) => !notification.read); useEffect(() => { dispatch(fetchNotifications({ currentVersion: version })); }, []); useEffect(() => { reset(); }, [showNotificationsModal]); useEffect(() => { if (!selectedNotification && notifications?.length > 0 && showNotificationsModal) { let firstNotification = notifications[0]; setSelectedNotification(firstNotification); dispatch(markNotificationAsRead({ notificationId: firstNotification?.id })); } }, [notifications, selectedNotification, showNotificationsModal]); const reset = () => { setSelectedNotification(null); setPageNumber(1); }; const handlePrev = (e) => { if (pageNumber - 1 < 1) return; setPageNumber(pageNumber - 1); }; const handleNext = (e) => { if (pageNumber + 1 > totalPages) return; setPageNumber(pageNumber + 1); }; const handleNotificationItemClick = (notification) => (e) => { e.preventDefault(); setSelectedNotification(notification); dispatch(markNotificationAsRead({ notificationId: notification?.id })); }; const getSanitizedDescription = (description) => { return DOMPurify.sanitize(encodeURIComponent(description), { ALLOWED_TAGS: ['a', 'ul', 'img', 'li', 'div', 'span', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'], ALLOWED_ATTR: ['href', 'style', 'target', 'src', 'alt'] }); }; const modalCustomHeader = (