stream-chat-react-native-core
Version:
The official React Native and Expo components for Stream Chat, a service for building chat applications
120 lines • 5.3 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useNotificationListController = void 0;
var _react = require("react");
var _useNotificationApi2 = require("./useNotificationApi");
var _useNotifications = require("./useNotifications");
var _ChatContext = require("../../../contexts/chatContext/ChatContext");
var _useLazyRef = require("../../../hooks/useLazyRef");
var _useStableCallback = require("../../../hooks/useStableCallback");
var _notificationTarget = require("../notificationTarget");
var _NotificationTargetContext = require("../NotificationTargetContext");
var ACTION_NOTIFICATION_DURATION = 5000;
var getNewestNotification = notifications => notifications.reduce((newest, notification) => !newest || notification.createdAt >= newest.createdAt ? notification : newest, null);
var isPersistentNotification = notification => !notification.duration;
var getActiveNotification = notifications => {
var persistentNotifications = notifications.filter(isPersistentNotification);
return getNewestNotification(persistentNotifications.length > 0 ? persistentNotifications : notifications);
};
var getNotificationDurationOverride = notification => {
if (isPersistentNotification(notification)) return undefined;
if (!notification.actions?.length) return undefined;
return Math.max(notification.duration ?? 0, ACTION_NOTIFICATION_DURATION);
};
var useNotificationListController = ({
filter,
hostId,
panel
} = {}) => {
var _useChatContext = (0, _ChatContext.useChatContext)(),
client = _useChatContext.client;
var _useNotificationApi = (0, _useNotificationApi2.useNotificationApi)(),
removeNotification = _useNotificationApi.removeNotification,
startNotificationTimeout = _useNotificationApi.startNotificationTimeout;
var target = (0, _NotificationTargetContext.useResolvedNotificationTarget)({
hostId,
panel
});
var startedTimeoutIdsRef = (0, _useLazyRef.useLazyRef)(() => new Set());
var combinedFilter = (0, _useStableCallback.useStableCallback)(notification => {
if ((0, _useNotificationApi2.hasSystemNotificationTag)(notification)) return false;
return filter ? filter(notification) : true;
});
var notifications = (0, _useNotifications.useNotifications)({
filter: combinedFilter,
requireTarget: true,
target
});
var notification = getActiveNotification(notifications);
var dismissNotification = (0, _useStableCallback.useStableCallback)(() => {
if (!notification) return;
startedTimeoutIdsRef.current.delete(notification.id);
removeNotification(notification.id);
});
var removeCurrentPanelNotifications = (0, _useStableCallback.useStableCallback)(() => {
if (!target) return;
startedTimeoutIdsRef.current.clear();
client.notifications.notifications.filter(notification => !(0, _useNotificationApi2.hasSystemNotificationTag)(notification) && (0, _notificationTarget.isNotificationForTarget)(notification, target)).forEach(({
id
}) => removeNotification(id));
});
(0, _react.useEffect)(() => {
return client.notifications.store.addPreprocessor((nextState, previousState) => {
var previousNotificationIds = new Set(previousState?.notifications.map(({
id
}) => id) ?? []);
nextState.notifications.forEach(notification => {
if (previousNotificationIds.has(notification.id)) return;
if ((0, _useNotificationApi2.hasSystemNotificationTag)(notification)) return;
(0, _notificationTarget.resolveNotificationTargetTagIfNeeded)(client.notifications, notification);
});
});
}, [client.notifications]);
(0, _react.useEffect)(() => {
if (!target) return;
return (0, _notificationTarget.registerActiveNotificationTarget)(client.notifications, target);
}, [client.notifications, target]);
(0, _react.useEffect)(() => {
var notificationIds = new Set(notifications.map(({
id
}) => id));
startedTimeoutIdsRef.current.forEach(id => {
if (!notificationIds.has(id)) {
startedTimeoutIdsRef.current.delete(id);
}
});
}, [notifications, startedTimeoutIdsRef]);
(0, _react.useEffect)(() => {
if (!notification || notifications.length <= 1) return;
notifications.forEach(({
id
}) => {
if (id === notification.id) return;
startedTimeoutIdsRef.current.delete(id);
removeNotification(id);
});
}, [notification, notifications, removeNotification, startedTimeoutIdsRef]);
(0, _react.useEffect)(() => {
if (!notification) return;
if (startedTimeoutIdsRef.current.has(notification.id)) return;
startedTimeoutIdsRef.current.add(notification.id);
var durationOverride = getNotificationDurationOverride(notification);
if (typeof durationOverride === 'number') {
startNotificationTimeout(notification.id, durationOverride);
} else {
startNotificationTimeout(notification.id);
}
}, [notification, startNotificationTimeout, startedTimeoutIdsRef]);
(0, _react.useEffect)(() => {
return () => {
removeCurrentPanelNotifications();
};
}, [removeCurrentPanelNotifications]);
return {
dismissNotification,
notification
};
};
exports.useNotificationListController = useNotificationListController;
//# sourceMappingURL=useNotificationListController.js.map