UNPKG

@wordpress/editor

Version:
180 lines (178 loc) 7.16 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/editor/src/components/collaborators-presence/use-collaborator-notifications.ts var use_collaborator_notifications_exports = {}; __export(use_collaborator_notifications_exports, { useCollaboratorNotifications: () => useCollaboratorNotifications }); module.exports = __toCommonJS(use_collaborator_notifications_exports); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_i18n = require("@wordpress/i18n"); var import_notices = require("@wordpress/notices"); var import_core_data = require("@wordpress/core-data"); var import_preferences = require("@wordpress/preferences"); var import_lock_unlock = require("../../lock-unlock.cjs"); var import_store = require("../../store/index.cjs"); var import_get_collaborator_display_name = require("../../utils/get-collaborator-display-name.cjs"); var { useOnCollaboratorJoin, useOnCollaboratorLeave, useOnPostSave } = (0, import_lock_unlock.unlock)(import_core_data.privateApis); var NOTIFICATION_TYPE = { COLLAB_POST_UPDATED: "collab-post-updated", COLLAB_USER_ENTERED: "collab-user-entered", COLLAB_USER_EXITED: "collab-user-exited" }; var PUBLISHED_STATUSES = ["publish", "private", "future"]; function getPostUpdatedMessage(name, status, isFirstPublish) { if (isFirstPublish) { return (0, import_i18n.sprintf)((0, import_i18n.__)("Post published by %s."), name); } if (PUBLISHED_STATUSES.includes(status)) { return (0, import_i18n.sprintf)((0, import_i18n.__)("Post updated by %s."), name); } return (0, import_i18n.sprintf)((0, import_i18n.__)("Draft saved by %s."), name); } function useCollaboratorNotifications(postId, postType) { const { postStatus, isCollaborationEnabled, showJoinNotifications, showLeaveNotifications, showPostSaveNotifications } = (0, import_data.useSelect)((select) => { const { getCurrentPostAttribute, isCollaborationEnabledForCurrentPost } = (0, import_lock_unlock.unlock)(select(import_store.store)); const getNotificationPreference = (name) => select(import_preferences.store).get("core", name) ?? true; return { postStatus: getCurrentPostAttribute("status"), isCollaborationEnabled: isCollaborationEnabledForCurrentPost(), showJoinNotifications: getNotificationPreference( "showCollaborationJoinNotifications" ), showLeaveNotifications: getNotificationPreference( "showCollaborationLeaveNotifications" ), showPostSaveNotifications: getNotificationPreference( "showCollaborationPostSaveNotifications" ) }; }, []); const { createNotice } = (0, import_data.useDispatch)(import_notices.store); const shouldShowJoinNotifications = isCollaborationEnabled && showJoinNotifications; const shouldShowLeaveNotifications = isCollaborationEnabled && showLeaveNotifications; const shouldShowPostSaveNotifications = isCollaborationEnabled && showPostSaveNotifications; const effectiveTarget = (shouldShow) => shouldShow ? [postId, postType] : [null, null]; const [joinPostId, joinPostType] = effectiveTarget( shouldShowJoinNotifications ); const [leavePostId, leavePostType] = effectiveTarget( shouldShowLeaveNotifications ); const [postSavePostId, postSavePostType] = effectiveTarget( shouldShowPostSaveNotifications ); useOnCollaboratorJoin( joinPostId, joinPostType, (0, import_element.useCallback)( (collaborator, me) => { if (!shouldShowJoinNotifications) { return; } if (me && collaborator.collaboratorInfo.enteredAt < me.collaboratorInfo.enteredAt) { return; } void createNotice( "info", (0, import_i18n.sprintf)( /* translators: %s: collaborator display name */ (0, import_i18n.__)("%s has joined the post."), (0, import_get_collaborator_display_name.getCollaboratorDisplayName)( collaborator.collaboratorInfo ) ), { id: `${NOTIFICATION_TYPE.COLLAB_USER_ENTERED}-${collaborator.collaboratorInfo.id ?? collaborator.clientId}`, type: "snackbar", isDismissible: false } ); }, [createNotice, shouldShowJoinNotifications] ) ); useOnCollaboratorLeave( leavePostId, leavePostType, (0, import_element.useCallback)( (collaborator) => { if (!shouldShowLeaveNotifications) { return; } void createNotice( "info", (0, import_i18n.sprintf)( /* translators: %s: collaborator display name */ (0, import_i18n.__)("%s has left the post."), (0, import_get_collaborator_display_name.getCollaboratorDisplayName)( collaborator.collaboratorInfo ) ), { id: `${NOTIFICATION_TYPE.COLLAB_USER_EXITED}-${collaborator.collaboratorInfo.id ?? collaborator.clientId}`, type: "snackbar", isDismissible: false } ); }, [createNotice, shouldShowLeaveNotifications] ) ); useOnPostSave( postSavePostId, postSavePostType, (0, import_element.useCallback)( (saveEvent, saver, prevEvent) => { if (!shouldShowPostSaveNotifications || !postStatus) { return; } const effectiveStatus = saveEvent.postStatus ?? postStatus ?? "draft"; const prevStatus = prevEvent?.postStatus ?? postStatus; const isFirstPublish = !(prevStatus && PUBLISHED_STATUSES.includes(prevStatus)) && PUBLISHED_STATUSES.includes(effectiveStatus); const message = getPostUpdatedMessage( (0, import_get_collaborator_display_name.getCollaboratorDisplayName)(saver.collaboratorInfo), effectiveStatus, isFirstPublish ); void createNotice("info", message, { id: `${NOTIFICATION_TYPE.COLLAB_POST_UPDATED}-${saver.collaboratorInfo.id ?? saver.clientId}`, type: "snackbar", isDismissible: false }); }, [createNotice, postStatus, shouldShowPostSaveNotifications] ) ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useCollaboratorNotifications }); //# sourceMappingURL=use-collaborator-notifications.cjs.map