@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
124 lines (123 loc) • 4.16 kB
JavaScript
// 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";
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, showNotifications } = useSelect(
(select) => {
const editorSel = select(editorStore);
return {
postStatus: editorSel.getCurrentPostAttribute("status"),
isCollaborationEnabled: editorSel.isCollaborationEnabledForCurrentPost(),
showNotifications: select(preferencesStore).get(
"core",
"showCollaborationNotifications"
) ?? true
};
},
[]
);
const { createNotice } = useDispatch(noticesStore);
const shouldSubscribe = isCollaborationEnabled && showNotifications;
const effectivePostId = shouldSubscribe ? postId : null;
const effectivePostType = shouldSubscribe ? postType : null;
useOnCollaboratorJoin(
effectivePostId,
effectivePostType,
useCallback(
(collaborator, me) => {
if (me && collaborator.collaboratorInfo.enteredAt < me.collaboratorInfo.enteredAt) {
return;
}
void createNotice(
"info",
sprintf(
/* translators: %s: collaborator display name */
__("%s has joined the post."),
collaborator.collaboratorInfo.name
),
{
id: `${NOTIFICATION_TYPE.COLLAB_USER_ENTERED}-${collaborator.collaboratorInfo.id}`,
type: "snackbar",
isDismissible: false
}
);
},
[createNotice]
)
);
useOnCollaboratorLeave(
effectivePostId,
effectivePostType,
useCallback(
(collaborator) => {
void createNotice(
"info",
sprintf(
/* translators: %s: collaborator display name */
__("%s has left the post."),
collaborator.collaboratorInfo.name
),
{
id: `${NOTIFICATION_TYPE.COLLAB_USER_EXITED}-${collaborator.collaboratorInfo.id}`,
type: "snackbar",
isDismissible: false
}
);
},
[createNotice]
)
);
useOnPostSave(
effectivePostId,
effectivePostType,
useCallback(
(saveEvent, saver, prevEvent) => {
if (!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(
saver.collaboratorInfo.name,
effectiveStatus,
isFirstPublish
);
void createNotice("info", message, {
id: `${NOTIFICATION_TYPE.COLLAB_POST_UPDATED}-${saver.collaboratorInfo.id}`,
type: "snackbar",
isDismissible: false
});
},
[createNotice, postStatus]
)
);
}
export {
useCollaboratorNotifications
};
//# sourceMappingURL=use-collaborator-notifications.mjs.map