stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
131 lines • 5.42 kB
JavaScript
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 => notification.tags?.includes(SYSTEM_NOTIFICATION_TAG) ?? false;
exports.hasSystemNotificationTag = hasSystemNotificationTag;
var getTargetTags = (target, targetPanels, tags) => {
if (targetPanels) {
return Array.from(new Set([...targetPanels.map(panel => (0, _notificationTarget.getNotificationTargetTag)(panel)), ...(tags ?? [])]));
}
if (target) {
return (0, _notificationTarget.addExactNotificationTargetTag)(target, tags);
}
return tags ?? [];
};
var getTypeFromIncident = ({
incident,
severity,
type
}) => {
if (type) return type;
if (!incident) return undefined;
var status = 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?.notifications;
var addNotification = (0, _react.useCallback)((payload, options) => {
if (!notificationManager) return;
var target = options?.target ?? (options?.targetPanels || (0, _notificationTarget.hasNotificationTargetTag)(payload.options?.tags) ? undefined : contextTarget);
var notificationTags = getTargetTags(target, options?.targetPanels, payload.options?.tags);
var resolvedType = getTypeFromIncident({
incident: options?.incident,
severity: payload.options?.severity,
type: payload.options?.type
});
notificationManager.add(mergeNotificationOptions(payload, {
...(notificationTags.length > 0 ? {
tags: notificationTags
} : {}),
...(resolvedType ? {
type: resolvedType
} : {})
}));
}, [contextTarget, notificationManager]);
var addSystemNotification = (0, _react.useCallback)((payload, options) => {
if (!notificationManager) return '';
var notificationTags = Array.from(new Set([SYSTEM_NOTIFICATION_TAG, ...(payload.options?.tags ?? [])]));
var resolvedType = getTypeFromIncident({
incident: options?.incident,
severity: payload.options?.severity,
type: payload.options?.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