UNPKG

stream-chat-react-native-core

Version:

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

192 lines 9.94 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveNotificationTargetTagIfNeeded = exports.registerNotificationActionTarget = exports.registerActiveNotificationTarget = exports.isNotificationTargetPanel = exports.isNotificationTargetEqual = exports.isNotificationForTarget = exports.isNotificationForPanel = exports.isBuiltInNotificationTargetPanel = exports.hasNotificationTargetTag = exports.getThreadNotificationHostId = exports.getNotificationTargetTag = exports.getNotificationTargetPanels = exports.getNotificationTargetPanel = exports.getNotificationTarget = exports.getNotificationActionTarget = exports.getChannelNotificationHostId = exports.getActiveNotificationTarget = exports.addNotificationTargetTag = exports.addExactNotificationTargetTag = void 0; var NOTIFICATION_TARGET_PANELS = ['channel', 'thread', 'channel-list', 'thread-list', 'channel-details']; var NOTIFICATION_TARGET_TAG_PREFIX = 'target:'; var activeNotificationTargets = new WeakMap(); var actionNotificationTargets = new WeakMap(); var isNotificationTargetPanel = value => typeof value === 'string' && value.length > 0 && !value.includes(':'); exports.isNotificationTargetPanel = isNotificationTargetPanel; var isBuiltInNotificationTargetPanel = value => typeof value === 'string' && NOTIFICATION_TARGET_PANELS.includes(value); exports.isBuiltInNotificationTargetPanel = isBuiltInNotificationTargetPanel; var getNotificationTargetTag = (panel, hostId) => `${NOTIFICATION_TARGET_TAG_PREFIX}${panel}${hostId ? `:${hostId}` : ''}`; exports.getNotificationTargetTag = getNotificationTargetTag; var addNotificationTargetTag = (panel, tags, hostId) => { if (!panel) return tags != null ? tags : []; return Array.from(new Set([getNotificationTargetTag(panel, hostId), ...(tags != null ? tags : [])])); }; exports.addNotificationTargetTag = addNotificationTargetTag; var addExactNotificationTargetTag = (target, tags) => addNotificationTargetTag(target.panel, tags, target.hostId); exports.addExactNotificationTargetTag = addExactNotificationTargetTag; var getChannelNotificationHostId = cid => `channel:${cid}`; exports.getChannelNotificationHostId = getChannelNotificationHostId; var getThreadNotificationHostId = threadId => `thread:${threadId}`; exports.getThreadNotificationHostId = getThreadNotificationHostId; var isNotificationTargetEqual = (left, right) => !!left && !!right && left.panel === right.panel && left.hostId === right.hostId; exports.isNotificationTargetEqual = isNotificationTargetEqual; var isRecord = value => typeof value === 'object' && value !== null; var getStringValue = value => typeof value === 'string' ? value : undefined; var parseNotificationTargetTag = tag => { if (!tag.startsWith(NOTIFICATION_TARGET_TAG_PREFIX)) return undefined; var tagValue = tag.slice(NOTIFICATION_TARGET_TAG_PREFIX.length); var hostSeparatorIndex = tagValue.indexOf(':'); var panel = hostSeparatorIndex === -1 ? tagValue : tagValue.slice(0, hostSeparatorIndex); if (!isNotificationTargetPanel(panel)) return undefined; var hostId = hostSeparatorIndex === -1 ? undefined : tagValue.slice(hostSeparatorIndex + 1); return hostId ? { hostId, panel } : { panel }; }; var getNotificationTargetTags = notification => { var _notification$tags; return ((_notification$tags = notification.tags) != null ? _notification$tags : []).map(parseNotificationTargetTag).filter(target => !!target); }; var hasNotificationTargetTag = tags => { var _tags$some; return (_tags$some = tags == null ? void 0 : tags.some(tag => !!parseNotificationTargetTag(tag))) != null ? _tags$some : false; }; exports.hasNotificationTargetTag = hasNotificationTargetTag; var getExactNotificationTargetTags = notification => getNotificationTargetTags(notification).filter(target => !!target.hostId); var getNotificationTargetFromTags = notification => getExactNotificationTargetTags(notification)[0]; var getNotificationTargetFromComposer = notification => { var _notification$origin$; var composer = (_notification$origin$ = notification.origin.context) == null ? void 0 : _notification$origin$.composer; if (!isRecord(composer)) return undefined; var threadId = getStringValue(composer.threadId); if (threadId) { return { hostId: getThreadNotificationHostId(threadId), panel: 'thread' }; } var channel = composer.channel; if (!isRecord(channel)) return undefined; var cid = getStringValue(channel.cid); return cid ? { hostId: getChannelNotificationHostId(cid), panel: 'channel' } : undefined; }; var getNotificationTarget = notification => { var _getNotificationTarge; return (_getNotificationTarge = getNotificationTargetFromTags(notification)) != null ? _getNotificationTarge : getNotificationTargetFromComposer(notification); }; exports.getNotificationTarget = getNotificationTarget; var getExplicitNotificationTargetPanels = notification => { var _notification$origin$2; var targetPanels = getNotificationTargetTags(notification).map(({ panel }) => panel); if (targetPanels.length > 0) { return Array.from(new Set(targetPanels)); } var panel = (_notification$origin$2 = notification.origin.context) == null ? void 0 : _notification$origin$2.panel; return isNotificationTargetPanel(panel) ? [panel] : []; }; var getNotificationTargetPanel = notification => { var _getNotificationTarge2; var explicitPanels = getExplicitNotificationTargetPanels(notification); if (explicitPanels.length > 0) return explicitPanels[0]; return (_getNotificationTarge2 = getNotificationTarget(notification)) == null ? void 0 : _getNotificationTarge2.panel; }; exports.getNotificationTargetPanel = getNotificationTargetPanel; var getNotificationTargetPanels = notification => { var explicitPanels = getExplicitNotificationTargetPanels(notification); if (explicitPanels.length > 0) return explicitPanels; var target = getNotificationTarget(notification); return target ? [target.panel] : []; }; exports.getNotificationTargetPanels = getNotificationTargetPanels; var getNotificationTargetStack = (stackMap, owner) => { var _stackMap$get; var stack = (_stackMap$get = stackMap.get(owner)) != null ? _stackMap$get : []; stackMap.set(owner, stack); return stack; }; var registerNotificationTarget = (stackMap, owner, target) => { var token = Symbol('notification-target'); var stack = getNotificationTargetStack(stackMap, owner); stack.push({ target, token }); return () => { var currentStack = stackMap.get(owner); if (!currentStack) return; var itemIndex = currentStack.findIndex(item => item.token === token); if (itemIndex !== -1) { currentStack.splice(itemIndex, 1); } if (currentStack.length === 0) { stackMap.delete(owner); } }; }; var getLastNotificationTarget = (stackMap, owner) => { var _stack; var stack = stackMap.get(owner); return stack == null || (_stack = stack[stack.length - 1]) == null ? void 0 : _stack.target; }; var registerActiveNotificationTarget = (owner, target) => registerNotificationTarget(activeNotificationTargets, owner, target); exports.registerActiveNotificationTarget = registerActiveNotificationTarget; var registerNotificationActionTarget = (owner, target) => registerNotificationTarget(actionNotificationTargets, owner, target); exports.registerNotificationActionTarget = registerNotificationActionTarget; var getActiveNotificationTarget = owner => getLastNotificationTarget(activeNotificationTargets, owner); exports.getActiveNotificationTarget = getActiveNotificationTarget; var getNotificationActionTarget = owner => getLastNotificationTarget(actionNotificationTargets, owner); exports.getNotificationActionTarget = getNotificationActionTarget; var hasExactNotificationTargetTag = notification => getExactNotificationTargetTags(notification).length > 0; var hasExplicitNotificationTargetPanel = notification => getExplicitNotificationTargetPanels(notification).length > 0; var setNotificationTargetTag = (notification, target) => { notification.tags = addExactNotificationTargetTag(target, notification.tags); }; var resolveNotificationTargetTagIfNeeded = (owner, notification) => { if (hasExactNotificationTargetTag(notification)) return false; if (hasExplicitNotificationTargetPanel(notification)) return false; var composerTarget = getNotificationTargetFromComposer(notification); if (composerTarget) { setNotificationTargetTag(notification, composerTarget); return true; } var actionTarget = getNotificationActionTarget(owner); if (actionTarget) { setNotificationTargetTag(notification, actionTarget); return true; } var activeTarget = getActiveNotificationTarget(owner); if (activeTarget) { setNotificationTargetTag(notification, activeTarget); return true; } return false; }; exports.resolveNotificationTargetTagIfNeeded = resolveNotificationTargetTagIfNeeded; var isNotificationForTarget = (notification, target) => { var exactTargets = getExactNotificationTargetTags(notification); if (exactTargets.length > 0) { return exactTargets.some(exactTarget => isNotificationTargetEqual(exactTarget, target)); } var explicitTargetPanels = getExplicitNotificationTargetPanels(notification); if (explicitTargetPanels.length > 0) { return explicitTargetPanels.includes(target.panel); } var inferredTarget = getNotificationTargetFromComposer(notification); if (inferredTarget) { return isNotificationTargetEqual(inferredTarget, target); } return false; }; exports.isNotificationForTarget = isNotificationForTarget; var isNotificationForPanel = (notification, panel) => { var explicitTargetPanels = getNotificationTargetPanels(notification); if (explicitTargetPanels.length > 0) { return explicitTargetPanels.includes(panel); } return false; }; exports.isNotificationForPanel = isNotificationForPanel; //# sourceMappingURL=notificationTarget.js.map