Skip to content

Commit

Permalink
Feedback Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
karthick-murugan committed Nov 28, 2024
1 parent d71c1c6 commit 07c85f6
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions packages/editor/src/components/collab-sidebar/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { useState, RawHTML } from '@wordpress/element';
import { useState, RawHTML, useEffect } from '@wordpress/element';
import {
__experimentalHStack as HStack,
__experimentalVStack as VStack,
Expand All @@ -17,7 +17,7 @@ import {
} from '@wordpress/components';
import { Icon, check, published, moreVertical } from '@wordpress/icons';
import { __, _x } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
Expand Down Expand Up @@ -46,6 +46,8 @@ export function Comments( {
} ) {
const [ actionState, setActionState ] = useState( false );
const [ isConfirmDialogOpen, setIsConfirmDialogOpen ] = useState( false );
const [ activeClientId, setActiveClientId ] = useState( null );
const [ blocksList, setBlocksList ] = useState( null );

const handleConfirmDelete = () => {
onCommentDelete( actionState.id );
Expand All @@ -72,6 +74,59 @@ export function Comments( {
);
}, [] );

const { selectedClientBlocks, selectedActiveClientId } = useSelect(
( select ) => {
const clientID =
select( blockEditorStore ).getSelectedBlockClientId();
const selClientBlocks = select( blockEditorStore ).getBlocks();

const getBlockCommentId =
select( blockEditorStore ).getBlock( clientID )?.attributes
?.blockCommentId ?? false;

return {
selectedClientBlocks: selClientBlocks,
selectedActiveClientId: getBlockCommentId || null,
};
},
[]
);

useEffect( () => {
setBlocksList( selectedClientBlocks );

if ( selectedActiveClientId ) {
setActiveClientId( selectedActiveClientId );
}
}, [ selectedClientBlocks, selectedActiveClientId ] );

const findBlockByCommentId = ( blocks, commentId ) => {
for ( const block of blocks ) {
if ( block.attributes.blockCommentId === commentId ) {
return block;
}
if ( block.innerBlocks && block.innerBlocks.length > 0 ) {
const foundBlock = findBlockByCommentId(
block.innerBlocks,
commentId
);
if ( foundBlock ) {
return foundBlock;
}
}
}
return null;
};

const { selectBlock } = useDispatch( blockEditorStore );
const handleThreadClick = ( thread ) => {
const block = findBlockByCommentId( blocksList, thread.id );
console.log( 'block', block );
if ( block ) {
selectBlock( block.clientId ); // Use the action to select the block
}
};

const CommentBoard = ( { thread, parentThread } ) => {
return (
<>
Expand Down Expand Up @@ -202,6 +257,7 @@ export function Comments( {
) }
id={ thread.id }
spacing="3"
onClick={ () => handleThreadClick( thread ) }
>
<CommentBoard thread={ thread } />
{ 0 < thread?.reply?.length &&
Expand Down

0 comments on commit 07c85f6

Please sign in to comment.