-
Notifications
You must be signed in to change notification settings - Fork 2.4k
🔨 Hooks Refactor #1925
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
🔨 Hooks Refactor #1925
Conversation
Removed unused container styles and reorganized code to follow updated coding conventions (to be documented at a later date). Added a new useScript hook (based on https://www.robinwieruch.de/react-hooks-fetch-data/) and a hooks directory.
|
Build failure appears to be unrelated to this commit. Appears to run fine locally. If I'm missing a step here let me know! |
Whoops! Mixed up my links here~
Removed unused container styles and reorganized code to follow updated coding conventions (to be documented at a later date). Added a new useScript hook (based on https://usehooks.com/useScript/) and a hooks directory.
…code-conventions-refactor
Small example refactor showing how to use Cerebral with React Hooks
* Add @twhite96 as a contributor * Add iOS touch icons
CompuIves
left a comment
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.
Great!! Lots of nice cleaning, I mostly have questions about the context of Cerebral. Also, it would be better to create all new files in typescript, as we're making our move to there anyway 😄
packages/app/src/app/index.js
Outdated
| </ThemeProvider> | ||
| </ApolloProvider> | ||
| </Provider>, | ||
| <Cerebral.Provider value={controller.provide()}> |
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.
Interesting, why do we do this? Aren't we setting the context twice now?
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.
Technically yes, we are using two providers here. This seems to be the recommended approach to migrating to mobx-react-lite, as this new provider uses the react context api, and the old way does it different somehow. Basically to use useContext we have to do it this way. Doesn't appear to be a problem at the moment, but I'm happy to do further tests before we commit to this.
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.
This has been re-addressed in 519b214, as now it's only calling controller.provide() once and passing it's values to our providers. Once the whole refactor is complete, we can remove the old provider entirely.
packages/app/src/app/pages/Sandbox/Editor/Workspace/Advertisement/Advertisement.js
Show resolved
Hide resolved
| @@ -0,0 +1,18 @@ | |||
| import React, { useContext } from 'react'; | |||
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.
Would it be better to call this file index.js for consistency with the other files?
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.
No, the reason why we always split index.js and Component.js is to deliberately improve readability in a contributor's code editor. It quickly becomes confusing which file you're looking at when every component is defined inside of an index.js. Additionally, we may want to export multiple components, functions, etc from a single directory using named exports. A good example of this is a component that comes paired with say a GraphQL query fragment. It makes sense to keep those files separate for readability's sake, much like how we split component definitions from css-in-js with elements.js. The goal is to break files down into small, highly readable chunks. My general philosophy is that if I have to scroll to see all the contents of a single file, it's getting too big. Sometimes that's unavoidable, but you can use your best judgement there.
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.
Right, the only thing I'm afraid of is that we're now following two different naming schemes inside the repo (alongside two languages). For new contributors we need to be clear what the preferred way is.
Removed unused container styles and reorganized code to follow updated coding conventions (to be documented at a later date). Added a new useScript hook (based on https://www.robinwieruch.de/react-hooks-fetch-data/) and a hooks directory.
Whoops! Mixed up my links here~
Small example refactor showing how to use Cerebral with React Hooks
Implemented request to have all new files be TypeScript. Added a useInterval Hook (demo here: https://codesandbox.io/s/sessiontimer-useinterval-hook-demo-0l6jq) Refactored Live workspace item and it's dependents to use hooks. Refactored SSEDownNotice to use hooks. Instead of useContext(Cerebral), now using useSignals() and useStore()
…code-conventions-refactor
…de-conventions-refactor"
|
Will need some additional help testing the Live components refactor, as you can't start a live session when running the client locally. |
MichaelDeBoey
left a comment
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.
| }, [callback]); | ||
|
|
||
| useEffect(() => { | ||
| function tick() { |
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.
This can be an arrow function
const tick = () => savedCallback.current();
packages/app/src/app/pages/Sandbox/Editor/Workspace/Advertisement/Advertisement.tsx
Show resolved
Hide resolved
Moved Git and CreateRepo to sub-directories of Workspace/items/GitHub, as they aren't being used elsewhere.
|
@MichaelDeBoey Thanks for the suggestions! I'll consider a few of those. For reference, both of these were taken from the blog posts linked at the top of each file and slightly modified from their respective original author's source primarily to fit our linting rules. |
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.
Really great job in refactoring all these components @Saeris! 🙂
I think we should close this PR tho.
This one is only getting bigger and bigger and I think it would be better to have separate PR's for each refactored component instead of this one big PR (which is getting to big right now imo) so we can review it in smaller pieces.
Like I said: really great job so keep up the good work! 🙂
You can just cherry-pick your commit from this branch in new branches so this work doesn't need to be done multiple times 🙂
|
This is a huge improvement over the existing code! I agree with @MichaelDeBoey, this PR is huge, and that it's a big risk merging this all in at once. We did this once before and it caused some small bugs of which some are still not fixed today. I prefer to split this up into smaller testable commits, but I understand that that might be a lot of work. I'm okay with merging this PR in, but only if we do a lot of testing on staging before we're going to put this on prod. Here's a list of things that we'll need to test:
I'm also curious what @christianalfoni thinks about the state management, the main thing I want to know if this refactor will be as performant as our current solution, because that's one of the main reasons that we switched. I'm not sure how |
|
@CompuIves Another option would be to first merge #1956 (which is an extracted part from this PR). So if that one's merged, this PR can be extracted (via |
|
That'd be best, also leads to a cleaner git history. My points about testing still stand, but that way it will be a bit cleaner.
Makes sense, I want to know how this affects |
|
Yeah apologies for the huge PR! After that mixup the other day where all my work in progress got wiped out, I figured it would be safer for me to make multiple commits for each section I ended up having to re-write. So far in all of my manual testing, everything appears to work. Here's some of what I've tried successfully:
Forking and saving still work. Haven't made changes to ProjectInfo, so changing the Sandbox Title/Subtitle should be unaffected. No changes to File Explorer, so adding/removing dependencies should be unaffected. That leaves testing Live, Now Deployment, Commits/PRs, Offline, and performance. As far as |
|
Hi guys! The refactor cleans up a lot. I have not had time to go through everything, but conceptually it works exactly the same as before. The only thing to watch out for is that hooks does not handle So basically most components will probably benefit from being Anyways... I would drop going crazy with optimizations and if one pops up, just add I hope React will implement "no props, no reconcile", so when a parent causes child to reconcile it skips it when no props are passed. Cause with external state stores a surprising amount of components has no props. Anyways... this should also make the transition to Overmind simpler, as we are already in hooks world. |
|
@CompuIves I've extracted all @Saeris' changes into (smaller) separate branches/PRs:
|
|
@CompuIves I thought a decision was made to not continue with major refactors just for the sake of refactoring, so I guess this one (and #1964) can be closed? 🤔 Or do you want me to fix the few conflicts in #1964 so that can be closed and this one can be merged? 🤔 |
|
hm, has this PR diverged too far away from current |
|
yeah, this is impossible to merge now :/ I am gonna close this and explain to @Saeris |
Removed unused container styles and reorganized code to follow updated coding conventions (to be documented at a later date). Added a new useScript hook (based on https://usehooks.com/useScript/) and a hooks directory.