bigbuffet-rw/app/soapbox/features/ui/containers/notifications_container.tsx

69 lines
2 KiB
TypeScript
Raw Normal View History

import React from 'react';
2022-06-23 14:05:11 -07:00
import { useIntl } from 'react-intl';
import { NotificationStack, NotificationObject, StyleFactoryFn } from 'react-notification';
import { Link } from 'react-router-dom';
import { Button } from 'soapbox/components/ui';
2022-06-23 14:05:11 -07:00
import { useAppSelector, useAppDispatch } from 'soapbox/hooks';
2020-03-27 13:59:38 -07:00
import { dismissAlert } from '../../../actions/alerts';
import { getAlerts } from '../../../selectors';
2022-06-23 14:05:11 -07:00
const defaultBarStyleFactory: StyleFactoryFn = (index, style, _notification) => {
return Object.assign(
{},
style,
{ bottom: `${14 + index * 12 + index * 42}px` },
);
};
2022-06-23 14:05:11 -07:00
const SnackbarContainer: React.FC = () => {
const intl = useIntl();
const dispatch = useAppDispatch();
const notifications = useAppSelector(getAlerts);
2020-03-27 13:59:38 -07:00
notifications.forEach(notification => {
['title', 'message', 'actionLabel'].forEach(key => {
2022-06-23 14:05:11 -07:00
// @ts-ignore
const value = notification[key];
if (typeof value === 'object') {
2022-06-23 14:05:11 -07:00
// @ts-ignore
notification[key] = intl.formatMessage(value);
}
});
if (notification.action) {
const { action } = notification;
notification.action = (
2022-06-23 14:24:10 -07:00
<Button theme='ghost' size='sm' onClick={action} text={notification.actionLabel} />
);
} else if (notification.actionLabel) {
notification.action = (
<Link to={notification.actionLink}>
{notification.actionLabel}
</Link>
);
2020-03-27 13:59:38 -07:00
}
});
2020-03-27 13:59:38 -07:00
2022-06-23 14:05:11 -07:00
const onDismiss = (alert: NotificationObject) => {
dispatch(dismissAlert(alert));
};
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;