UNPKG

@wordpress/editor

Version:
157 lines (156 loc) 5.57 kB
// packages/editor/src/components/collaborators-presence/use-collaborator-notifications.ts import { useDispatch, useSelect } from "@wordpress/data"; import { useCallback } from "@wordpress/element"; import { __, sprintf } from "@wordpress/i18n"; import { store as noticesStore } from "@wordpress/notices"; import { privateApis } from "@wordpress/core-data"; import { store as preferencesStore } from "@wordpress/preferences"; import { unlock } from "../../lock-unlock.mjs"; import { store as editorStore } from "../../store/index.mjs"; import { getCollaboratorDisplayName } from "../../utils/get-collaborator-display-name.mjs"; var { useOnCollaboratorJoin, useOnCollaboratorLeave, useOnPostSave } = unlock(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 sprintf(__("Post published by %s."), name); } if (PUBLISHED_STATUSES.includes(status)) { return sprintf(__("Post updated by %s."), name); } return sprintf(__("Draft saved by %s."), name); } function useCollaboratorNotifications(postId, postType) { const { postStatus, isCollaborationEnabled, showJoinNotifications, showLeaveNotifications, showPostSaveNotifications } = useSelect((select) => { const { getCurrentPostAttribute, isCollaborationEnabledForCurrentPost } = unlock(select(editorStore)); const getNotificationPreference = (name) => select(preferencesStore).get("core", name) ?? true; return { postStatus: getCurrentPostAttribute("status"), isCollaborationEnabled: isCollaborationEnabledForCurrentPost(), showJoinNotifications: getNotificationPreference( "showCollaborationJoinNotifications" ), showLeaveNotifications: getNotificationPreference( "showCollaborationLeaveNotifications" ), showPostSaveNotifications: getNotificationPreference( "showCollaborationPostSaveNotifications" ) }; }, []); const { createNotice } = useDispatch(noticesStore); 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, useCallback( (collaborator, me) => { if (!shouldShowJoinNotifications) { return; } if (me && collaborator.collaboratorInfo.enteredAt < me.collaboratorInfo.enteredAt) { return; } void createNotice( "info", sprintf( /* translators: %s: collaborator display name */ __("%s has joined the post."), getCollaboratorDisplayName( collaborator.collaboratorInfo ) ), { id: `${NOTIFICATION_TYPE.COLLAB_USER_ENTERED}-${collaborator.collaboratorInfo.id ?? collaborator.clientId}`, type: "snackbar", isDismissible: false } ); }, [createNotice, shouldShowJoinNotifications] ) ); useOnCollaboratorLeave( leavePostId, leavePostType, useCallback( (collaborator) => { if (!shouldShowLeaveNotifications) { return; } void createNotice( "info", sprintf( /* translators: %s: collaborator display name */ __("%s has left the post."), getCollaboratorDisplayName( collaborator.collaboratorInfo ) ), { id: `${NOTIFICATION_TYPE.COLLAB_USER_EXITED}-${collaborator.collaboratorInfo.id ?? collaborator.clientId}`, type: "snackbar", isDismissible: false } ); }, [createNotice, shouldShowLeaveNotifications] ) ); useOnPostSave( postSavePostId, postSavePostType, 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( 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] ) ); } export { useCollaboratorNotifications }; //# sourceMappingURL=use-collaborator-notifications.mjs.map