import './notifications.css'; import { Fragment } from 'preact'; import { memo } from 'preact/compat'; import { useCallback, useEffect, useRef, useState } from 'preact/hooks'; import { InView } from 'react-intersection-observer'; import { useSearchParams } from 'react-router-dom'; import { useSnapshot } from 'valtio'; import { subscribeKey } from 'valtio/utils'; import AccountBlock from '../components/account-block'; import FollowRequestButtons from '../components/follow-request-buttons'; import Icon from '../components/icon'; import Link from '../components/link'; import Loader from '../components/loader'; import NavMenu from '../components/nav-menu'; import Notification from '../components/notification'; import { api } from '../utils/api'; import enhanceContent from '../utils/enhance-content'; import groupNotifications from '../utils/group-notifications'; import handleContentLinks from '../utils/handle-content-links'; import niceDateTime from '../utils/nice-date-time'; import { getRegistration } from '../utils/push-notifications'; import shortenNumber from '../utils/shorten-number'; import states, { saveStatus } from '../utils/states'; import { getCurrentInstance } from '../utils/store-utils'; import usePageVisibility from '../utils/usePageVisibility'; import useScroll from '../utils/useScroll'; import useTitle from '../utils/useTitle'; const LIMIT = 30; // 30 is the maximum limit :( const emptySearchParams = new URLSearchParams(); function Notifications({ columnMode }) { useTitle('Notifications', '/notifications'); const { masto, instance } = api(); const snapStates = useSnapshot(states); const [uiState, setUIState] = useState('default'); const [searchParams] = columnMode ? [emptySearchParams] : useSearchParams(); const notificationID = searchParams.get('id'); const notificationAccessToken = searchParams.get('access_token'); const [showMore, setShowMore] = useState(false); const [onlyMentions, setOnlyMentions] = useState(false); const scrollableRef = useRef(); const { nearReachEnd, scrollDirection, reachStart, nearReachStart } = useScroll({ scrollableRef, }); const hiddenUI = scrollDirection === 'end' && !nearReachStart; const [followRequests, setFollowRequests] = useState([]); const [announcements, setAnnouncements] = useState([]); console.debug('RENDER Notifications'); const notificationsIterator = useRef(); async function fetchNotifications(firstLoad) { if (firstLoad || !notificationsIterator.current) { // Reset iterator notificationsIterator.current = masto.v1.notifications.list({ limit: LIMIT, excludeTypes: ['follow_request'], }); } const allNotifications = await notificationsIterator.current.next(); const notifications = allNotifications.value; if (notifications?.length) { notifications.forEach((notification) => { saveStatus(notification.status, instance, { skipThreading: true, }); }); const groupedNotifications = groupNotifications(notifications); if (firstLoad) { states.notificationsLast = notifications[0]; states.notifications = groupedNotifications; // Update last read marker masto.v1.markers .create({ notifications: { lastReadId: notifications[0].id, }, }) .catch(() => {}); } else { states.notifications.push(...groupedNotifications); } } states.notificationsShowNew = false; states.notificationsLastFetchTime = Date.now(); return allNotifications; } async function fetchFollowRequests() { // Note: no pagination here yet because this better be on a separate page. Should be rare use-case??? try { return await masto.v1.followRequests.list({ limit: 80, }); } catch (e) { // Silently fail return []; } } const loadFollowRequests = () => { setUIState('loading'); (async () => { try { const requests = await fetchFollowRequests(); setFollowRequests(requests); setUIState('default'); } catch (e) { setUIState('error'); } })(); }; async function fetchAnnouncements() { try { return await masto.v1.announcements.list(); } catch (e) { // Silently fail return []; } } const loadNotifications = (firstLoad) => { setShowNew(false); setUIState('loading'); (async () => { try { const fetchNotificationsPromise = fetchNotifications(firstLoad); if (firstLoad) { fetchAnnouncements() .then((announcements) => { announcements.sort((a, b) => { // Sort by updatedAt first, then createdAt const aDate = new Date(a.updatedAt || a.createdAt); const bDate = new Date(b.updatedAt || b.createdAt); return bDate - aDate; }); setAnnouncements(announcements); }) .catch(() => {}); fetchFollowRequests() .then((requests) => { setFollowRequests(requests); }) .catch(() => {}); } const { done } = await fetchNotificationsPromise; setShowMore(!done); setUIState('default'); } catch (e) { setUIState('error'); } })(); }; useEffect(() => { loadNotifications(true); }, []); useEffect(() => { if (reachStart) { loadNotifications(true); } }, [reachStart]); // useEffect(() => { // if (nearReachEnd && showMore) { // loadNotifications(); // } // }, [nearReachEnd, showMore]); const [showNew, setShowNew] = useState(false); const loadUpdates = useCallback( ({ disableIdleCheck = false } = {}) => { if (uiState === 'loading') { return; } console.log('✨ Load updates', { autoRefresh: snapStates.settings.autoRefresh, scrollTop: scrollableRef.current?.scrollTop, inBackground: inBackground(), disableIdleCheck, }); if ( snapStates.settings.autoRefresh && scrollableRef.current?.scrollTop < 16 && (disableIdleCheck || window.__IDLE__) && !inBackground() ) { loadNotifications(true); } }, [snapStates.notificationsShowNew, snapStates.settings.autoRefresh, uiState], ); // useEffect(loadUpdates, [snapStates.notificationsShowNew]); const lastHiddenTime = useRef(); usePageVisibility((visible) => { let unsub; if (visible) { const timeDiff = Date.now() - lastHiddenTime.current; if (!lastHiddenTime.current || timeDiff > 1000 * 3) { // 3 seconds loadUpdates({ disableIdleCheck: true, }); } else { lastHiddenTime.current = Date.now(); } unsub = subscribeKey(states, 'notificationsShowNew', (v) => { if (v) { loadUpdates(); } setShowNew(v); }); } return () => { unsub?.(); }; }); const todayDate = new Date(); const yesterdayDate = new Date(todayDate - 24 * 60 * 60 * 1000); let currentDay = new Date(); const showTodayEmpty = !snapStates.notifications.some( (notification) => new Date(notification.createdAt).toDateString() === todayDate.toDateString(), ); const announcementsListRef = useRef(); useEffect(() => { if (notificationID) { states.routeNotification = { id: notificationID, accessToken: atob(notificationAccessToken), }; } }, [notificationID, notificationAccessToken]); // useEffect(() => { // if (uiState === 'default') { // (async () => { // try { // const registration = await getRegistration(); // if (registration?.getNotifications) { // const notifications = await registration.getNotifications(); // console.log('🔔 Push notifications', notifications); // // Close all notifications? // // notifications.forEach((notification) => { // // notification.close(); // // }); // } // } catch (e) {} // })(); // } // }, [uiState]); return (
{uiState === 'default' ? "You're all caught up." : <>…>}
)} {snapStates.notifications.length ? ( <> {snapStates.notifications // This is leaked from Notifications popover .filter((n) => n.type !== 'follow_request') .map((notification) => { if (onlyMentions && notification.type !== 'mention') { return null; } const notificationDay = new Date(notification.createdAt); const differentDay = notificationDay.toDateString() !== currentDay.toDateString(); if (differentDay) { currentDay = notificationDay; } // if notificationDay is yesterday, show "Yesterday" // if notificationDay is before yesterday, show date const heading = notificationDay.toDateString() === yesterdayDate.toDateString() ? 'Yesterday' : niceDateTime(currentDay, { hideTime: true, }); return (███████████ ████
Unable to load notifications
{updatedAt && updatedAtText !== publishedDateText && ( <> {' '} •{' '} Updated{' '} > )}