From b6229a4727192f4dbe4bb952cacf9448e99d50b7 Mon Sep 17 00:00:00 2001 From: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Date: Sat, 21 Sep 2024 05:04:37 +0900 Subject: [PATCH] Command Palette: Fix "Add new page" command for hybrid theme (#65534) Co-authored-by: t-hamano Co-authored-by: ramonjd --- .../src/admin-navigation-commands.js | 79 +++++++++++-------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/packages/core-commands/src/admin-navigation-commands.js b/packages/core-commands/src/admin-navigation-commands.js index be5e19386b65b..c0d8bb084b46a 100644 --- a/packages/core-commands/src/admin-navigation-commands.js +++ b/packages/core-commands/src/admin-navigation-commands.js @@ -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'; @@ -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', @@ -71,7 +80,7 @@ function useAddNewPageCommand() { callback: addNewPage, }, ]; - }, [ createPageEntity, isSiteEditor ] ); + }, [ createPageEntity, isSiteEditor, isBlockBasedTheme ] ); return { isLoading: false,