-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 = []; | ||
|
||
const isBlockCommentExperimentEnabled = | ||
window?.__experimentalEnableBlockComment; | ||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
|
||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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.