Skip to content

Commit

Permalink
Updating feedback changes
Browse files Browse the repository at this point in the history
  • Loading branch information
karthick-murugan committed Dec 5, 2024
1 parent 13194c1 commit e9b0e4b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 24 deletions.
13 changes: 9 additions & 4 deletions packages/editor/src/components/collab-sidebar/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { useEntityBlockEditor } from '@wordpress/core-data';
import CommentAuthorInfo from './comment-author-info';
import CommentForm from './comment-form';
import { getBlockByCommentId } from './utils';
import { store as editorStore } from '../../store';

/**
* Renders the Comments component.
Expand All @@ -37,8 +38,6 @@ import { getBlockByCommentId } from './utils';
* @param {Function} props.onAddReply - The function to add a reply to a comment.
* @param {Function} props.onCommentDelete - The function to delete a comment.
* @param {Function} props.onCommentResolve - The function to mark a comment as resolved.
* @param {string} props.postType - The post type.
* @param {number} props.postId - The post ID.
* @return {React.ReactNode} The rendered Comments component.
*/
export function Comments( {
Expand All @@ -47,8 +46,6 @@ export function Comments( {
onAddReply,
onCommentDelete,
onCommentResolve,
postType, // Ensure postType is passed as a prop
postId, // Ensure postId is passed as a prop
} ) {
const [ actionState, setActionState ] = useState( false );
const [ isConfirmDialogOpen, setIsConfirmDialogOpen ] = useState( false );
Expand Down Expand Up @@ -78,6 +75,14 @@ export function Comments( {
);
}, [] );

const { postId, postType } = useSelect( ( select ) => {
const { getCurrentPostId, getCurrentPostType } = select( editorStore );
return {
postId: getCurrentPostId(),
postType: getCurrentPostType(),
};
}, [] );

const [ blocks ] = useEntityBlockEditor( 'postType', postType, {
id: postId,
} );
Expand Down
7 changes: 0 additions & 7 deletions packages/editor/src/components/collab-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,6 @@ function CollabSidebarContent( { showCommentBoard, setShowCommentBoard } ) {
);
};

const postType = useSelect(
( select ) => select( editorStore ).getCurrentPostType(),
[]
);

return (
<div className="editor-collab-sidebar-panel">
<AddComment
Expand All @@ -239,8 +234,6 @@ function CollabSidebarContent( { showCommentBoard, setShowCommentBoard } ) {
onAddReply={ addNewComment }
onCommentDelete={ onCommentDelete }
onCommentResolve={ onCommentResolve }
postId={ postId }
postType={ postType }
/>
</div>
);
Expand Down
65 changes: 52 additions & 13 deletions packages/editor/src/components/collab-sidebar/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,59 @@ export function sanitizeCommentString( str ) {
return str.trim();
}

/**
* Extracts comment IDs from an array of blocks.
*
* This function recursively traverses the blocks and their inner blocks to
* collect all comment IDs found in the block attributes.
*
* @param {Array} blocks - The array of blocks to extract comment IDs from.
* @return {Array} An array of comment IDs extracted from the blocks.
*/
export function getCommentIdsFromBlocks( blocks ) {
// Recursive function to extract comment IDs from blocks
const extractCommentIds = ( items ) => {
return items.reduce( ( commentIds, block ) => {
// Check for comment IDs in the current block's attributes
if (
block.attributes &&
block.attributes.blockCommentId &&
! commentIds.includes( block.attributes.blockCommentId )
) {
commentIds.push( block.attributes.blockCommentId );
}

// Recursively check inner blocks
if ( block.innerBlocks && block.innerBlocks.length > 0 ) {
const innerCommentIds = extractCommentIds( block.innerBlocks );
commentIds.push( ...innerCommentIds );
}

return commentIds;
}, [] );
};

// Extract all comment IDs recursively
return extractCommentIds( blocks );
}

export const getBlockByCommentId = ( blocks, commentId ) => {
for ( const block of blocks ) {
if ( block.attributes.blockCommentId === commentId ) {
return block;
}
if ( block.innerBlocks && block.innerBlocks.length > 0 ) {
const foundBlock = getBlockByCommentId(
block.innerBlocks,
commentId
);
if ( foundBlock ) {
return foundBlock;
const extractClientId = ( blocksArray, commentID ) => {
for ( const block of blocksArray ) {
if ( block.attributes.blockCommentId === commentID ) {
return block;
}
if ( block.innerBlocks && block.innerBlocks.length > 0 ) {
const foundBlock = extractClientId(
block.innerBlocks,
commentID
);
if ( foundBlock ) {
return foundBlock;
}
}
}
}
return blocks ? getBlockByCommentId( blocks, commentId ) : null;
};

return blocks ? extractClientId( blocks, commentId ) : null;
};

0 comments on commit e9b0e4b

Please sign in to comment.