UNPKG

stream-chat-react-native-core

Version:

The official React Native and Expo components for Stream Chat, a service for building chat applications

137 lines 6.54 kB
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useNotificationApi = exports.hasSystemNotificationTag = exports.SYSTEM_NOTIFICATION_TAG = void 0; var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _react = require("react"); var _ChatContext = require("../../../contexts/chatContext/ChatContext"); var _notificationTarget = require("../notificationTarget"); var _NotificationTargetContext = require("../NotificationTargetContext"); var SYSTEM_NOTIFICATION_TAG = exports.SYSTEM_NOTIFICATION_TAG = 'system'; var hasSystemNotificationTag = notification => { var _notification$tags$in, _notification$tags; return (_notification$tags$in = (_notification$tags = notification.tags) == null ? void 0 : _notification$tags.includes(SYSTEM_NOTIFICATION_TAG)) != null ? _notification$tags$in : false; }; exports.hasSystemNotificationTag = hasSystemNotificationTag; var getTargetTags = (target, targetPanels, tags) => { if (targetPanels) { return Array.from(new Set([...targetPanels.map(panel => (0, _notificationTarget.getNotificationTargetTag)(panel)), ...(tags != null ? tags : [])])); } if (target) { return (0, _notificationTarget.addExactNotificationTargetTag)(target, tags); } return tags != null ? tags : []; }; var getTypeFromIncident = ({ incident, severity, type }) => { var _incident$status; if (type) return type; if (!incident) return undefined; var status = (_incident$status = incident.status) != null ? _incident$status : severity === 'error' ? 'failed' : severity === 'success' ? 'success' : severity; return [incident.domain, incident.entity, incident.operation, status].filter(segment => !!segment).join(':'); }; var mergeNotificationOptions = (payload, options) => { var mergedOptions = { ...payload.options, ...options }; if (!payload.options && Object.keys(mergedOptions).length === 0) { return payload; } return { ...payload, options: mergedOptions }; }; var useNotificationApi = () => { var _useChatContext = (0, _ChatContext.useChatContext)(), client = _useChatContext.client; var contextTarget = (0, _NotificationTargetContext.useNotificationTargetContext)(); var notificationManager = client == null ? void 0 : client.notifications; var addNotification = (0, _react.useCallback)((payload, options) => { var _options$target, _payload$options, _payload$options2, _payload$options3, _payload$options4; if (!notificationManager) return; var target = (_options$target = options == null ? void 0 : options.target) != null ? _options$target : options != null && options.targetPanels || (0, _notificationTarget.hasNotificationTargetTag)((_payload$options = payload.options) == null ? void 0 : _payload$options.tags) ? undefined : contextTarget; var notificationTags = getTargetTags(target, options == null ? void 0 : options.targetPanels, (_payload$options2 = payload.options) == null ? void 0 : _payload$options2.tags); var resolvedType = getTypeFromIncident({ incident: options == null ? void 0 : options.incident, severity: (_payload$options3 = payload.options) == null ? void 0 : _payload$options3.severity, type: (_payload$options4 = payload.options) == null ? void 0 : _payload$options4.type }); notificationManager.add(mergeNotificationOptions(payload, { ...(notificationTags.length > 0 ? { tags: notificationTags } : {}), ...(resolvedType ? { type: resolvedType } : {}) })); }, [contextTarget, notificationManager]); var addSystemNotification = (0, _react.useCallback)((payload, options) => { var _payload$options$tags, _payload$options5, _payload$options6, _payload$options7; if (!notificationManager) return ''; var notificationTags = Array.from(new Set([SYSTEM_NOTIFICATION_TAG, ...((_payload$options$tags = (_payload$options5 = payload.options) == null ? void 0 : _payload$options5.tags) != null ? _payload$options$tags : [])])); var resolvedType = getTypeFromIncident({ incident: options == null ? void 0 : options.incident, severity: (_payload$options6 = payload.options) == null ? void 0 : _payload$options6.severity, type: (_payload$options7 = payload.options) == null ? void 0 : _payload$options7.type }); return notificationManager.add(mergeNotificationOptions(payload, { ...(notificationTags.length > 0 ? { tags: notificationTags } : {}), ...(resolvedType ? { type: resolvedType } : {}) })); }, [notificationManager]); var removeNotification = (0, _react.useCallback)(id => { if (!notificationManager) return; notificationManager.remove(id); }, [notificationManager]); var removeNotificationsForCurrentPanel = (0, _react.useCallback)(() => { if (!contextTarget || !notificationManager) return; var notificationIds = notificationManager.notifications.filter(notification => !hasSystemNotificationTag(notification) && (0, _notificationTarget.isNotificationForTarget)(notification, contextTarget)).map(({ id }) => id); notificationIds.forEach(removeNotification); }, [contextTarget, notificationManager, removeNotification]); var runWithNotificationTarget = (0, _react.useCallback)(function () { var _ref = (0, _asyncToGenerator2.default)(function* (callback, target = contextTarget) { if (!target || !notificationManager) { return yield callback(); } var unregisterActionTarget = (0, _notificationTarget.registerNotificationActionTarget)(notificationManager, target); try { return yield callback(); } finally { unregisterActionTarget(); } }); return function (_x) { return _ref.apply(this, arguments); }; }(), [contextTarget, notificationManager]); var startNotificationTimeout = (0, _react.useCallback)((id, durationOverride) => { if (!notificationManager) return; if (typeof durationOverride === 'number') { notificationManager.startTimeout(id, durationOverride); return; } notificationManager.startTimeout(id); }, [notificationManager]); return { addNotification, addSystemNotification, removeNotification, removeNotificationsForCurrentPanel, runWithNotificationTarget, startNotificationTimeout }; }; exports.useNotificationApi = useNotificationApi; //# sourceMappingURL=useNotificationApi.js.map