-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Edit Site: Fix useEditedEntityRecord()
loading state
#50730
Conversation
Size Change: +12 B (0%) Total Size: 1.39 MB
ℹ️ View Unchanged
|
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.
Good catch, @tyxla!
The fix works as expected ✅
I left a minor comment, but feel free to ignore it; there's nothing wrong with the current method.
packages/edit-site/src/components/use-edited-entity-record/index.js
Outdated
Show resolved
Hide resolved
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.
Thanks, I've addressed your feedback, @Mamaduka, could you take one last look before we 🚀 ?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, of course, good catch! Added in d032f4d
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 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
Thanks for the follow-ups, @tyxla!
Everything works as expected!
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
null
title 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!! postID
check to verify entity record has loaded, and the post ID for a template part is always a string (liketwentytwentytwo//header
for example). That way, it's always consideredtrue
, and when passing thenull
title to theuseTitle()
hook, it is converted to anull
string.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