Skip to content

Commit

Permalink
Site Editor: Fix document actions label helper method (#52974)
Browse files Browse the repository at this point in the history
* Site Editor: Fix document actions label helper method
* Add missing useSelect dep
* Fix e2e tests
* Move the label map at the file level to avoid recreating the object on every render
  • Loading branch information
Mamaduka authored and tellthemachines committed Aug 31, 2023
1 parent cc90b61 commit 340df75
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { sprintf, __, isRTL } from '@wordpress/i18n';
import { __, isRTL } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import {
Button,
Expand All @@ -33,8 +33,18 @@ import { store as coreStore } from '@wordpress/core-data';
import useEditedEntityRecord from '../../use-edited-entity-record';
import { store as editSiteStore } from '../../../store';

const typeLabels = {
wp_block: __( 'Editing pattern:' ),
wp_navigation: __( 'Editing navigation menu:' ),
wp_template: __( 'Editing template:' ),
wp_template_part: __( 'Editing template part:' ),
};

export default function DocumentActions() {
const isPage = useSelect( ( select ) => select( editSiteStore ).isPage() );
const isPage = useSelect(
( select ) => select( editSiteStore ).isPage(),
[]
);
return isPage ? <PageDocumentActions /> : <TemplateDocumentActions />;
}

Expand Down Expand Up @@ -118,8 +128,6 @@ function TemplateDocumentActions( { className, onBack } ) {
);
}

const entityLabel = getEntityLabel( record.type );

let typeIcon = icon;
if ( record.type === 'wp_navigation' ) {
typeIcon = navigationIcon;
Expand All @@ -134,11 +142,7 @@ function TemplateDocumentActions( { className, onBack } ) {
onBack={ onBack }
>
<VisuallyHidden as="span">
{ sprintf(
/* translators: %s: the entity being edited, like "template"*/
__( 'Editing %s: ' ),
entityLabel
) }
{ typeLabels[ record.type ] ?? typeLabels.wp_template }
</VisuallyHidden>
{ getTitle() }
</BaseDocumentActions>
Expand Down Expand Up @@ -184,20 +188,3 @@ function BaseDocumentActions( { className, icon, children, onBack } ) {
</div>
);
}

function getEntityLabel( entityType ) {
let label = '';
switch ( entityType ) {
case 'wp_navigation':
label = 'navigation menu';
break;
case 'wp_template_part':
label = 'template part';
break;
default:
label = 'template';
break;
}

return label;
}
4 changes: 2 additions & 2 deletions test/e2e/specs/site-editor/title.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test.describe( 'Site editor title', () => {
'role=region[name="Editor top bar"i] >> role=heading[level=1]'
);

await expect( title ).toHaveText( 'Editing template: Index' );
await expect( title ).toHaveText( 'Editing template:Index' );
} );

test( 'displays the selected template name in the title for the header template', async ( {
Expand All @@ -43,6 +43,6 @@ test.describe( 'Site editor title', () => {
'role=region[name="Editor top bar"i] >> role=heading[level=1]'
);

await expect( title ).toHaveText( 'Editing template part: header' );
await expect( title ).toHaveText( 'Editing template part:header' );
} );
} );

0 comments on commit 340df75

Please sign in to comment.