-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Navigation: Use getTitle to get the navigation title #52635
Conversation
Size Change: +76 B (0%) Total Size: 1.43 MB
ℹ️ View Unchanged
|
|
||
export default function TemplatePartNavigationMenuListItem( { id } ) { | ||
const [ title ] = useEntityProp( 'postType', 'wp_navigation', 'title', id ); | ||
const { getTitle } = useEditedEntityRecord( 'wp_navigation', id ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a good solution for now.
I wonder if in the future we should have a selector for getEntityTitle( id )
which does this? It's so common and it causes so many bugs all the time.
I think you should propose it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't do anything different internally. See https://github.com/WordPress/gutenberg/blob/trunk/packages/edit-site/src/components/use-edited-entity-record/index.js#L59.
Plus useEditedEntityRecord
performs extra checks that aren't needed here.
I think we just need to make sure entities return values like titles in a predictable way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh really. Got to be honest I was doing a fly by review. My comment probably wasn't super helpful.
My thought was we need a single source of truth for getting a string-based title. I think the raw
vs rendered
thing throws people. Maybe if we had some well-named utilities that got the correct title based on where you want to output it that night help.
getEntityTitleForField
vs getEntityTitle
or something. Just an idea...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated this to just use getEditedEntityRecord
. I'm not sure what the best way to do this is, but right now we have a JS error in trunk and this branch fixes it, so I'd rather get this merged and iterate on improvements later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ll be able to do another round of reviews tomorrow morning.
Can you post more details on how to reproduce issue issue you’re having on trunk?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't get it to happen since #52707 merged. Still investigating...
...-site/src/components/sidebar-navigation-screen-pattern/template-part-navigation-menu-list.js
Show resolved
Hide resolved
7dc6aec
to
ec1d0f7
Compare
...-site/src/components/sidebar-navigation-screen-pattern/template-part-navigation-menu-list.js
Outdated
Show resolved
Hide resolved
…ttern/template-part-navigation-menu-list.js
...-site/src/components/sidebar-navigation-screen-pattern/template-part-navigation-menu-list.js
Outdated
Show resolved
Hide resolved
…ttern/template-part-navigation-menu-list.js
const title = useSelect( ( select ) => { | ||
const { getEditedEntityRecord } = select( coreStore ); | ||
const { __experimentalGetTemplateInfo: getTemplateInfo } = | ||
select( editorStore ); | ||
|
||
const _record = getEditedEntityRecord( | ||
'postType', | ||
'wp_navigation', | ||
id | ||
); | ||
|
||
const templateInfo = getTemplateInfo( _record ); | ||
return templateInfo?.title || __( '(no title)' ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just use record?.title?.rendered || record?.title
here? We can leave no title
fallback outside of the mapSelect
.
Using the selector getTemplateInfo
meant for different entities can have unexpected results in the future.
P.S. The useSelect
is missing a dependency here.
@@ -10,7 +10,7 @@ import TemplatePartNavigationMenuListItem from './template-part-navigation-menu- | |||
export default function TemplatePartNavigationMenuList( { menus } ) { | |||
return ( | |||
<ItemGroup className="edit-site-sidebar-navigation-screen-template-part-navigation-menu-list"> | |||
{ menus.map( ( menuId ) => ( | |||
{ [ ...new Set( menus ) ].map( ( menuId ) => ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change related to the title rendering fix?
If we need to ensure that menus
are a unique set, then this probably has to be done higher in the tree, maybe in useNavigationMenuContent
or getBlocksOfTypeFromBlocks
?
Edit: I just saw the #52707. I think this can be removed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I don't see a new for Set
here unless I missed something...?
There's a proper fix here: #52758 |
The issue this was solving is closed by #52758 |
What?
Gets the entity title in a more predictable format. An alternative to #52167.
Why?
Sometimes it seems like
useEntityProp
returns an object and sometimes a string, so sometimes this throws an error.How?
This changes the approach to use
getTitle
which is more predictable.Testing Instructions