Skip to content

Commit

Permalink
Editor: Improve conditions for displaying new page assembler (WordPre…
Browse files Browse the repository at this point in the history
…ss#68852)

* Editor: Improve conditions for displaying new page assembler
* Add note as inline comment

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: carolinan <[email protected]>
  • Loading branch information
3 people authored Jan 29, 2025
1 parent 73b8e2c commit 35ba7ef
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions packages/editor/src/components/start-page-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ import { store as interfaceStore } from '@wordpress/interface';
import { store as editorStore } from '../../store';

export default function StartPageOptions() {
const { postId, shouldEnable } = useSelect( ( select ) => {
const {
isEditedPostDirty,
isEditedPostEmpty,
getCurrentPostId,
getCurrentPostType,
} = select( editorStore );
const { postId, enabled } = useSelect( ( select ) => {
const { getCurrentPostId, getCurrentPostType } = select( editorStore );
const preferencesModalActive =
select( interfaceStore ).isModalActive( 'editor/preferences' );
const choosePatternModalEnabled = select( preferencesStore ).get(
Expand All @@ -27,24 +22,37 @@ export default function StartPageOptions() {
);
return {
postId: getCurrentPostId(),
shouldEnable:
enabled:
choosePatternModalEnabled &&
! preferencesModalActive &&
! isEditedPostDirty() &&
isEditedPostEmpty() &&
'page' === getCurrentPostType(),
};
}, [] );
const { isEditedPostDirty, isEditedPostEmpty } = useSelect( editorStore );
const { setIsInserterOpened } = useDispatch( editorStore );

useEffect( () => {
if ( shouldEnable ) {
if ( ! enabled ) {
return;
}

const isFreshPage = ! isEditedPostDirty() && isEditedPostEmpty();
if ( isFreshPage ) {
setIsInserterOpened( {
tab: 'patterns',
category: 'core/starter-content',
} );
}
}, [ postId, shouldEnable, setIsInserterOpened ] );

// Note: The `postId` ensures the effect re-runs when pages are switched without remounting the component.
// Examples: changing pages in the List View, creating a new page via Command Palette.
}, [
postId,
enabled,
setIsInserterOpened,
isEditedPostDirty,
isEditedPostEmpty,
] );

return null;
}

0 comments on commit 35ba7ef

Please sign in to comment.