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

Editor: Improve conditions for displaying new page assembler #68852

Merged
merged 2 commits into from
Jan 29, 2025
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
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();
Copy link
Member Author

Choose a reason for hiding this comment

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

The effect should react to this state. Check if the page is freshly created when the component is rendered.

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,
Copy link
Member Author

Choose a reason for hiding this comment

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

The postId ensures the effect re-runs when the user switches pages in the Site Editor or creates a new page via Command Palette.

enabled,
setIsInserterOpened,
isEditedPostDirty,
isEditedPostEmpty,
] );

return null;
}
Loading