Skip to content

Commit

Permalink
Command Palette: Fix "Add new page" command for hybrid theme (WordPre…
Browse files Browse the repository at this point in the history
…ss#65534)

Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
  • Loading branch information
3 people committed Sep 20, 2024
1 parent b1d9255 commit 219065c
Showing 1 changed file with 44 additions and 35 deletions.
79 changes: 44 additions & 35 deletions packages/core-commands/src/admin-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { __ } from '@wordpress/i18n';
import { plus } from '@wordpress/icons';
import { getPath } from '@wordpress/url';
import { store as coreStore } from '@wordpress/core-data';
import { useDispatch } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import { useCallback, useMemo } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';
import { privateApis as routerPrivateApis } from '@wordpress/router';

Expand All @@ -23,46 +23,55 @@ function useAddNewPageCommand() {
'site-editor.php'
);
const history = useHistory();
const isBlockBasedTheme = useSelect( ( select ) => {
return select( coreStore ).getCurrentTheme()?.is_block_theme;
}, [] );
const { saveEntityRecord } = useDispatch( coreStore );
const { createErrorNotice } = useDispatch( noticesStore );

const createPageEntity = async ( { close } ) => {
try {
const page = await saveEntityRecord(
'postType',
'page',
{
status: 'draft',
},
{
throwOnError: true,
const createPageEntity = useCallback(
async ( { close } ) => {
try {
const page = await saveEntityRecord(
'postType',
'page',
{
status: 'draft',
},
{
throwOnError: true,
}
);
if ( page?.id ) {
history.push( {
postId: page.id,
postType: 'page',
canvas: 'edit',
} );
}
);
if ( page?.id ) {
history.push( {
postId: page.id,
postType: 'page',
canvas: 'edit',
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while creating the item.' );

createErrorNotice( errorMessage, {
type: 'snackbar',
} );
} finally {
close();
}
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while creating the item.' );

createErrorNotice( errorMessage, {
type: 'snackbar',
} );
} finally {
close();
}
};
},
[ createErrorNotice, history, saveEntityRecord ]
);

const commands = useMemo( () => {
const addNewPage = isSiteEditor
? createPageEntity
: () => ( document.location.href = 'post-new.php?post_type=page' );
const addNewPage =
isSiteEditor && isBlockBasedTheme
? createPageEntity
: () =>
( document.location.href =
'post-new.php?post_type=page' );
return [
{
name: 'core/add-new-page',
Expand All @@ -71,7 +80,7 @@ function useAddNewPageCommand() {
callback: addNewPage,
},
];
}, [ createPageEntity, isSiteEditor ] );
}, [ createPageEntity, isSiteEditor, isBlockBasedTheme ] );

return {
isLoading: false,
Expand Down

0 comments on commit 219065c

Please sign in to comment.