import {type Notification, NOTIFICATIONS_PER_PAGE} from 'common/notifications' import {type User} from 'common/src/user' import {Fragment, useEffect, useMemo, useState} from 'react' import {Col} from 'web/components/layout/col' import {UncontrolledTabs} from 'web/components/layout/tabs' import {NoSEO} from 'web/components/NoSEO' import {NotificationItem} from 'web/components/notification-items' import {NotificationSettings} from 'web/components/notifications' import {PageBase} from 'web/components/page-base' import {CompassLoadingIndicator} from 'web/components/widgets/loading-indicator' import {Pagination} from 'web/components/widgets/pagination' import {Title} from 'web/components/widgets/title' import {useGroupedNotifications} from 'web/hooks/use-notifications' import {useRedirectIfSignedOut} from 'web/hooks/use-redirect-if-signed-out' import {usePrivateUser, useUser} from 'web/hooks/use-user' import {api} from 'web/lib/api' import {useT} from 'web/lib/locale' export default function NotificationsPage() { useRedirectIfSignedOut() const t = useT() return ( {t('notifications.title', 'Updates')} , }, { title: t('notifications.tabs.settings', 'Settings'), content: , }, ]} trackingName={'notifications page'} /> ) } const NotificationsContent = () => { const user = useUser() if (!user) return return } function LoadedNotificationsContent(props: {user: User}) { const {user} = props const t = useT() const privateUser = usePrivateUser() const {groupedNotifications, mostRecentNotification} = useGroupedNotifications( user, // NOTIFICATION_TYPES_TO_SELECT ) const [page, setPage] = useState(0) const paginatedGroupedNotifications = useMemo(() => { const start = page * NOTIFICATIONS_PER_PAGE const end = start + NOTIFICATIONS_PER_PAGE return groupedNotifications?.slice(start, end) }, [groupedNotifications, page]) // Mark all notifications as seen. Rerun as new notifications come in. useEffect(() => { if (!privateUser) return api('mark-all-notifs-read', {seen: true}) groupedNotifications ?.map((ng) => ng.notifications) .flat() .forEach((n) => (!n.isSeen ? (n.isSeen = true) : null)) }, [privateUser, mostRecentNotification?.id]) if (!privateUser) return null return (
{groupedNotifications === undefined || paginatedGroupedNotifications === undefined ? ( ) : paginatedGroupedNotifications.length === 0 ? (
{t('notifications.empty', "You don't have any notifications, yet.")}
) : ( )}
) } export type NotificationGroup = { notifications: Notification[] groupedById: string isSeen: boolean } function RenderNotificationGroups(props: { notificationGroups: NotificationGroup[] totalItems: number page: number setPage: (page: number) => void }) { const {notificationGroups, page, setPage, totalItems} = props return ( <> {notificationGroups.map((notification) => { return notification.notifications.map((notification: Notification) => (
)) })} {notificationGroups.length > 0 && totalItems > NOTIFICATIONS_PER_PAGE && ( )} ) }