Merge branch 'grouped-notifications' into 'develop'

Grouped notifications

See merge request soapbox-pub/soapbox-fe!1449
This commit is contained in:
Justin 2022-05-26 18:19:13 +00:00
commit b5d73cd2d0
4 changed files with 64 additions and 17 deletions

View file

@ -28,6 +28,31 @@ describe('<Notification />', () => {
expect(screen.getByTestId('notification')).toBeInTheDocument(); expect(screen.getByTestId('notification')).toBeInTheDocument();
expect(screen.getByTestId('account')).toContainHTML('neko@rdrama.cc'); expect(screen.getByTestId('account')).toContainHTML('neko@rdrama.cc');
expect(screen.getByTestId('message')).toHaveTextContent('Nekobit followed you');
});
describe('grouped notifications', () => {
it('renders a grouped follow notification for more than 2', async() => {
const { notification, state } = normalize(require('soapbox/__fixtures__/notification-follow.json'));
const groupedNotification = { ...notification.toJS(), total_count: 5 };
render(<Notification notification={groupedNotification} />, undefined, state);
expect(screen.getByTestId('notification')).toBeInTheDocument();
expect(screen.getByTestId('account')).toContainHTML('neko@rdrama.cc');
expect(screen.getByTestId('message')).toHaveTextContent('Nekobit + 4 others followed you');
});
it('renders a grouped follow notification for 1', async() => {
const { notification, state } = normalize(require('soapbox/__fixtures__/notification-follow.json'));
const groupedNotification = { ...notification.toJS(), total_count: 2 };
render(<Notification notification={groupedNotification} />, undefined, state);
expect(screen.getByTestId('notification')).toBeInTheDocument();
expect(screen.getByTestId('account')).toContainHTML('neko@rdrama.cc');
expect(screen.getByTestId('message')).toHaveTextContent('Nekobit + 1 other followed you');
});
}); });
it('renders a favourite notification', async() => { it('renders a favourite notification', async() => {

View file

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { HotKeys } from 'react-hotkeys'; import { HotKeys } from 'react-hotkeys';
import { FormattedMessage, useIntl } from 'react-intl'; import { defineMessages, FormattedMessage, IntlShape, MessageDescriptor } from 'react-intl';
import { useIntl } from 'react-intl';
import { useHistory } from 'react-router-dom'; import { useHistory } from 'react-router-dom';
import { useAppSelector } from 'soapbox/hooks'; import { useAppSelector } from 'soapbox/hooks';
@ -24,11 +25,6 @@ const notificationForScreenReader = (intl: ReturnType<typeof useIntl>, message:
return output.join(', '); return output.join(', ');
}; };
// Workaround for dynamic messages (https://github.com/formatjs/babel-plugin-react-intl/issues/119#issuecomment-326202499)
function FormattedMessageFixed(props: any) {
return <FormattedMessage {...props} />;
}
const buildLink = (account: Account): JSX.Element => ( const buildLink = (account: Account): JSX.Element => (
<bdi> <bdi>
<Permalink <Permalink
@ -55,7 +51,7 @@ const icons: Record<NotificationType, string> = {
user_approved: require('@tabler/icons/icons/user-plus.svg'), user_approved: require('@tabler/icons/icons/user-plus.svg'),
}; };
const messages: Record<NotificationType, { id: string, defaultMessage: string }> = { const messages: Record<string | number | symbol, MessageDescriptor> = defineMessages({
follow: { follow: {
id: 'notification.follow', id: 'notification.follow',
defaultMessage: '{name} followed you', defaultMessage: '{name} followed you',
@ -100,18 +96,36 @@ const messages: Record<NotificationType, { id: string, defaultMessage: string }>
id: 'notification.user_approved', id: 'notification.user_approved',
defaultMessage: 'Welcome to {instance}!', defaultMessage: 'Welcome to {instance}!',
}, },
}; });
const buildMessage = (type: NotificationType, account: Account, targetName: string, instanceTitle: string): JSX.Element => { const buildMessage = (
intl: IntlShape,
type: NotificationType,
account: Account,
totalCount: number | null,
targetName: string,
instanceTitle: string,
): React.ReactNode => {
const link = buildLink(account); const link = buildLink(account);
const name = intl.formatMessage({
id: 'notification.name',
defaultMessage: '{link}{others}',
}, {
link,
others: totalCount && totalCount > 0 ? (
<FormattedMessage
id='notification.others'
defaultMessage=' + {count} {count, plural, one {other} other {others}}'
values={{ count: totalCount - 1 }}
/>
) : '',
});
return ( return intl.formatMessage(messages[type], {
<FormattedMessageFixed name,
id={messages[type].id} targetName,
defaultMessage={messages[type].defaultMessage} instance: instanceTitle,
values={{ name: link, targetName, instance: instanceTitle }} });
/>
);
}; };
interface INotificaton { interface INotificaton {
@ -268,7 +282,7 @@ const Notification: React.FC<INotificaton> = (props) => {
const targetName = notification.target && typeof notification.target === 'object' ? notification.target.acct : ''; const targetName = notification.target && typeof notification.target === 'object' ? notification.target.acct : '';
const message: React.ReactNode = type && account && typeof account === 'object' ? buildMessage(type, account, targetName, instance.title) : null; const message: React.ReactNode = type && account && typeof account === 'object' ? buildMessage(intl, type, account, notification.total_count, targetName, instance.title) : null;
return ( return (
<HotKeys handlers={getHandlers()} data-testid='notification'> <HotKeys handlers={getHandlers()} data-testid='notification'>
@ -300,6 +314,7 @@ const Notification: React.FC<INotificaton> = (props) => {
theme='muted' theme='muted'
size='sm' size='sm'
truncate truncate
data-testid='message'
> >
{message} {message}
</Text> </Text>

View file

@ -34,6 +34,7 @@ export const NotificationRecord = ImmutableRecord({
status: null as EmbeddedEntity<Status>, status: null as EmbeddedEntity<Status>,
target: null as EmbeddedEntity<Account>, // move target: null as EmbeddedEntity<Account>, // move
type: '' as NotificationType | '', type: '' as NotificationType | '',
total_count: null as number | null, // grouped notifications
}); });
export const normalizeNotification = (notification: Record<string, any>) => { export const normalizeNotification = (notification: Record<string, any>) => {

View file

@ -274,6 +274,7 @@ describe('notifications reducer', () => {
status: '9vvNxoo5EFbbnfdXQu', status: '9vvNxoo5EFbbnfdXQu',
emoji: '😢', emoji: '😢',
chat_message: null, chat_message: null,
total_count: null,
})], })],
['10743', ImmutableMap({ ['10743', ImmutableMap({
id: '10743', id: '10743',
@ -284,6 +285,7 @@ describe('notifications reducer', () => {
status: '9vvNxoo5EFbbnfdXQu', status: '9vvNxoo5EFbbnfdXQu',
emoji: null, emoji: null,
chat_message: null, chat_message: null,
total_count: null,
})], })],
['10741', ImmutableMap({ ['10741', ImmutableMap({
id: '10741', id: '10741',
@ -294,6 +296,7 @@ describe('notifications reducer', () => {
status: '9vvNxoo5EFbbnfdXQu', status: '9vvNxoo5EFbbnfdXQu',
emoji: null, emoji: null,
chat_message: null, chat_message: null,
total_count: null,
})], })],
['10734', ImmutableMap({ ['10734', ImmutableMap({
id: '10734', id: '10734',
@ -339,6 +342,7 @@ describe('notifications reducer', () => {
status: '9vvNxoo5EFbbnfdXQu', status: '9vvNxoo5EFbbnfdXQu',
emoji: '😢', emoji: '😢',
chat_message: null, chat_message: null,
total_count: null,
})], })],
['10743', ImmutableMap({ ['10743', ImmutableMap({
id: '10743', id: '10743',
@ -349,6 +353,7 @@ describe('notifications reducer', () => {
status: '9vvNxoo5EFbbnfdXQu', status: '9vvNxoo5EFbbnfdXQu',
emoji: null, emoji: null,
chat_message: null, chat_message: null,
total_count: null,
})], })],
['10741', ImmutableMap({ ['10741', ImmutableMap({
id: '10741', id: '10741',
@ -359,6 +364,7 @@ describe('notifications reducer', () => {
status: '9vvNxoo5EFbbnfdXQu', status: '9vvNxoo5EFbbnfdXQu',
emoji: null, emoji: null,
chat_message: null, chat_message: null,
total_count: null,
})], })],
]), ]),
unread: 1, unread: 1,