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

useBlockNameForPatterns: Refactor as a single useSelect call #67171

Merged
merged 1 commit into from
Nov 21, 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
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
Loading