Edit Site: Fix useEditedEntityRecord() loading state#50730
Conversation
|
Size Change: +12 B (0%) Total Size: 1.39 MB
ℹ️ View Unchanged
|
packages/edit-site/src/components/use-edited-entity-record/index.js
Outdated
Show resolved
Hide resolved
packages/edit-site/src/components/use-edited-entity-record/index.js
Outdated
Show resolved
Hide resolved
| const _isLoaded = hasFinishedResolution( 'getEditedEntityRecord', [ | ||
| 'postType', | ||
| usedPostType, | ||
| usedPostId, | ||
| ] ); |
There was a problem hiding this comment.
We should probably check if usedPostId is defined. Otherwise, you'll see the null flash in the title when visiting wp-admin/site-editor.php, because the editor is resolving the edited template from the server, and we get a false positive.
const _isLoaded =
usedPostId &&
hasFinishedResolution( 'getEditedEntityRecord', [
'postType',
usedPostType,
usedPostId,
] );There was a problem hiding this comment.
I don't understand why the extra usedPostId && ... check was needed here. If usedPostId is null, the hasFinishedSelector returns true for the null value?
There was a problem hiding this comment.
I'd say the idea is to not call the hasFinishedResolution selector at all if the post ID is null since we already know the result.
There was a problem hiding this comment.
There's a point where both selectors use undefined as postId. The getEditedEntityRecord will resolve and return an empty object, and hasFinishedResolution will report that our template was loaded.
Example:
const { testRecord, testResolved } = useSelect( ( select ) => {
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
return {
testRecord: getEditedEntityRecord(
'postType',
'wp_template',
undefined
),
testResolved: hasFinishedResolution( 'getEditedEntityRecord', [
'postType',
'wp_template',
undefined,
] ),
};
} );
console.log( { testRecord, testResolved } )There was a problem hiding this comment.
Oh, now I see what's happening. A getEntityRecord( 'postType', 'wp_template', undefined ) call doesn't make sense. It will do a lookup like return items[ key ], but there is never a template with id === undefined, so it will always return undefined.
But it still calls the resolver, where undefined key does make sense. Instead of fetching a specific template from /templates/{key}, it will fetch all templates from /templates and store the list.
That's why we end up in a situation where getEntityRecord returns undefined (there's no item with undefined key) and hasFinishedResolution returns true (the list is loaded).
If usedPostId is not known, the hook shouldn't call any selector at all and return an empty-ish object right away.
|
Thank you for your help, @Mamaduka 🙌 |
…dd/static-closures * 'trunk' of https://github.com/WordPress/gutenberg: (26 commits) Add transparent outline to input control BackdropUI focus style. (#50772) Added wrapper element for RichText in File block (#50607) Remove the experimental flag of the command center (#50781) Update the document title in the site editor to open the command center (#50369) Remove `unwrap` from transforms and add `ungroup` to more blocks (#50385) Add new experimental version of DropdownMenu (#49473) Force display of in custom css input boxes to LTR (#50768) Polish experimental navigation block (#50670) Support negation operator in selectors in the Interactivity API (#50732) Minor updates to theme.json schema pages (#50742) $revisions_controller is not used. Let's delete it. (#50763) Remove OffCanvasEditor (#50705) Mobile - E2E test - Update code to use the new navigateUp helper (#50736) Try: Smaller external link icon (#50728) Block Editor: Remove unused 'useIsDimensionsSupportValid' method (#50735) Fix flaky media inserter drag-and-dropping e2e test (#50740) docs: Fix change log typo (#50737) Edit Site: Fix `useEditedEntityRecord()` loading state (#50730) Fix labelling, description, and focus style of the block transform to pattern previews (#50577) Fix Global Styles sidebar block selection on zoom out mode (#50708) ...
What?
This PR fixes the
nulltitle that appears when fresh loading a template part.Why?
While working on testing site editor loading experience in various areas, I discovered that on a fresh load of a template part, the title of the document shortly appears as "null > Template Part > {SITE NAME}".
How?
It seems that the loaded state (
isLoaded) of theuseEditedEntityRecord()hook doesn't work correctly for template parts. The reason is that we're currently relying on a!! postIDcheck to verify entity record has loaded, and the post ID for a template part is always a string (liketwentytwentytwo//headerfor example). That way, it's always consideredtrue, and when passing thenulltitle to theuseTitle()hook, it is converted to anullstring.Since this is leading to a bigger issue, where the loading experience could be broken in other places due to it always being considered loaded, we're fixing the loaded state by having it check if the entity record is not empty.
Testing Instructions
Testing Instructions for Keyboard
None
Screenshots or screencast
Demo of the issue (watch the last 5-6 seconds):
Screen.Recording.2023-05-18.at.12.37.22.mov