import React from 'react'; import Typography from '@mui/material/Typography'; import Divider from '@mui/material/Divider'; import Box from '@mui/material/Box'; import IconButton from '@mui/material/IconButton'; import CloseOutlinedIcon from '@mui/icons-material/CloseOutlined'; import { HermesNotification, OnNotificationClick } from './types'; import NotificationBox from './NotificationBox'; interface NotificationListProps { notifications: HermesNotification[]; onClickClose: () => void; title?: string; onNotificationClick?: OnNotificationClick } const sortByRecentDates = (list: HermesNotification[]) => { if (list.length === 0) { return [] } return list.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()); } const NotificationList = (props: NotificationListProps) => { const { notifications, onClickClose, title, onNotificationClick } = props; const [sortedNotifications, setSortedNotifications] = React.useState<{ today: HermesNotification[]; yesterday: HermesNotification[]; } | null>(null); React.useEffect(() => { if (notifications) { const today = new Date(); const todayList: HermesNotification[] = []; const yesterdayList: HermesNotification[] = []; notifications.forEach((notification) => { const notificationDate = new Date(notification.createdAt).getDate(); if (notificationDate === today.getDate()) { todayList.push(notification); } else if (notificationDate === today.getDate() - 1) { yesterdayList.push(notification); } }); setSortedNotifications({ yesterday: sortByRecentDates(yesterdayList), today: sortByRecentDates(todayList) }); } }, [notifications]); return ( {title} {!!sortedNotifications?.today.length && ( <> Hoy {sortedNotifications.today.map((notification) => ( ))} )} {!!sortedNotifications?.yesterday.length && ( <> Ayer {sortedNotifications.yesterday.map((notification) => ( ))} )} ); }; NotificationList.defaultProps = { title: 'Notificaciones', }; export default NotificationList;