wagtail-deprecated / react-streamfield

Powerful field for inserting multiple blocks with nesting. (NO LONGER MAINTAINED - See Wagtail 2.13 Release Notes)
https://wagtail.github.io/react-streamfield/public/
35 stars 12 forks source link

Code review #4

Open thibaudcolas opened 5 years ago

thibaudcolas commented 5 years ago

Alright! 🙂 I generally don't have the chance to review a whole package, that was a lot of fun. In the end I tried to be as exhaustive as possible with my comments, but a lot of it is fairly minor / quick to address.

I made the review as a pull request that contains all of the package's code, over at https://github.com/thibaudcolas/react-streamfield/pull/1, so it would be easier to relate comments to code. But I also summarised most of the comments below, so they are easier to relate to one another, and prioritise. It's probably easier to first read this list, then the comments in the PR.

I also made a PR to address some of the issues below (packaging, dev tools, and API), over at #5, along with the corresponding changes to Wagtail in a branch that builds upon https://github.com/wagtail/wagtail/pull/4942.

Potential problems

These are the code-level problems I would expect to cause the most pain / actual bugs. They are ordered by how important I think they are to address sooner rather than later.

Performance

Error handling

Generally I haven't seen much error handling code. I would expect the inner script execution to be the most problematic, since it will be very common for third-party code to break.

Minor ones

Minor but still sources of concern


Packaging - build & dependenceies

The general problem here is that the library is compiled as if it was an app, instead of a library, with all of its dependencies bundled instead of resolved by npm on install.

Bonus points

Documentation

To me this is what would be the most worthy of documentation. The blockDefinitions schema is probably this package’s most important API, and the polyfills are its least obvious requirements.

Development & demo env

Styles

Accessibility

I'm sure the current StreamField implementation isn't particularly SR-friendly, so we're not aiming super high, but there are obvious improvements to be made here.

react-redux

react-redux recently released its v6, which uses the new React context API from React 16.4, and introduces a change in behavior that affects this package:

[...] there is a behavior change around dispatching actions in constructors / componentWillMount. Previously, dispatching in a parent component's constructor would cause its children to immediately use the updated state as they mounted, because each component read from the store individually. In version 6, all components read the same current store state value from context, which means the tree will be consistent and not have "tearing". This is an improvement overall, but there may be applications that relied on the existing behavior.

This is the problematic code:

https://github.com/noripyt/react-streamfield/blob/9b720dd715140f5c931560e75431ea472600f9be/src/StreamField.js#L95-L107

The consequence is that BlocksContainer will fail to render because it does not expect to have access to an uninitialised state. It's generally not recommended to have side-effects in the constructor anyway, moving this init to componentDidMount would make the problem even more obvious.

I can see a few solutions:

Anyway, it's not necessary to upgrade to v6 now. I also have two concerns with the upgrade:


Finally on the react-redux front, I'm surprised that all/most of the components in the package are connected. I would expect the performance to be better if some of the connections were removed, as they clearly duplicate their computation (but use PureComponent or React.memo to still have the same rendering performance)

This would also make it easier to write tests for those components, which I would really like to see.

Smaller JS / React things

jonnyscholes commented 5 years ago

This PR https://github.com/noripyt/react-streamfield/pull/6 should resolve the following:

@thibaudcolas would be good to get you to confirm and provide your thoughts :)

thibaudcolas commented 5 years ago

Sweet, I'll try to have a look this weekend!

BertrandBordage commented 5 years ago

Thank you very much to both of you for these contributions!

@thibaudcolas I improved a lot of things as well after @jonnyscholes’ contribution. I updated the checkboxes above. As you can see, there is mainly one important thing remaining that I plan to do after we release the first integration to Wagtail: the error handling. I will go through the same incremental updates you did for Draftail.

I leave this review open for me to fix the remaining unticked checkboxes. But for now, this is already more than good to be in Wagtail :)