UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

111 lines 5.15 kB
import * as React from 'react'; import { useState, useEffect, useCallback } from 'react'; import { CloseNotificationContext, undoableEventEmitter, useNotificationContext, useTakeUndoableMutation, useTranslate, } from "..//index.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 var Notification = function () { var _a = useNotificationContext(), notifications = _a.notifications, takeNotification = _a.takeNotification; var takeMutation = useTakeUndoableMutation(); var _b = useState(false), open = _b[0], setOpen = _b[1]; var _c = React.useState(undefined), currentNotification = _c[0], setCurrentNotification = _c[1]; var translate = useTranslate(); useEffect(function () { var _a; if (notifications.length && !currentNotification) { // Set a new snack when we don't have an active one var notification = takeNotification(); if (notification) { setCurrentNotification(notification); setOpen(true); } } if (currentNotification) { var beforeunload_1 = function (e) { e.preventDefault(); var confirmationMessage = ''; e.returnValue = confirmationMessage; return confirmationMessage; }; if ((_a = currentNotification === null || currentNotification === void 0 ? void 0 : currentNotification.notificationOptions) === null || _a === void 0 ? void 0 : _a.undoable) { window.addEventListener('beforeunload', beforeunload_1); return function () { window.removeEventListener('beforeunload', beforeunload_1); }; } } }, [notifications, currentNotification, open, takeNotification]); var handleRequestClose = useCallback(function () { setOpen(false); }, [setOpen]); var handleExited = useCallback(function () { var _a; if (currentNotification && ((_a = currentNotification.notificationOptions) === null || _a === void 0 ? void 0 : _a.undoable)) { var mutation = takeMutation(); if (mutation) { mutation({ isUndo: false }); } else { // FIXME kept for BC: remove in v6 undoableEventEmitter.emit('end', { isUndo: false }); } } setCurrentNotification(undefined); }, [currentNotification, takeMutation]); var handleUndo = useCallback(function () { var mutation = takeMutation(); if (mutation) { mutation({ isUndo: true }); } else { // FIXME kept for BC: remove in v6 undoableEventEmitter.emit('end', { isUndo: true }); } setOpen(false); }, [takeMutation]); var _d = currentNotification || {}, message = _d.message, notificationOptions = _d.notificationOptions; var _e = notificationOptions || {}, messageArgs = _e.messageArgs, undoable = _e.undoable; useEffect(function () { if (!undoable) return; var timer = setTimeout(function () { handleExited(); }, (notificationOptions === null || notificationOptions === void 0 ? void 0 : notificationOptions.autoHideDuration) || 4000); return function () { 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