// @flow import * as ICONS from 'constants/icons'; import { NOTIFICATION_COMMENT } from 'constants/notifications'; import React from 'react'; import Page from 'component/page'; import Card from 'component/common/card'; import Spinner from 'component/spinner'; import Notification from 'component/notification'; import Yrbl from 'component/yrbl'; import Button from 'component/button'; type Props = { notifications: ?Array, fetching: boolean, unreadCount: number, doReadNotifications: () => void, }; export default function NotificationsPage(props: Props) { const { notifications, fetching, unreadCount, doReadNotifications } = props; // Group sequential comment notifications if they are by the same author let groupedCount = 1; const groupedNotifications = notifications && notifications.reduce((list, notification, index) => { if (index === 0) { return [notification]; } const previousNotification = notifications[index - 1]; const isCommentNotification = notification.notification_rule === NOTIFICATION_COMMENT; const previousIsCommentNotification = previousNotification.notification_rule === NOTIFICATION_COMMENT; if (isCommentNotification && previousIsCommentNotification) { const notificationTarget = notification.notification_parameters.device.target; const previousTarget = previousNotification && previousNotification.notification_parameters.device.target; const author = notification.notification_parameters.dynamic.comment_author; const previousAuthor = previousNotification.notification_parameters.dynamic.comment_author; if (author === previousAuthor && notificationTarget === previousTarget) { const newList = [...list]; newList.pop(); groupedCount += 1; const newNotification = { ...previousNotification, group_count: groupedCount, }; newList[index - groupedCount] = newNotification; return newList; } else { if (groupedCount > 1) { groupedCount = 1; } return [...list, notification]; } } else { if (groupedCount > 1) { groupedCount = 1; } return [...list, notification]; } }, []); React.useEffect(() => { if (unreadCount > 0) { doReadNotifications(); } }, [unreadCount, doReadNotifications]); return ( {fetching && (
)} {groupedNotifications && groupedNotifications.length > 0 ? ( {groupedNotifications.map((notification, index) => { if (!notification) { return null; } return ; })} } /> ) : (

{__("You don't have any notifications yet, but they will be here when you do!")}

} /> )}
); }