phanpy/src/pages/notifications.jsx

482 lines
15 KiB
React
Raw Normal View History

2022-12-10 12:14:48 +03:00
import './notifications.css';
import { memo } from 'preact/compat';
2023-05-05 12:53:16 +03:00
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
2022-12-10 12:14:48 +03:00
import { useSnapshot } from 'valtio';
import AccountBlock from '../components/account-block';
import FollowRequestButtons from '../components/follow-request-buttons';
2022-12-10 12:14:48 +03:00
import Icon from '../components/icon';
import Link from '../components/link';
2022-12-10 12:14:48 +03:00
import Loader from '../components/loader';
2023-04-26 08:09:44 +03:00
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';
2023-03-01 15:07:22 +03:00
import niceDateTime from '../utils/nice-date-time';
import shortenNumber from '../utils/shorten-number';
import states, { saveStatus } from '../utils/states';
import { getCurrentInstance } from '../utils/store-utils';
import useScroll from '../utils/useScroll';
2022-12-10 12:14:48 +03:00
import useTitle from '../utils/useTitle';
2022-12-19 19:11:55 +03:00
const LIMIT = 30; // 30 is the maximum limit :(
2022-12-10 12:14:48 +03:00
2022-12-16 08:27:04 +03:00
function Notifications() {
useTitle('Notifications', '/notifications');
2023-02-18 19:05:46 +03:00
const { masto, instance } = api();
2022-12-10 12:14:48 +03:00
const snapStates = useSnapshot(states);
const [uiState, setUIState] = useState('default');
const [showMore, setShowMore] = useState(false);
const [onlyMentions, setOnlyMentions] = useState(false);
const scrollableRef = useRef();
const { nearReachEnd, scrollDirection, reachStart, nearReachStart } =
useScroll({
2023-02-28 16:56:41 +03:00
scrollableRef,
});
const hiddenUI = scrollDirection === 'end' && !nearReachStart;
const [followRequests, setFollowRequests] = useState([]);
const [announcements, setAnnouncements] = useState([]);
2022-12-10 12:14:48 +03:00
console.debug('RENDER Notifications');
const notificationsIterator = useRef();
2022-12-10 12:14:48 +03:00
async function fetchNotifications(firstLoad) {
2023-02-12 12:38:50 +03:00
if (firstLoad || !notificationsIterator.current) {
// Reset iterator
notificationsIterator.current = masto.v1.notifications.list({
limit: LIMIT,
});
}
const allNotifications = await notificationsIterator.current.next();
2023-02-12 12:38:50 +03:00
const notifications = allNotifications.value;
if (notifications?.length) {
notifications.forEach((notification) => {
saveStatus(notification.status, instance, {
skipThreading: true,
});
});
2023-02-12 12:38:50 +03:00
const groupedNotifications = groupNotifications(notifications);
if (firstLoad) {
2023-02-12 12:38:50 +03:00
states.notificationsLast = notifications[0];
states.notifications = groupedNotifications;
} else {
states.notifications.push(...groupedNotifications);
2022-12-10 12:14:48 +03:00
}
}
2023-02-12 12:38:50 +03:00
states.notificationsShowNew = false;
2022-12-10 12:14:48 +03:00
states.notificationsLastFetchTime = Date.now();
return allNotifications;
}
function fetchFollowRequests() {
// Note: no pagination here yet because this better be on a separate page. Should be rare use-case???
return masto.v1.followRequests.list({
limit: 80,
});
}
const loadFollowRequests = () => {
setUIState('loading');
(async () => {
try {
const requests = await fetchFollowRequests();
setFollowRequests(requests);
setUIState('default');
} catch (e) {
setUIState('error');
}
})();
};
function fetchAnnouncements() {
return masto.v1.announcements.list();
}
2022-12-10 12:14:48 +03:00
const loadNotifications = (firstLoad) => {
setUIState('loading');
(async () => {
try {
const fetchFollowRequestsPromise = fetchFollowRequests();
const fetchAnnouncementsPromise = fetchAnnouncements();
2022-12-10 12:14:48 +03:00
const { done } = await fetchNotifications(firstLoad);
setShowMore(!done);
if (firstLoad) {
const requests = await fetchFollowRequestsPromise;
setFollowRequests(requests);
const announcements = await fetchAnnouncementsPromise;
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);
}
2022-12-10 12:14:48 +03:00
setUIState('default');
} catch (e) {
setUIState('error');
}
})();
};
useEffect(() => {
loadNotifications(true);
}, []);
useEffect(() => {
if (reachStart) {
loadNotifications(true);
}
}, [reachStart]);
2022-12-10 12:14:48 +03:00
useEffect(() => {
if (nearReachEnd && showMore) {
loadNotifications();
}
}, [nearReachEnd, showMore]);
2022-12-10 12:14:48 +03:00
2023-05-05 12:53:16 +03:00
const isHovering = useRef(false);
const loadUpdates = useCallback(() => {
console.log('✨ Load updates', {
autoRefresh: snapStates.settings.autoRefresh,
scrollTop: scrollableRef.current?.scrollTop === 0,
isHovering: isHovering.current,
inBackground: inBackground(),
notificationsShowNew: snapStates.notificationsShowNew,
uiState,
});
if (
snapStates.settings.autoRefresh &&
scrollableRef.current?.scrollTop === 0 &&
!isHovering.current &&
!inBackground() &&
snapStates.notificationsShowNew &&
uiState !== 'loading'
) {
loadNotifications(true);
}
}, [
snapStates.notificationsShowNew,
snapStates.settings.autoRefresh,
uiState,
]);
useEffect(loadUpdates, [snapStates.notificationsShowNew]);
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(),
2022-12-10 12:14:48 +03:00
);
const announcementsListRef = useRef();
2022-12-10 12:14:48 +03:00
return (
2022-12-30 15:37:57 +03:00
<div
id="notifications-page"
class="deck-container"
ref={scrollableRef}
tabIndex="-1"
2023-05-05 12:53:16 +03:00
onPointerEnter={() => {
console.log('👆 Pointer enter');
isHovering.current = true;
}}
onPointerLeave={() => {
console.log('👇 Pointer leave');
isHovering.current = false;
}}
2022-12-30 15:37:57 +03:00
>
<div class={`timeline-deck deck ${onlyMentions ? 'only-mentions' : ''}`}>
2022-12-24 12:54:42 +03:00
<header
hidden={hiddenUI}
2023-02-15 06:20:48 +03:00
onClick={(e) => {
if (!e.target.closest('a, button')) {
scrollableRef.current?.scrollTo({ top: 0, behavior: 'smooth' });
}
2022-12-24 12:54:42 +03:00
}}
2023-04-14 10:46:11 +03:00
class={uiState === 'loading' ? 'loading' : ''}
2022-12-24 12:54:42 +03:00
>
2023-02-08 14:11:33 +03:00
<div class="header-grid">
<div class="header-side">
2023-04-26 08:09:44 +03:00
<NavMenu />
2023-02-08 14:11:33 +03:00
<Link to="/" class="button plain">
2023-04-06 14:32:26 +03:00
<Icon icon="home" size="l" alt="Home" />
2023-02-08 14:11:33 +03:00
</Link>
</div>
<h1>Notifications</h1>
<div class="header-side">
2023-04-14 10:46:11 +03:00
{/* <Loader hidden={uiState !== 'loading'} /> */}
2023-02-08 14:11:33 +03:00
</div>
2022-12-10 12:14:48 +03:00
</div>
{snapStates.notificationsShowNew && uiState !== 'loading' && (
<button
2023-02-23 10:56:58 +03:00
class="updates-button shiny-pill"
type="button"
onClick={() => {
loadNotifications(true);
scrollableRef.current?.scrollTo({
top: 0,
behavior: 'smooth',
});
}}
>
<Icon icon="arrow-up" /> New notifications
</button>
)}
2022-12-10 12:14:48 +03:00
</header>
{announcements.length > 0 && (
<div class="shazam-container">
<div class="shazam-container-inner">
<details class="announcements">
<summary>
<span>
<Icon icon="announce" class="announcement-icon" size="l" />{' '}
<b>Announcement{announcements.length > 1 ? 's' : ''}</b>{' '}
<small class="insignificant">{instance}</small>
</span>
{announcements.length > 1 && (
<span class="announcements-nav-buttons">
{announcements.map((announcement, index) => (
<button
type="button"
class="plain2 small"
onClick={() => {
announcementsListRef.current?.children[
index
].scrollIntoView({
behavior: 'smooth',
block: 'nearest',
});
}}
>
{index + 1}
</button>
))}
</span>
)}
</summary>
<ul
class={`announcements-list-${
announcements.length > 1 ? 'multiple' : 'single'
}`}
ref={announcementsListRef}
>
{announcements.map((announcement) => (
<li>
<AnnouncementBlock announcement={announcement} />
</li>
))}
</ul>
</details>
</div>
</div>
)}
{followRequests.length > 0 && (
<div class="follow-requests">
<h2 class="timeline-header">Follow requests</h2>
<ul>
{followRequests.map((account) => (
<li>
<AccountBlock account={account} />
<FollowRequestButtons
accountID={account.id}
onChange={() => {
loadFollowRequests();
2023-05-07 14:17:16 +03:00
loadNotifications(true);
}}
/>
</li>
))}
</ul>
</div>
)}
<div id="mentions-option">
<label>
<input
type="checkbox"
checked={onlyMentions}
onChange={(e) => {
setOnlyMentions(e.target.checked);
}}
/>{' '}
Only mentions
</label>
</div>
<h2 class="timeline-header">Today</h2>
{showTodayEmpty && !!snapStates.notifications.length && (
<p class="ui-state insignificant">
{uiState === 'default' ? "You're all caught up." : <>&hellip;</>}
</p>
)}
2022-12-10 12:14:48 +03:00
{snapStates.notifications.length ? (
<>
{snapStates.notifications.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'
2023-03-01 17:18:45 +03:00
: niceDateTime(currentDay, {
2023-03-01 15:07:22 +03:00
hideTime: true,
});
return (
<>
{differentDay && <h2 class="timeline-header">{heading}</h2>}
<Notification
2023-02-18 19:05:46 +03:00
instance={instance}
notification={notification}
key={notification.id}
2023-05-07 14:17:16 +03:00
reload={() => {
loadNotifications(true);
loadFollowRequests();
}}
/>
</>
);
})}
2022-12-10 12:14:48 +03:00
</>
) : (
<>
{uiState === 'loading' && (
<>
<ul class="timeline flat">
{Array.from({ length: 5 }).map((_, i) => (
<li class="notification skeleton">
<div class="notification-type">
<Icon icon="notification" size="xl" />
</div>
<div class="notification-content">
<p> </p>
</div>
</li>
))}
</ul>
</>
)}
{uiState === 'error' && (
<p class="ui-state">
Unable to load notifications
<br />
<br />
<button type="button" onClick={() => loadNotifications(true)}>
Try again
</button>
</p>
2022-12-10 12:14:48 +03:00
)}
</>
)}
{showMore && (
<button
type="button"
class="plain block"
disabled={uiState === 'loading'}
onClick={() => loadNotifications()}
style={{ marginBlockEnd: '6em' }}
>
{uiState === 'loading' ? <Loader abrupt /> : <>Show more&hellip;</>}
</button>
)}
</div>
</div>
);
}
2023-05-05 12:53:16 +03:00
function inBackground() {
return !!document.querySelector('.deck-backdrop, #modal-container > *');
}
function AnnouncementBlock({ announcement }) {
const { instance } = api();
const { contact } = getCurrentInstance();
const contactAccount = contact?.account;
const {
id,
content,
startsAt,
endsAt,
published,
allDay,
publishedAt,
updatedAt,
read,
mentions,
statuses,
tags,
emojis,
reactions,
} = announcement;
const publishedAtDate = new Date(publishedAt);
const publishedDateText = niceDateTime(publishedAtDate);
const updatedAtDate = new Date(updatedAt);
const updatedAtText = niceDateTime(updatedAtDate);
return (
<div class="announcement-block">
<AccountBlock account={contactAccount} />
<div
class="announcement-content"
onClick={handleContentLinks({ mentions, instance })}
dangerouslySetInnerHTML={{
__html: enhanceContent(content, {
emojis,
}),
}}
/>
<p class="insignificant">
<time datetime={publishedAtDate.toISOString()}>
{niceDateTime(publishedAtDate)}
</time>
{updatedAt && updatedAtText !== publishedDateText && (
<>
{' '}
&bull;{' '}
<span class="ib">
Updated{' '}
<time datetime={updatedAtDate.toISOString()}>
{niceDateTime(updatedAtDate)}
</time>
</span>
</>
)}
</p>
<div class="announcement-reactions" hidden>
{reactions.map((reaction) => {
const { name, count, me, staticUrl, url } = reaction;
return (
<button type="button" class={`plain4 small ${me ? 'reacted' : ''}`}>
{url || staticUrl ? (
<img src={url || staticUrl} alt={name} width="16" height="16" />
) : (
<span>{name}</span>
)}{' '}
<span class="count">{shortenNumber(count)}</span>
</button>
);
})}
</div>
</div>
);
}
export default memo(Notifications);