ra-core
Version:
Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React
114 lines • 4.99 kB
JavaScript
import * as React from 'react';
import { useState, useEffect, useCallback } from 'react';
import { useTranslate } from "../i18n/useTranslate.js";
import { useNotificationContext } from "../notification/useNotificationContext.js";
import { CloseNotificationContext } from "../notification/CloseNotificationContext.js";
import undoableEventEmitter from "../dataProvider/undoableEventEmitter.js";
import { useTakeUndoableMutation } from "../dataProvider/undo/useTakeUndoableMutation.js";
/**
* Provides a way to show a notification.
* @see useNotify
*
* @example <caption>Basic usage</caption>
* <Notification />
*
* @param props The component props
* @param {string} props.type The notification type. Defaults to 'info'.
* @param {number} props.autoHideDuration Duration in milliseconds to wait until hiding a given notification. Defaults to 4000.
* @param {boolean} props.multiLine Set it to `true` if the notification message should be shown in more than one line.
*/
export const Notification = () => {
const { notifications, takeNotification } = useNotificationContext();
const takeMutation = useTakeUndoableMutation();
const [open, setOpen] = useState(false);
const [currentNotification, setCurrentNotification] = React.useState(undefined);
const translate = useTranslate();
useEffect(() => {
if (notifications.length && !currentNotification) {
// Set a new snack when we don't have an active one
const notification = takeNotification();
if (notification) {
setCurrentNotification(notification);
setOpen(true);
}
}
if (currentNotification) {
const beforeunload = (e) => {
e.preventDefault();
const confirmationMessage = '';
e.returnValue = confirmationMessage;
return confirmationMessage;
};
if (currentNotification?.notificationOptions?.undoable) {
window.addEventListener('beforeunload', beforeunload);
return () => {
window.removeEventListener('beforeunload', beforeunload);
};
}
}
}, [notifications, currentNotification, open, takeNotification]);
const handleRequestClose = useCallback(() => {
setOpen(false);
setCurrentNotification(undefined);
}, [setOpen]);
const handleExited = useCallback(() => {
if (currentNotification &&
currentNotification.notificationOptions?.undoable) {
const mutation = takeMutation();
if (mutation) {
mutation({ isUndo: false });
}
else {
// FIXME kept for BC: remove in v6
undoableEventEmitter.emit('end', { isUndo: false });
}
}
setCurrentNotification(undefined);
}, [currentNotification, takeMutation]);
const handleUndo = useCallback(() => {
const mutation = takeMutation();
if (mutation) {
mutation({ isUndo: true });
}
else {
// FIXME kept for BC: remove in v6
undoableEventEmitter.emit('end', { isUndo: true });
}
setOpen(false);
}, [takeMutation]);
const { message, notificationOptions } = currentNotification || {};
const { messageArgs, undoable } = notificationOptions || {};
useEffect(() => {
if (!undoable)
return;
const timer = setTimeout(() => {
handleExited();
}, notificationOptions?.autoHideDuration || 4000);
return () => clearTimeout(timer);
}, [undoable, handleExited, notificationOptions]);
if (!currentNotification)
return null;
return (React.createElement(CloseNotificationContext.Provider, { value: handleRequestClose },
React.createElement("div", { className: "ra-notification", style: {
color: 'white',
position: 'fixed',
bottom: 16,
right: 16,
zIndex: 1400,
display: open ? 'flex' : 'none',
paddingLeft: '16px',
paddingRight: '16px',
borderRadius: '4px',
alignItems: 'center',
justifyContent: 'center',
gap: '16px',
backgroundColor: 'rgba(0, 0, 0, 0.8)',
} },
React.createElement("p", null, message && typeof message === 'string'
? translate(message, messageArgs)
: message),
React.createElement("div", { style: { display: 'flex', gap: '8px' } },
undoable ? (React.createElement("button", { onClick: handleUndo, type: "button" }, translate('ra.action.undo'))) : null,
React.createElement("button", { onClick: handleExited, type: "button" }, translate('ra.action.close'))))));
};
//# sourceMappingURL=Notification.js.map