Skip to content

Commit

Permalink
move login in useSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Dec 17, 2020
1 parent c500f47 commit cd60bbd
Showing 1 changed file with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,34 @@ import { store as blocksStore } from '@wordpress/blocks';

// TODO write tests
export default function useBlockDisplayInformation( clientId ) {
const { attributes, blockType, variations } = useSelect(
return useSelect(
( select ) => {
if ( ! clientId ) return {};
if ( ! clientId ) return null;
const { getBlockName, getBlockAttributes } = select(
'core/block-editor'
);
const { getBlockType, getBlockVariations } = select( blocksStore );
const blockName = getBlockName( clientId );
const blockType = getBlockType( blockName );
if ( ! blockType ) return null;
const variations = getBlockVariations( blockName );
const blockTypeInfo = {
title: blockType.title,
icon: blockType.icon,
description: blockType.description,
};
if ( ! variations ) return blockTypeInfo;
const attributes = getBlockAttributes( clientId );
const match = variations.find( ( variation ) =>
variation.isActive?.( attributes, variation.attributes )
);
if ( ! match ) return blockTypeInfo;
return {
name: getBlockName( clientId ),
attributes: getBlockAttributes( clientId ),
blockType: getBlockType( blockName ),
variations: getBlockVariations( blockName ),
title: match.title || blockType.title,
icon: match.icon || blockType.icon,
description: match.description || blockType.description,
};
},
[ clientId ]
);
if ( ! blockType ) return null;

const blockTypeInfo = {
title: blockType.title,
icon: blockType.icon,
description: blockType.description,
};
if ( ! variations ) return blockTypeInfo;
const match = variations.find( ( variation ) =>
variation.isActive?.( attributes, variation.attributes )
);
if ( ! match ) return blockTypeInfo;
return {
title: match.title || blockType.title,
icon: match.icon || blockType.icon,
description: match.description || blockType.description,
};
}

0 comments on commit cd60bbd

Please sign in to comment.