Skip to content

Commit

Permalink
useBlockNameForPatterns: Refactor as a single useSelect call (#67171)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf authored Nov 21, 2024
1 parent b7d989e commit afb05e8
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions packages/block-library/src/query/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,32 +272,31 @@ export const getTransformedBlocksFromPattern = (
* @return {string} The block name to be used in the patterns suggestions.
*/
export function useBlockNameForPatterns( clientId, attributes ) {
const activeVariationName = useSelect(
( select ) =>
select( blocksStore ).getActiveBlockVariation(
'core/query',
attributes
)?.name,
[ attributes ]
);
const blockName = `core/query/${ activeVariationName }`;
const hasActiveVariationPatterns = useSelect(
return useSelect(
( select ) => {
const activeVariationName = select(
blocksStore
).getActiveBlockVariation( 'core/query', attributes )?.name;

if ( ! activeVariationName ) {
return false;
return 'core/query';
}

const { getBlockRootClientId, getPatternsByBlockTypes } =
select( blockEditorStore );

const rootClientId = getBlockRootClientId( clientId );
const activePatterns = getPatternsByBlockTypes(
blockName,
`core/query/${ activeVariationName }`,
rootClientId
);
return activePatterns.length > 0;

return activePatterns.length > 0
? `core/query/${ activeVariationName }`
: 'core/query';
},
[ clientId, activeVariationName, blockName ]
[ clientId, attributes ]
);
return hasActiveVariationPatterns ? blockName : 'core/query';
}

/**
Expand Down

0 comments on commit afb05e8

Please sign in to comment.