UNPKG

@wordpress/block-library

Version:
96 lines (95 loc) • 3.19 kB
import { useMemo } from "@wordpress/element"; import { useSelect } from "@wordpress/data"; import { store } from "@wordpress/block-editor"; import { useEntityRecords } from "@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 } = useSelect((select) => { const { getSettings } = select(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 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 } = 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 useMemo(() => topLevelComments?.map(({ id, _embedded }) => { const [children] = _embedded?.children || [[]]; return { commentId: id, children: children.map((child) => ({ commentId: child.id })) }; }), [topLevelComments]); }; export { useCommentQueryArgs, useCommentTree }; //# sourceMappingURL=hooks.mjs.map