2022-01-27 07:00:05 -08:00
|
|
|
import React from 'react';
|
2022-06-23 16:03:35 -07:00
|
|
|
import { useIntl, MessageDescriptor } from 'react-intl';
|
2022-06-23 15:29:10 -07:00
|
|
|
import { useHistory } from 'react-router-dom';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2022-06-23 15:29:10 -07:00
|
|
|
import { dismissAlert } from 'soapbox/actions/alerts';
|
2022-06-23 13:20:41 -07:00
|
|
|
import { Button } from 'soapbox/components/ui';
|
2022-06-23 14:05:11 -07:00
|
|
|
import { useAppSelector, useAppDispatch } from 'soapbox/hooks';
|
2022-07-25 20:58:09 -07:00
|
|
|
import { NotificationStack, NotificationObject, StyleFactoryFn } from 'soapbox/react-notification';
|
2022-06-23 13:20:41 -07:00
|
|
|
|
2022-06-23 15:29:10 -07:00
|
|
|
import type { Alert } from 'soapbox/reducers/alerts';
|
2020-09-29 16:55:05 -07:00
|
|
|
|
2022-06-23 15:29:10 -07:00
|
|
|
/** Portal for snackbar alerts. */
|
2022-06-23 14:05:11 -07:00
|
|
|
const SnackbarContainer: React.FC = () => {
|
|
|
|
const intl = useIntl();
|
2022-06-23 15:29:10 -07:00
|
|
|
const history = useHistory();
|
2022-06-23 14:05:11 -07:00
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
2022-06-23 15:29:10 -07:00
|
|
|
const alerts = useAppSelector(state => state.alerts);
|
|
|
|
|
|
|
|
/** Apply i18n to the message if it's an object. */
|
2022-06-23 16:03:35 -07:00
|
|
|
const maybeFormatMessage = (message: MessageDescriptor | string): string => {
|
2022-06-23 15:29:10 -07:00
|
|
|
switch (typeof message) {
|
|
|
|
case 'string': return message;
|
|
|
|
case 'object': return intl.formatMessage(message);
|
|
|
|
default: return '';
|
|
|
|
}
|
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-23 15:29:10 -07:00
|
|
|
/** Convert a reducer Alert into a react-notification object. */
|
|
|
|
const buildAlert = (item: Alert): NotificationObject => {
|
|
|
|
// Backwards-compatibility
|
|
|
|
if (item.actionLink) {
|
|
|
|
item = item.set('action', () => history.push(item.actionLink));
|
|
|
|
}
|
2022-01-27 07:00:05 -08:00
|
|
|
|
2022-06-23 15:29:10 -07:00
|
|
|
const alert: NotificationObject = {
|
|
|
|
message: maybeFormatMessage(item.message),
|
|
|
|
title: maybeFormatMessage(item.title),
|
|
|
|
key: item.key,
|
|
|
|
className: `notification-bar-${item.severity}`,
|
|
|
|
activeClassName: 'snackbar--active',
|
2022-06-23 16:03:35 -07:00
|
|
|
dismissAfter: item.dismissAfter,
|
2022-06-23 15:29:10 -07:00
|
|
|
style: false,
|
|
|
|
};
|
2022-01-27 07:00:05 -08:00
|
|
|
|
2022-06-23 15:29:10 -07:00
|
|
|
if (item.action && item.actionLabel) {
|
|
|
|
// HACK: it's a JSX.Element instead of a string!
|
|
|
|
// react-notification displays it just fine.
|
|
|
|
alert.action = (
|
2022-07-22 10:30:16 -07:00
|
|
|
<Button theme='tertiary' size='sm' onClick={item.action} text={maybeFormatMessage(item.actionLabel)} />
|
2022-06-23 15:29:10 -07:00
|
|
|
) as any;
|
2020-03-27 13:59:38 -07:00
|
|
|
}
|
2022-06-23 15:29:10 -07:00
|
|
|
|
|
|
|
return alert;
|
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-06-23 14:05:11 -07:00
|
|
|
const onDismiss = (alert: NotificationObject) => {
|
2022-01-27 07:00:05 -08:00
|
|
|
dispatch(dismissAlert(alert));
|
|
|
|
};
|
|
|
|
|
2022-06-23 15:29:10 -07:00
|
|
|
const defaultBarStyleFactory: StyleFactoryFn = (index, style, _notification) => {
|
|
|
|
return Object.assign(
|
|
|
|
{},
|
|
|
|
style,
|
|
|
|
{ bottom: `${14 + index * 12 + index * 42}px` },
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const notifications = alerts.toArray().map(buildAlert);
|
|
|
|
|
2022-06-23 14:05:11 -07:00
|
|
|
return (
|
|
|
|
<div role='assertive' data-testid='toast' className='z-1000 fixed inset-0 flex items-end px-4 py-6 pointer-events-none pt-16 lg:pt-20 sm:items-start'>
|
|
|
|
<NotificationStack
|
|
|
|
onDismiss={onDismiss}
|
|
|
|
onClick={onDismiss}
|
|
|
|
barStyleFactory={defaultBarStyleFactory}
|
|
|
|
activeBarStyleFactory={defaultBarStyleFactory}
|
|
|
|
notifications={notifications}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2020-03-27 13:59:38 -07:00
|
|
|
};
|
|
|
|
|
2022-06-23 14:05:11 -07:00
|
|
|
export default SnackbarContainer;
|