import { useState, memo } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; import { BlockContextProvider, useBlockProps, useInnerBlocksProps, store as blockEditorStore, __experimentalUseBlockPreview as useBlockPreview, } from '@wordpress/block-editor'; import { Spinner as WCSpinner } from '@wordpress/components'; import { store as coreStore } from '@wordpress/core-data'; import type { BlockEditProps } from '@wordpress/blocks'; import { useCommentQueryArgs, useCommentTree } from './hooks'; import type { CommentNode, CommentTemplateBlockContext, CommentTemplateInnerBlocksProps, CommentTemplatePreviewProps, CommentsListProps, CommentsPlaceholderSettings, DiscussionSettings, EmbeddedComment, } from './types'; /** * Function that returns a comment structure that will be rendered with default placeholders. * * Each comment has a `commentId` property that is always a negative number in * case of the placeholders. This is to ensure that the comment does not * conflict with the actual (real) comments. * * @param settings Discussion Settings. * @param settings.perPage - Comments per page setting or block attribute. * @param settings.pageComments - Enable break comments into pages setting. * @param settings.threadComments - Enable threaded (nested) comments setting. * @param settings.threadCommentsDepth - Level deep of threaded comments. * * @return Inner blocks of the Comment Template */ const getCommentsPlaceholder = ( { perPage, pageComments, threadComments, threadCommentsDepth, }: CommentsPlaceholderSettings ): CommentNode[] => { // Limit commentsDepth to 3 const commentsDepth = ! threadComments ? 1 : Math.min( threadCommentsDepth!, 3 ); const buildChildrenComment = ( commentsLevel: number ): CommentNode[] => { // Render children comments until commentsDepth is reached if ( commentsLevel < commentsDepth ) { const nextLevel = commentsLevel + 1; return [ { commentId: -( commentsLevel + 3 ), children: buildChildrenComment( nextLevel ), }, ]; } return []; }; // Add the first comment and its children const placeholderComments = [ { commentId: -1, children: buildChildrenComment( 1 ) }, ]; // Add a second comment unless the break comments setting is active and set to less than 2, and there is one nested comment max if ( ( ! pageComments || perPage! >= 2 ) && commentsDepth < 3 ) { placeholderComments.push( { commentId: -2, children: [], } ); } // Add a third comment unless the break comments setting is active and set to less than 3, and there aren't nested comments if ( ( ! pageComments || perPage! >= 3 ) && commentsDepth < 2 ) { placeholderComments.push( { commentId: -3, children: [], } ); } // In case that the value is set but larger than 3 we truncate it to 3. return placeholderComments; }; /** * Component which renders the inner blocks of the Comment Template. * * @param props Component props. * @param props.comment - A comment object. * @param props.activeCommentId - The ID of the comment that is currently active. * @param props.setActiveCommentId - The setter for activeCommentId. * @param props.firstCommentId - ID of the first comment in the array. * @param props.blocks - Array of blocks returned from * getBlocks() in parent . * @return Inner blocks of the Comment Template */ function CommentTemplateInnerBlocks( { comment, activeCommentId, setActiveCommentId, firstCommentId, blocks, }: CommentTemplateInnerBlocksProps ) { const { children, ...innerBlocksProps } = useInnerBlocksProps( {} ); return (
  • { comment.commentId === ( activeCommentId || firstCommentId ) ? children : null } { /* To avoid flicker when switching active block contexts, a preview is ALWAYS rendered and the preview for the active block is hidden. This ensures that when switching the active block, the component is not mounted again but rather it only toggles the `isHidden` prop. The same strategy is used for preventing the flicker in the Post Template block. */ } { comment?.children?.length! > 0 ? ( ) : null }
  • ); } const CommentTemplatePreview = ( { blocks, commentId, setActiveCommentId, isHidden, }: CommentTemplatePreviewProps ) => { const blockPreviewProps = useBlockPreview( { blocks, } ); const handleOnClick = () => { setActiveCommentId( commentId ); }; // We have to hide the preview block if the `comment` props points to // the currently active block! // Or, to put it differently, every preview block is visible unless it is the // currently active block - in this case we render its inner blocks. const style = { display: isHidden ? 'none' : undefined, }; return (
    ); }; const MemoizedCommentTemplatePreview = memo( CommentTemplatePreview ); /** * Component that renders a list of (nested) comments. It is called recursively. * * @param props Component props. * @param props.comments - Array of comment objects. * @param props.blockProps - Props from parent's `useBlockProps()`. * @param props.activeCommentId - The ID of the comment that is currently active. * @param props.setActiveCommentId - The setter for activeCommentId. * @param props.blocks - Array of blocks returned from getBlocks() in parent. * @param props.firstCommentId - The ID of the first comment in the array of * comment objects. * @return List of comments. */ const CommentsList = ( { comments, blockProps, activeCommentId, setActiveCommentId, blocks, firstCommentId, }: CommentsListProps ) => (
      { comments && comments.map( ( { commentId, ...comment }, index ) => ( ) ) }
    ); export default function CommentTemplateEdit( { clientId, context: { postId }, }: BlockEditProps< Record< string, unknown > > & { context: CommentTemplateBlockContext; } ) { const blockProps = useBlockProps(); const [ activeCommentId, setActiveCommentId ] = useState< number | undefined >(); const { commentOrder, threadCommentsDepth, threadComments, commentsPerPage, pageComments, } = useSelect( ( select ) => { const { getSettings } = select( blockEditorStore ); return ( ( getSettings().__experimentalDiscussionSettings as DiscussionSettings | undefined ) ?? {} ); }, [] ); const commentQuery = useCommentQueryArgs( { postId, } ); const { topLevelComments, blocks } = useSelect( ( select ) => { const { getEntityRecords } = select( coreStore ); const { getBlocks } = select( blockEditorStore ); return { // Request only top-level comments. Replies are embedded. topLevelComments: commentQuery ? getEntityRecords< EmbeddedComment >( 'root', 'comment', commentQuery ) : null, blocks: getBlocks( clientId ), }; }, [ clientId, commentQuery ] ); // Generate a tree structure of comment IDs. let commentTree = useCommentTree( // Reverse the order of top comments if needed. commentOrder === 'desc' && topLevelComments ? [ ...topLevelComments ].reverse() : topLevelComments ); if ( ! topLevelComments ) { return (

    ); } if ( ! postId ) { commentTree = getCommentsPlaceholder( { perPage: commentsPerPage, pageComments, threadComments, threadCommentsDepth, } ); } if ( ! commentTree!.length ) { return

    { __( 'No results found.' ) }

    ; } return ( ); }