-
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
MediaReplaceFlow: Fix UI showing stale URL by avoiding state duplication #42116
Conversation
Size Change: +695 B (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
useEffect( () => { | ||
if ( mediaURL && mediaURL !== mediaURLValue ) { | ||
setMediaURLValue( mediaURL ); | ||
} | ||
}, [ mediaURL ] ); |
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.
The new React docs recommend adjusting the state while rendering for similar cases. But I think this might be easier to understand at a glance.
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 reference! I don't think this was in the recommended practices a couple of years ago, but it makes a lot of sense nowadays, especially in the context of concurrent rendering. :)
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.
LGTM. Thanks, @mcsf!
@Mamaduka: I was going to give your feedback a go, but then I decided to have a quick look at how easily one could just remove the internal state. Well, I have some hope that a7a776a is actually enough. I've manually tested it with block types Image, File, Gallery, Video, and Cover (FYI, blocks like File and Cover don't even show the URL in the popover, because they don't use MediaReplaceFlow's Let's see if the automated tests raise any flags. In the meantime, a sanity check would be welcome. :) Edit: in 31a0893 I updated a unit test for the component to reflect the fact that there needs to be a wrapper component that keeps the state of |
Thanks for the follow-up, @mcsf! This tests well for me 👍 |
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.
Tested this and works as described. Thanks @mcsf 👏🏻
Thank you both! |
What?
Fixes a UI bug discovered in #41994.
Why?
After a new Image block has uploaded an image to the media library, the "Replace" popover will incorrectly report the image's URL as
blob:...
, even though the block has correctly updated itsurl
attribute to the new resource in the media library.How?
The
MediaReplaceFlow
component is passedmediaURL
as a prop, but it also keeps its own internal URL state (mediaURLValue
) usingmediaURL
as a seed. The reason for keeping internal state is to let users edit the URL in an "edit and confirm" kind of interaction, as initially implemented in #16200 (see line), but that lends itself to synchronisation bugs. This PR removes the component's internal state altogether and uses themediaURL
prop directly. The actual link-editing functionality is provided byLinkControl
(a descendant ofMediaReplaceFlow
), which already does it own state synchronisation anyway.Testing Instructions
See original report in #41994 (comment).
blob:
URL.