Skip to content

Commit

Permalink
Fix filtering for downloadable blocks in inserter. (#55243)
Browse files Browse the repository at this point in the history
* Fix filering for downloadable blocks in inserter.

* Don't remove block from list if just installed
  • Loading branch information
tellthemachines authored Oct 13, 2023
1 parent 4d823a3 commit 68a33b5
Showing 1 changed file with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import { __ } from '@wordpress/i18n';
import { Spinner } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as coreStore } from '@wordpress/core-data';
import { withSelect } from '@wordpress/data';
import { getBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -72,10 +72,12 @@ function DownloadableBlocksPanel( {
}

export default compose( [
withSelect( ( select, { filterValue, rootClientId = null } ) => {
const { getDownloadableBlocks, isRequestingDownloadableBlocks } =
select( blockDirectoryStore );
const { canInsertBlockType } = select( blockEditorStore );
withSelect( ( select, { filterValue } ) => {
const {
getDownloadableBlocks,
isRequestingDownloadableBlocks,
getInstalledBlockTypes,
} = select( blockDirectoryStore );

const hasPermission = select( coreStore ).canUser(
'read',
Expand All @@ -84,9 +86,19 @@ export default compose( [

function getInstallableBlocks( term ) {
const downloadableBlocks = getDownloadableBlocks( term );
const installableBlocks = downloadableBlocks.filter( ( block ) =>
canInsertBlockType( block, rootClientId, true )
);
const installedBlockTypes = getInstalledBlockTypes();
// Filter out blocks that are already installed.
const installableBlocks = downloadableBlocks.filter( ( block ) => {
// Check if the block has just been installed, in which case it
// should still show in the list to avoid suddenly disappearing.
// `installedBlockTypes` only returns blocks stored in state
// immediately after installation, not all installed blocks.
const isJustInstalled = !! installedBlockTypes.find(
( blockType ) => blockType.name === block.name
);
const isPreviouslyInstalled = getBlockType( block.name );
return isJustInstalled || ! isPreviouslyInstalled;
} );

if ( downloadableBlocks.length === installableBlocks.length ) {
return downloadableBlocks;
Expand Down

1 comment on commit 68a33b5

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 68a33b5.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6503476848
📝 Reported issues:

Please sign in to comment.