@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
89 lines (88 loc) • 3.2 kB
JavaScript
// packages/editor/src/components/collab-sidebar/note-indicator-toolbar.js
import { ToolbarButton } from "@wordpress/components";
import { Stack } from "@wordpress/ui";
import { __, sprintf } from "@wordpress/i18n";
import { useMemo } from "@wordpress/element";
import {
privateApis as blockEditorPrivateApis,
store as blockEditorStore
} from "@wordpress/block-editor";
import { useSelect } from "@wordpress/data";
import { unlock } from "../../lock-unlock.mjs";
import { getAvatarBorderColor } from "./utils.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
var { NoteIconToolbarSlotFill } = unlock(blockEditorPrivateApis);
function ThreadParticipants({ participants }) {
const defaultAvatar = useSelect((select) => {
const { getSettings } = select(blockEditorStore);
const { __experimentalDiscussionSettings } = getSettings();
return __experimentalDiscussionSettings?.avatarURL;
}, []);
const maxAvatars = 3;
const isOverflow = participants.length > maxAvatars;
const visibleParticipants = isOverflow ? participants.slice(0, maxAvatars - 1) : participants;
const overflowCount = Math.max(
0,
participants.length - visibleParticipants.length
);
const threadHasMoreParticipants = participants.length > 100;
const overflowText = threadHasMoreParticipants && overflowCount > 0 ? __("100+") : sprintf(
// translators: %s: Number of participants.
__("+%s"),
overflowCount
);
return /* @__PURE__ */ jsxs(Stack, { direction: "row", align: "center", gap: "xs", children: [
visibleParticipants.map((participant) => /* @__PURE__ */ jsx(
"img",
{
src: participant.avatar || defaultAvatar,
alt: participant.name,
className: "editor-note-indicator__avatar",
style: {
borderColor: getAvatarBorderColor(participant.id)
}
},
participant.id
)),
overflowCount > 0 && /* @__PURE__ */ jsx("span", { className: "editor-note-indicator__overflow", children: overflowText })
] });
}
function NoteAvatarIndicator({ onClick, note }) {
const threadParticipants = useMemo(() => {
if (!note) {
return [];
}
const participantsMap = /* @__PURE__ */ new Map();
const allNotes = [note, ...note.reply].sort(
(a, b) => new Date(a.date) - new Date(b.date)
);
for (const entry of allNotes) {
if (!entry.author_name || participantsMap.has(entry.author)) {
continue;
}
participantsMap.set(entry.author, {
id: entry.author,
name: entry.author_name,
avatar: entry.author_avatar_urls?.["48"] || entry.author_avatar_urls?.["96"]
});
}
return Array.from(participantsMap.values());
}, [note]);
if (!threadParticipants.length) {
return null;
}
return /* @__PURE__ */ jsx(NoteIconToolbarSlotFill.Fill, { children: /* @__PURE__ */ jsx(
ToolbarButton,
{
className: "editor-note-indicator",
label: __("View notes"),
onClick: () => onClick(),
showTooltip: true,
children: /* @__PURE__ */ jsx(ThreadParticipants, { participants: threadParticipants })
}
) });
}
export {
NoteAvatarIndicator
};
//# sourceMappingURL=note-indicator-toolbar.mjs.map