2022-12-10 12:14:48 +03:00
|
|
|
import './notifications.css';
|
|
|
|
|
|
|
|
import { Link } from 'preact-router/match';
|
|
|
|
import { useEffect, useRef, useState } from 'preact/hooks';
|
|
|
|
import { useSnapshot } from 'valtio';
|
|
|
|
|
2022-12-14 17:46:50 +03:00
|
|
|
import Avatar from '../components/avatar';
|
2022-12-10 12:14:48 +03:00
|
|
|
import Icon from '../components/icon';
|
|
|
|
import Loader from '../components/loader';
|
|
|
|
import NameText from '../components/name-text';
|
|
|
|
import Status from '../components/status';
|
|
|
|
import states from '../utils/states';
|
2022-12-11 07:15:39 +03:00
|
|
|
import store from '../utils/store';
|
2022-12-10 12:14:48 +03:00
|
|
|
import useTitle from '../utils/useTitle';
|
|
|
|
|
|
|
|
/*
|
|
|
|
Notification types
|
|
|
|
==================
|
|
|
|
mention = Someone mentioned you in their status
|
|
|
|
status = Someone you enabled notifications for has posted a status
|
|
|
|
reblog = Someone boosted one of your statuses
|
|
|
|
follow = Someone followed you
|
|
|
|
follow_request = Someone requested to follow you
|
|
|
|
favourite = Someone favourited one of your statuses
|
|
|
|
poll = A poll you have voted in or created has ended
|
|
|
|
update = A status you interacted with has been edited
|
|
|
|
admin.sign_up = Someone signed up (optionally sent to admins)
|
|
|
|
admin.report = A new report has been filed
|
|
|
|
*/
|
|
|
|
|
|
|
|
const contentText = {
|
|
|
|
mention: 'mentioned you in their status.',
|
|
|
|
status: 'posted a status.',
|
|
|
|
reblog: 'boosted your status.',
|
|
|
|
follow: 'followed you.',
|
|
|
|
follow_request: 'requested to follow you.',
|
|
|
|
favourite: 'favourited your status.',
|
|
|
|
poll: 'A poll you have voted in or created has ended.',
|
2022-12-11 07:15:39 +03:00
|
|
|
'poll-self': 'A poll you have created has ended.',
|
|
|
|
'poll-voted': 'A poll you have voted in has ended.',
|
2022-12-10 12:14:48 +03:00
|
|
|
update: 'A status you interacted with has been edited.',
|
|
|
|
};
|
|
|
|
|
2022-12-19 19:11:55 +03:00
|
|
|
const LIMIT = 30; // 30 is the maximum limit :(
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
function Notification({ notification }) {
|
2022-12-14 17:46:50 +03:00
|
|
|
const { id, type, status, account, _accounts } = notification;
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
// status = Attached when type of the notification is favourite, reblog, status, mention, poll, or update
|
|
|
|
const actualStatusID = status?.reblog?.id || status?.id;
|
|
|
|
|
2022-12-11 07:15:39 +03:00
|
|
|
const currentAccount = store.session.get('currentAccount');
|
2022-12-14 20:45:36 +03:00
|
|
|
const isSelf = currentAccount === account?.id;
|
2022-12-11 07:15:39 +03:00
|
|
|
const isVoted = status?.poll?.voted;
|
|
|
|
|
|
|
|
const text =
|
|
|
|
type === 'poll'
|
|
|
|
? contentText[isSelf ? 'poll-self' : isVoted ? 'poll-voted' : 'poll']
|
|
|
|
: contentText[type];
|
|
|
|
|
2022-12-10 12:14:48 +03:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div
|
2022-12-21 02:42:48 +03:00
|
|
|
class={`notification-type notification-${type}`}
|
2022-12-10 12:14:48 +03:00
|
|
|
title={new Date(notification.createdAt).toLocaleString()}
|
|
|
|
>
|
|
|
|
<Icon
|
|
|
|
icon={
|
|
|
|
{
|
|
|
|
mention: 'comment',
|
|
|
|
status: 'notification',
|
2022-12-11 12:10:10 +03:00
|
|
|
reblog: 'rocket',
|
2022-12-10 12:14:48 +03:00
|
|
|
follow: 'follow',
|
|
|
|
follow_request: 'follow-add',
|
|
|
|
favourite: 'heart',
|
|
|
|
poll: 'poll',
|
|
|
|
update: 'pencil',
|
|
|
|
}[type] || 'notification'
|
|
|
|
}
|
|
|
|
size="xl"
|
|
|
|
alt={type}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="notification-content">
|
2022-12-21 03:48:11 +03:00
|
|
|
{type !== 'mention' && (
|
|
|
|
<p>
|
|
|
|
{!/poll|update/i.test(type) && (
|
|
|
|
<>
|
|
|
|
{_accounts?.length > 1 ? (
|
|
|
|
<>
|
|
|
|
<b>{_accounts.length} people</b>{' '}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<NameText account={account} showAvatar />{' '}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{text}
|
|
|
|
{type === 'mention' && (
|
|
|
|
<span class="insignificant">
|
|
|
|
{' '}
|
|
|
|
•{' '}
|
|
|
|
<relative-time
|
|
|
|
datetime={notification.createdAt}
|
|
|
|
format="micro"
|
|
|
|
threshold="P1D"
|
|
|
|
prefix=""
|
|
|
|
/>
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
)}
|
2022-12-14 17:46:50 +03:00
|
|
|
{_accounts?.length > 1 && (
|
|
|
|
<p>
|
|
|
|
{_accounts.map((account, i) => (
|
|
|
|
<>
|
|
|
|
<a
|
|
|
|
href={account.url}
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
states.showAccount = account;
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Avatar
|
|
|
|
url={account.avatarStatic}
|
|
|
|
size={
|
2022-12-14 17:53:14 +03:00
|
|
|
_accounts.length < 10
|
2022-12-14 17:46:50 +03:00
|
|
|
? 'xl'
|
2022-12-14 17:53:14 +03:00
|
|
|
: _accounts.length < 100
|
2022-12-14 17:46:50 +03:00
|
|
|
? 'l'
|
2022-12-14 17:53:14 +03:00
|
|
|
: _accounts.length < 1000
|
2022-12-14 17:46:50 +03:00
|
|
|
? 'm'
|
|
|
|
: 's' // My god, this person is popular!
|
|
|
|
}
|
|
|
|
key={account.id}
|
|
|
|
alt={`${account.displayName} @${account.acct}`}
|
|
|
|
/>
|
|
|
|
</a>{' '}
|
|
|
|
</>
|
|
|
|
))}
|
|
|
|
</p>
|
|
|
|
)}
|
2022-12-10 12:14:48 +03:00
|
|
|
{status && (
|
2022-12-21 03:48:11 +03:00
|
|
|
<Link
|
|
|
|
class={`status-link status-type-${type}`}
|
|
|
|
href={`#/s/${actualStatusID}`}
|
|
|
|
>
|
2022-12-10 12:14:48 +03:00
|
|
|
<Status status={status} size="s" />
|
|
|
|
</Link>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function NotificationsList({ notifications, emptyCopy }) {
|
|
|
|
if (!notifications.length && emptyCopy) {
|
|
|
|
return <p class="timeline-empty">{emptyCopy}</p>;
|
|
|
|
}
|
2022-12-14 17:46:50 +03:00
|
|
|
|
|
|
|
// Create new flat list of notifications
|
|
|
|
// Combine sibling notifications based on type and status id, ignore the id
|
|
|
|
// Concat all notification.account into an array of _accounts
|
2022-12-15 08:11:28 +03:00
|
|
|
const cleanNotifications = [
|
|
|
|
{
|
|
|
|
...notifications[0],
|
|
|
|
_accounts: [notifications[0].account],
|
|
|
|
},
|
|
|
|
];
|
2022-12-14 17:46:50 +03:00
|
|
|
for (let i = 1, j = 0; i < notifications.length; i++) {
|
|
|
|
const notification = notifications[i];
|
|
|
|
const cleanNotification = cleanNotifications[j];
|
|
|
|
const { status, account, type } = notification;
|
|
|
|
if (
|
|
|
|
account &&
|
|
|
|
cleanNotification?.account &&
|
|
|
|
cleanNotification?.status?.id === status?.id &&
|
|
|
|
cleanNotification?.type === type
|
|
|
|
) {
|
|
|
|
cleanNotification._accounts.push(account);
|
|
|
|
} else {
|
|
|
|
cleanNotifications[++j] = {
|
|
|
|
...notification,
|
|
|
|
_accounts: [account],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// console.log({ notifications, cleanNotifications });
|
|
|
|
|
2022-12-10 12:14:48 +03:00
|
|
|
return (
|
|
|
|
<ul class="timeline flat">
|
2022-12-14 17:46:50 +03:00
|
|
|
{cleanNotifications.map((notification, i) => {
|
2022-12-10 12:14:48 +03:00
|
|
|
const { id, type } = notification;
|
|
|
|
return (
|
|
|
|
<li key={id} class={`notification ${type}`}>
|
|
|
|
<Notification notification={notification} />
|
|
|
|
</li>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-16 08:27:04 +03:00
|
|
|
function Notifications() {
|
2022-12-10 12:14:48 +03:00
|
|
|
useTitle('Notifications');
|
|
|
|
const snapStates = useSnapshot(states);
|
|
|
|
const [uiState, setUIState] = useState('default');
|
|
|
|
const [showMore, setShowMore] = useState(false);
|
2022-12-17 20:04:26 +03:00
|
|
|
const [onlyMentions, setOnlyMentions] = useState(false);
|
2022-12-10 12:14:48 +03:00
|
|
|
|
|
|
|
const notificationsIterator = useRef(
|
2022-12-18 06:52:53 +03:00
|
|
|
masto.notifications.iterate({
|
2022-12-10 12:14:48 +03:00
|
|
|
limit: LIMIT,
|
|
|
|
}),
|
|
|
|
).current;
|
|
|
|
async function fetchNotifications(firstLoad) {
|
|
|
|
const allNotifications = await notificationsIterator.next(
|
|
|
|
firstLoad
|
|
|
|
? {
|
|
|
|
limit: LIMIT,
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
);
|
|
|
|
if (allNotifications.value <= 0) {
|
|
|
|
return { done: true };
|
|
|
|
}
|
|
|
|
const notificationsValues = allNotifications.value.map((notification) => {
|
|
|
|
if (notification.status) {
|
|
|
|
states.statuses.set(notification.status.id, notification.status);
|
|
|
|
}
|
|
|
|
return notification;
|
|
|
|
});
|
|
|
|
if (firstLoad) {
|
|
|
|
states.notifications = notificationsValues;
|
|
|
|
} else {
|
|
|
|
states.notifications.push(...notificationsValues);
|
|
|
|
}
|
|
|
|
states.notificationsLastFetchTime = Date.now();
|
|
|
|
return allNotifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadNotifications = (firstLoad) => {
|
|
|
|
setUIState('loading');
|
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
const { done } = await fetchNotifications(firstLoad);
|
|
|
|
setShowMore(!done);
|
|
|
|
setUIState('default');
|
|
|
|
} catch (e) {
|
|
|
|
setUIState('error');
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
loadNotifications(true);
|
2022-12-11 07:07:36 +03:00
|
|
|
states.notificationsNew = [];
|
2022-12-10 12:14:48 +03:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const scrollableRef = useRef();
|
|
|
|
|
|
|
|
// Group notifications by today, yesterday, and older
|
|
|
|
const groupedNotifications = snapStates.notifications.reduce(
|
|
|
|
(acc, notification) => {
|
2022-12-10 16:23:19 +03:00
|
|
|
const date = new Date(notification.createdAt);
|
2022-12-10 12:14:48 +03:00
|
|
|
const today = new Date();
|
|
|
|
const yesterday = new Date();
|
|
|
|
yesterday.setDate(today.getDate() - 1);
|
|
|
|
if (
|
|
|
|
date.getDate() === today.getDate() &&
|
|
|
|
date.getMonth() === today.getMonth() &&
|
|
|
|
date.getFullYear() === today.getFullYear()
|
|
|
|
) {
|
|
|
|
acc.today.push(notification);
|
|
|
|
} else if (
|
|
|
|
date.getDate() === yesterday.getDate() &&
|
|
|
|
date.getMonth() === yesterday.getMonth() &&
|
|
|
|
date.getFullYear() === yesterday.getFullYear()
|
|
|
|
) {
|
|
|
|
acc.yesterday.push(notification);
|
|
|
|
} else {
|
|
|
|
acc.older.push(notification);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
{ today: [], yesterday: [], older: [] },
|
|
|
|
);
|
2022-12-14 17:46:50 +03:00
|
|
|
// console.log(groupedNotifications);
|
2022-12-10 12:14:48 +03:00
|
|
|
return (
|
|
|
|
<div class="deck-container" ref={scrollableRef}>
|
2022-12-17 20:04:26 +03:00
|
|
|
<div class={`timeline-deck deck ${onlyMentions ? 'only-mentions' : ''}`}>
|
2022-12-10 12:14:48 +03:00
|
|
|
<header>
|
|
|
|
<div class="header-side">
|
|
|
|
<a href="#" class="button plain">
|
|
|
|
<Icon icon="home" size="l" />
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
<h1>Notifications</h1>
|
|
|
|
<div class="header-side">
|
|
|
|
<Loader hidden={uiState !== 'loading'} />
|
|
|
|
</div>
|
|
|
|
{snapStates.notificationsNew.length > 0 && (
|
|
|
|
<button
|
|
|
|
class="updates-button"
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
const uniqueNotificationsNew =
|
|
|
|
snapStates.notificationsNew.filter(
|
|
|
|
(notification) =>
|
|
|
|
!snapStates.notifications.some(
|
|
|
|
(n) => n.id === notification.id,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
states.notifications.unshift(...uniqueNotificationsNew);
|
|
|
|
loadNotifications(true);
|
|
|
|
states.notificationsNew = [];
|
|
|
|
|
|
|
|
scrollableRef.current?.scrollTo({
|
|
|
|
top: 0,
|
|
|
|
behavior: 'smooth',
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon icon="arrow-up" /> New notifications
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</header>
|
2022-12-17 20:04:26 +03:00
|
|
|
<div id="mentions-option">
|
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={onlyMentions}
|
|
|
|
onChange={(e) => {
|
|
|
|
setOnlyMentions(e.target.checked);
|
|
|
|
}}
|
|
|
|
/>{' '}
|
|
|
|
Only mentions
|
|
|
|
</label>
|
|
|
|
</div>
|
2022-12-10 12:14:48 +03:00
|
|
|
{snapStates.notifications.length ? (
|
|
|
|
<>
|
|
|
|
<h2 class="timeline-header">Today</h2>
|
|
|
|
<NotificationsList
|
|
|
|
notifications={groupedNotifications.today}
|
|
|
|
emptyCopy="You're all caught up."
|
|
|
|
/>
|
|
|
|
{groupedNotifications.yesterday.length > 0 && (
|
|
|
|
<>
|
|
|
|
<h2 class="timeline-header">Yesterday</h2>
|
|
|
|
<NotificationsList
|
|
|
|
notifications={groupedNotifications.yesterday}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{groupedNotifications.older.length > 0 && (
|
|
|
|
<>
|
|
|
|
<h2 class="timeline-header">Older</h2>
|
|
|
|
<NotificationsList notifications={groupedNotifications.older} />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{showMore && (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="plain block"
|
|
|
|
disabled={uiState === 'loading'}
|
|
|
|
onClick={() => loadNotifications()}
|
2022-12-15 12:00:11 +03:00
|
|
|
style={{ marginBlockEnd: '6em' }}
|
2022-12-10 12:14:48 +03:00
|
|
|
>
|
|
|
|
{uiState === 'loading' ? <Loader /> : <>Show more…</>}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{uiState === 'loading' && (
|
|
|
|
<>
|
|
|
|
<h2 class="timeline-header">Today</h2>
|
|
|
|
<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">Error loading notifications</p>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2022-12-16 08:27:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Notifications;
|