Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline Commenting: Optimize store selector and misc changes #66592

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 32 additions & 50 deletions packages/editor/src/components/collab-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import { useSelect, useDispatch, resolveSelect } from '@wordpress/data';
import { useState, useEffect, useMemo } from '@wordpress/element';
import { useState, useMemo } from '@wordpress/element';
import { comment as commentIcon } from '@wordpress/icons';
import { addFilter } from '@wordpress/hooks';
import { store as noticesStore } from '@wordpress/notices';
Expand All @@ -22,7 +22,7 @@ import { store as editorStore } from '../../store';
import AddCommentButton from './comment-button';
import AddCommentToolbarButton from './comment-button-toolbar';

const threadsEmptyArray = [];
const EMPTY_ARRAY = [];
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This matches the name used in other places and makes it easier to search the code base.


const isBlockCommentExperimentEnabled =
window?.__experimentalEnableBlockComment;
Expand Down Expand Up @@ -54,50 +54,41 @@ export default function CollabSidebar() {
const { saveEntityRecord, deleteEntityRecord } = useDispatch( coreStore );
const { getEntityRecord } = resolveSelect( coreStore );
const { enableComplementaryArea } = useDispatch( interfaceStore );
const [ blockCommentID, setBlockCommentID ] = useState( null );
const [ showCommentBoard, setShowCommentBoard ] = useState( false );
const { postId } = useSelect( ( select ) => {

const { postId, postStatus, threads } = useSelect( ( select ) => {
const { getCurrentPostId, getEditedPostAttribute } =
select( editorStore );
const _postId = getCurrentPostId();
const data = !! _postId
? select( coreStore ).getEntityRecords( 'root', 'comment', {
post: _postId,
type: 'block_comment',
status: 'any',
per_page: 100,
} )
: null;

return {
postId: select( editorStore ).getCurrentPostId(),
postId: _postId,
postStatus: getEditedPostAttribute( 'status' ),
threads: data ?? EMPTY_ARRAY,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could also fall back to an array outside of the selector and we wouldn't need the constant.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave it for now. The component seems to change more rapidly now that the general concept has landed and we have the updated designs.

};
}, [] );

const postStatus = useSelect( ( select ) => {
const post = select( editorStore ).getCurrentPost();
return { postStatus: post?.status };
}, [] );

const threads = useSelect(
( select ) => {
if ( ! postId ) {
return threadsEmptyArray;
}
const { getEntityRecords } = select( coreStore );
const data = getEntityRecords( 'root', 'comment', {
post: postId,
type: 'block_comment',
status: 'any',
per_page: 100,
} );
return data || threadsEmptyArray;
},
[ postId ]
);
const { clientId, blockCommentId } = useSelect( ( select ) => {
const { getBlockAttributes, getSelectedBlockClientId } =
select( blockEditorStore );
const _clientId = getSelectedBlockClientId();

const clientId = useSelect( ( select ) => {
const { getSelectedBlockClientId } = select( blockEditorStore );
return getSelectedBlockClientId();
return {
clientId: _clientId,
blockCommentId: _clientId
? getBlockAttributes( _clientId )?.blockCommentId
: null,
};
}, [] );

const blockDetails = useSelect(
( select ) => {
return clientId
? select( blockEditorStore ).getBlock( clientId )
: null;
},
[ clientId ]
);

// Get the dispatch functions to save the comment and update the block attributes.
const { updateBlockAttributes } = useDispatch( blockEditorStore );

Expand Down Expand Up @@ -257,27 +248,18 @@ export default function CollabSidebar() {
);
};

useEffect( () => {
if ( blockDetails ) {
setBlockCommentID( blockDetails?.attributes.blockCommentId );
}
}, [ postId, clientId ] );

// Check if the experimental flag is enabled.
if (
! isBlockCommentExperimentEnabled ||
postStatus.postStatus === 'publish'
) {
if ( ! isBlockCommentExperimentEnabled || postStatus === 'publish' ) {
return null; // or maybe return some message indicating no threads are available.
}

return (
<>
{ ! blockCommentID && (
{ ! blockCommentId && (
<AddCommentButton onClick={ openCollabBoard } />
) }

{ blockCommentID > 0 && (
{ blockCommentId > 0 && (
<AddCommentToolbarButton onClick={ openCollabBoard } />
) }
<PluginSidebar
Expand Down
Loading