UNPKG

@wordpress/block-library

Version:
102 lines (101 loc) • 3.51 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_data = require("@wordpress/data"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_core_data = require("@wordpress/core-data"); // packages/block-library/src/comment-template/hooks.js const MAX_COMMENTS_PER_PAGE = 100; /** * Return an object with the query args needed to fetch the default page of * comments. * * @param {Object} props Hook props. * @param {number} props.postId ID of the post that contains the comments. * discussion settings. * * @return {Object} Query args to retrieve the comments. */ const useCommentQueryArgs = ({ postId }) => { const queryArgs = { status: "approve", order: "asc", context: "embed", parent: 0, _embed: "children" }; const { pageComments, commentsPerPage, defaultCommentsPage: defaultPage } = (0, _wordpress_data.useSelect)((select) => { const { getSettings } = select(_wordpress_block_editor.store); const { __experimentalDiscussionSettings } = getSettings(); return __experimentalDiscussionSettings ?? {}; }, []); const perPage = pageComments ? Math.min(commentsPerPage, MAX_COMMENTS_PER_PAGE) : MAX_COMMENTS_PER_PAGE; const page = useDefaultPageIndex({ defaultPage, postId, perPage, queryArgs }); return (0, _wordpress_element.useMemo)(() => { return page ? { ...queryArgs, post: postId, per_page: perPage, page } : null; }, [ postId, perPage, page ]); }; /** * Return the index of the default page, depending on whether `defaultPage` is * `newest` or `oldest`. In the first case, the only way to know the page's * index is by using the `X-WP-Total` header, which forces to make an * additional request. * * @param {Object} props Hook props. * @param {string} props.defaultPage Page shown by default (newest/oldest). * @param {number} props.postId ID of the post that contains the comments. * @param {number} props.perPage The number of comments included per page. * @param {Object} props.queryArgs Other query args. * * @return {number} Index of the default comments page. */ const useDefaultPageIndex = ({ defaultPage, postId, perPage, queryArgs }) => { const isNewest = defaultPage === "newest"; const { totalItems, hasResolved } = (0, _wordpress_core_data.useEntityRecords)("root", "comment", { ...queryArgs, post: postId, per_page: 1, _fields: "id" }, { enabled: isNewest }); if (!isNewest) return 1; if (totalItems === null && !hasResolved) return 0; return Math.max(Math.ceil((totalItems ?? 0) / perPage), 1); }; /** * Generate a tree structure of comment IDs from a list of comment entities. The * children of each comment are obtained from `_embedded`. * * @typedef {{ commentId: number, children: CommentNode }} CommentNode * * @param {Object[]} topLevelComments List of comment entities. * @return {{ commentTree: CommentNode[]}} Tree of comment IDs. */ const useCommentTree = (topLevelComments) => { return (0, _wordpress_element.useMemo)(() => topLevelComments?.map(({ id, _embedded }) => { const [children] = _embedded?.children || [[]]; return { commentId: id, children: children.map((child) => ({ commentId: child.id })) }; }), [topLevelComments]); }; exports.useCommentQueryArgs = useCommentQueryArgs; exports.useCommentTree = useCommentTree; //# sourceMappingURL=hooks.cjs.map