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

Patterns: Don't override the rootClientID in create menu - only set if undefined #52713

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';
import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
Expand All @@ -10,7 +10,11 @@ import { store as blockEditorStore } from '@wordpress/block-editor';
import ReusableBlockConvertButton from './reusable-block-convert-button';
import ReusableBlocksManageButton from './reusable-blocks-manage-button';

function ReusableBlocksMenuItems( { clientIds, rootClientId } ) {
export default function ReusableBlocksMenuItems( { rootClientId } ) {
const clientIds = useSelect( ( select ) =>
select( blockEditorStore ).getSelectedBlockClientIds()
);
Copy link
Member

Choose a reason for hiding this comment

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

Should pass [] as the deps for useSelect here. Annoyingly it doesn't default to [].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, fixed, we should have a lint rule for that.


return (
<>
<ReusableBlockConvertButton
Expand All @@ -23,16 +27,3 @@ function ReusableBlocksMenuItems( { clientIds, rootClientId } ) {
</>
);
}

export default withSelect( ( select ) => {
const { getSelectedBlockClientIds, getBlockRootClientId } =
select( blockEditorStore );
const clientIds = getSelectedBlockClientIds();
return {
clientIds,
rootClientId:
clientIds?.length > 0
? getBlockRootClientId( clientIds[ 0 ] )
: undefined,
};
} )( ReusableBlocksMenuItems );
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ export default function ReusableBlockConvertButton( {
const canConvert = useSelect(
( select ) => {
const { canUser } = select( coreStore );
const { getBlocksByClientId, canInsertBlockType } =
select( blockEditorStore );
const {
getBlocksByClientId,
canInsertBlockType,
getBlockRootClientId,
} = select( blockEditorStore );

const rootId =
rootClientId || clientIds.length > 0
? getBlockRootClientId( clientIds[ 0 ] )
: undefined;
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong, no? It will call getBlockRootClientId if rootClientId is truthy. I think you need parens around the right hand side of the ||.

const rootId = rootClientId || ( clientIds.length > 0 ? getBlockRootClientId( clientIds[ 0 ] ) : undefined );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤦 should have stuck with the nested ternary I first had, it was easier to grok 😄 It works as expected when properly bracketed and have updated, but I wonder if something like the following is clearer for our future selves and not much more verbose 🤔 :

let rootId;
if ( rootClientId ) {
   rootId = rootClientId;
} else if ( clientIds.length > 0 ) {
   rootId = getBlockRootClientId( clientIds[ 0 ] );
}

Copy link
Member

Choose a reason for hiding this comment

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

Up to you 😀


const blocks = getBlocksByClientId( clientIds ) ?? [];

Expand All @@ -70,7 +78,7 @@ export default function ReusableBlockConvertButton( {
// Hide when this is already a reusable block.
! isReusable &&
// Hide when reusable blocks are disabled.
canInsertBlockType( 'core/block', rootClientId ) &&
canInsertBlockType( 'core/block', rootId ) &&
blocks.every(
( block ) =>
// Guard against the case where a regular block has *just* been converted.
Expand Down