Skip to content

Commit

Permalink
Refactor navigation title usage (#52807)
Browse files Browse the repository at this point in the history
#52758 fixes the response from the fallback-navigation endpoint that did not have title.raw in the response, meaning we had to sometimes rely on title.rendered. This removes the extra 'or' statements that were added as hot fixes. The ones that still rely on title.rendered are due to the value needing to be checked within an object, or somewhere that the title property is not rendered via react.
  • Loading branch information
jeryj authored Jul 28, 2023
1 parent 7b1c44c commit 44fb9cd
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const MainContent = ( {
? sprintf(
/* translators: %s: The name of a menu. */
__( 'Structure for navigation menu: %s' ),
navigationMenu?.title?.rendered || __( 'Untitled menu' )
navigationMenu?.title || __( 'Untitled menu' )
)
: __(
'You have not yet created any menus. Displaying a list of your Pages'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ import useNavigationMenu from '../use-navigation-menu';
import useNavigationEntities from '../use-navigation-entities';

function buildMenuLabel( title, id, status ) {
if ( ! title?.rendered ) {
if ( ! title ) {
/* translators: %s is the index of the menu in the list of menus. */
return sprintf( __( '(no title %s)' ), id );
}

if ( status === 'publish' ) {
return decodeEntities( title?.rendered );
return decodeEntities( title );
}

return sprintf(
// translators: %1s: title of the menu; %2s: status of the menu (draft, pending, etc.).
__( '%1$s (%2$s)' ),
decodeEntities( title?.rendered ),
decodeEntities( title ),
status
);
}
Expand Down Expand Up @@ -72,7 +72,11 @@ function NavigationMenuSelector( {
const menuChoices = useMemo( () => {
return (
navigationMenus?.map( ( { id, title, status }, index ) => {
const label = buildMenuLabel( title, index + 1, status );
const label = buildMenuLabel(
title?.rendered,
index + 1,
status
);

return {
value: id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ function useDuplicateNavigationMenu() {
useDispatch( noticesStore );

const handleDuplicate = async ( navigationMenu ) => {
const menuTitle =
navigationMenu?.title?.rendered || navigationMenu?.slug;
const menuTitle = navigationMenu?.title || navigationMenu?.slug;

try {
const savedRecord = await saveEntityRecord(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ import { unlock } from '../../lock-unlock';

// Copied from packages/block-library/src/navigation/edit/navigation-menu-selector.js.
function buildMenuLabel( title, id, status ) {
if ( ! title?.rendered ) {
if ( ! title ) {
/* translators: %s is the index of the menu in the list of menus. */
return sprintf( __( '(no title %s)' ), id );
}

if ( status === 'publish' ) {
return decodeEntities( title?.rendered );
return decodeEntities( title );
}

return sprintf(
// translators: %1s: title of the menu; %2s: status of the menu (draft, pending, etc.).
__( '%1$s (%2$s)' ),
decodeEntities( title?.rendered ),
decodeEntities( title ),
status
);
}
Expand Down Expand Up @@ -124,7 +124,7 @@ export default function SidebarNavigationScreenNavigationMenus() {
withChevron
icon={ navigation }
>
{ buildMenuLabel( title, index + 1, status ) }
{ buildMenuLabel( title?.rendered, index + 1, status ) }
</NavMenuItem>
) ) }
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function TemplatePartNavigationMenu( { id } ) {
upperCase={ true }
weight={ 500 }
>
{ title?.rendered || title || __( 'Navigation' ) }
{ title || __( 'Navigation' ) }
</Heading>
<NavigationMenuEditor navigationMenuId={ id } />
</>
Expand Down

1 comment on commit 44fb9cd

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 44fb9cd.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5693969852
📝 Reported issues:

Please sign in to comment.