Skip to content

Commit

Permalink
Fix for empty starter patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored and scruffian committed May 10, 2024
1 parent 8dc3953 commit 132a00d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@ function useStartPatterns() {
// A pattern is a start pattern if it includes 'core/post-content' in its blockTypes,
// and it has no postTypes declared and the current post type is page or if
// the current post type is part of the postTypes declared.
const { blockPatternsWithPostContentBlockType } = useSelect( ( select ) => {
const { getPatternsByBlockTypes } = select( blockEditorStore );
return {
blockPatternsWithPostContentBlockType:
getPatternsByBlockTypes( 'core/post-content' ),
};
}, [] );

return blockPatternsWithPostContentBlockType;
return useSelect( ( select ) =>
select( blockEditorStore ).getPatternsByBlockTypes(
'core/post-content'
)
);
}

/**
Expand Down
23 changes: 19 additions & 4 deletions packages/editor/src/components/start-page-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useRegistry } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';

function StartPageOptionsModal() {
const { setIsInserterOpened } = useDispatch( editorStore );
const registry = useRegistry();
// A pattern is a start pattern if it includes 'core/post-content' in its
// blockTypes, and it has no postTypes declared and the current post type is
// page or if the current post type is part of the postTypes declared.
const hasStarterPatterns = useSelect(
( select ) =>
!! select( blockEditorStore ).getPatternsByBlockTypes(
'core/post-content'
).length
);
useEffect( () => {
setIsInserterOpened( { tab: 'patterns', category: 'core/content' } );
}, [ setIsInserterOpened ] );
if ( hasStarterPatterns ) {
registry.dispatch( editorStore ).setIsInserterOpened( {
tab: 'patterns',
category: 'core/content',
} );
}
}, [ hasStarterPatterns, registry ] );
return null;
}

Expand Down

0 comments on commit 132a00d

Please sign in to comment.