Skip to content
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

Update/refactor url list #27559

Closed
wants to merge 21 commits into from
Closed

Update/refactor url list #27559

wants to merge 21 commits into from

Conversation

jhnstn
Copy link
Contributor

@jhnstn jhnstn commented Dec 7, 2020

Description

Part of #22890. Refactor the URLInput component to a functional component using hooks.

How has this been tested?

Manually tested to make sure URL input behavior is consistent with existing behavior using #24089 as a guide.

Screenshots

Types of changes

Refactoring to use hooks.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@jhnstn jhnstn requested a review from ellatrix as a code owner December 7, 2020 19:38
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Dec 7, 2020
@skorasaurus skorasaurus added the [Type] Code Quality Issues or PRs that relate to code quality label Dec 10, 2020
@jhnstn jhnstn marked this pull request as draft December 10, 2020 18:12
@youknowriad youknowriad requested a review from a team December 10, 2020 18:20
@jhnstn jhnstn marked this pull request as ready for review December 11, 2020 20:46
Also collect similar value state concerns
Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

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

Thanks for working on this! It's a pretty complex component :)

I have checked most of the functions but needs more checking in lifecycle components. Let's address some of the comments and review again.

packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
packages/block-editor/src/components/url-input/index.js Outdated Show resolved Hide resolved
@jhnstn jhnstn marked this pull request as draft December 17, 2020 03:51
@jhnstn
Copy link
Contributor Author

jhnstn commented Jan 12, 2021

hi @ntsekouras , thanks again for all the feedback. I got pulled away from this issue but I'm hoping to get back on it this week. I've pushed updates per your comments ( which were super helpful ) and I believe this is ready for a second look. I see a few e2e tests failing but they seem unrelated to these changes, I'd re-run to verify it's just a blinking test but I don't have access to do that yet. Thanks again.

@jhnstn jhnstn marked this pull request as ready for review January 12, 2021 21:01
! this.isUpdatingSuggestions &&
! ( suggestions && suggestions.length )
! isUpdatingSuggestions() &&
! suggestions.length
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
! suggestions.length
! suggestions?.length

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure this guard is needed. suggestions is initialized to an empty array in a useState call. There is one call to setSuggestions with the result from the suggestions fetch but there is an truthy check before it's called. I'll add an Array.isArray check on the result (length won't work here since we want to update suggestions if they are empty)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added Array check in d5fe5e8

Copy link
Contributor

@ntsekouras ntsekouras Jan 15, 2021

Choose a reason for hiding this comment

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

It's okay with the check you added, but actually Array.isArray catches null and undefined, so you need just this check without checking the plain result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The conditional is something like result && Array.isArray( result ) which should prevent the setSuggestions call with null or undefined. But I thought Array.isArray will return false for null or undefined ? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#using_array.isarray

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understood correctly but my comment was about result && Array.isArray( result ) being equivalent to Array.isArray(result)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah I see, I total misread your comment @ntsekouras , my bad.

Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

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

Great work here @jhnstn! Thanks for refactoring this quite complicated component. 💯

I have left some small notes but this feels to be in good place. By testing I noticed a different behavior with the master, which also feels like it should behave like it does here :) Other than that it LGTM.

The first part of the gif is from your branch and when I clear the search term it shows the initial results. In master as you can see it doesn't (second part of gif).

urlinput

It would be great for someone more experienced in class components and especially getDerivedStateFromProps part to take a look as well. @ellatrix if you have some time, I know you have done tons of such refactorings. This includes my two comments about wheter a useEffect should be used.

[]
);

if ( prevInstanceId !== instanceId ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be in a useEffect? 🤔

Copy link
Contributor Author

@jhnstn jhnstn Jan 14, 2021

Choose a reason for hiding this comment

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

Good question, tbh I'm not sure since this logic is from the existing getDerivedStateFromProps. I tried following back through the commit history to see why getDerivedStateFromProps was being used but nothing jumped out. Then after some research on how to refactor getDerivedStateFromProps to use hooks I noticed that folks are just comparing refs to state outside of useEffect.

componentWillUnmount() {
delete this.suggestionsRequest;
}
if ( showInitialSuggestionsChanged ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be in a useEffect as well? 🤔

@jhnstn
Copy link
Contributor Author

jhnstn commented Jan 14, 2021

Thanks again @ntsekouras !

The first part of the gif is from your branch and when I clear the search term it shows the initial results. In master as you can see it doesn't (second part of gif).

Interesting, I had scrutinized that behavior and I was fairly confident that I was seeing the same thing on master. Regardless I'll get those to match.

Base automatically changed from master to trunk March 1, 2021 15:44
@jhnstn
Copy link
Contributor Author

jhnstn commented Apr 5, 2021

Closing due to the changes going stale. Resolving the conflicts will likely take longer than starting a new PR

@jhnstn jhnstn closed this Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants