Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Add useSetting hook #47979

Merged
merged 2 commits into from
Feb 22, 2023
Merged

Add useSetting hook #47979

merged 2 commits into from
Feb 22, 2023

Conversation

philipp-spiess
Copy link
Contributor

@philipp-spiess philipp-spiess commented Feb 21, 2023

Inspired by Vova's #47864, this adds a new useSettings() hook to replace the existing SettingsCascadeProps prop drilling.

I decided to not replace all of the props drilling just yet to avoid a massive merge conflict when Vova's PR ships. I also really want to focus on cleaning up the app shell instead and I need this particularly for one use case: Cleaning up globbing which, as it turns out, is just a computed value from the setting instead.

With this new settings API in place, I want to start trimming the shared props mess in a follow-up PR. This will then already remove the needs to pass the settings cascade around naturally and we will end up with a few smaller issues instead of a high change of breaking things.

I've also marked the existing settings APIs (including access to the SettingsCascade object directly) as deprecated. useSettings() should be used when possible. I don't think we need to validate the settings in every caller manually. 😅

Test plan

  • Add console.log(useSettings()) to a route
  • See the settings resolve. Wow. Magic!

Screenshot 2023-02-21 at 18 34 44

App preview:

Check out the client app preview documentation to learn more.

@philipp-spiess philipp-spiess self-assigned this Feb 21, 2023
@cla-bot cla-bot bot added the cla-signed label Feb 21, 2023
@github-actions github-actions bot added the team/code-exploration Issues owned by the Code Exploration team label Feb 21, 2023
@sg-e2e-regression-test-bob
Copy link

sg-e2e-regression-test-bob commented Feb 21, 2023

Bundle size report 📦

Initial size Total size Async size Modules
0.01% (+0.32 kb) 0.00% (+0.35 kb) 0.00% (+0.04 kb) 0.00% (0)

Look at the Statoscope report for a full comparison between the commits 0d1154a and acd88ab or learn more.

Open explanation
  • Initial size is the size of the initial bundle (the one that is loaded when you open the page)
  • Total size is the size of the initial bundle + all the async loaded chunks
  • Async size is the size of all the async loaded chunks
  • Modules is the number of modules in the initial bundle

Copy link
Contributor

@courier-new courier-new left a comment

Choose a reason for hiding this comment

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

I've long awaited this. 😍 EAGERLY APPROVE. 👍👍👍👍

Copy link
Contributor

@vovakulikov vovakulikov left a comment

Choose a reason for hiding this comment

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

@philipp-spiess Nice change, only one thought about \useExperimentalFeatures\ hook, at the moment, it works via zustand global store (not just a context), but this hook is related to your change because there is a two options now to get experimental settings (three actually if we count prop drilling)

Do you think we could (maybe as a follow-up) migrate \useExperimentalFeatures\ to the version where this hook uses your new useSetting hook internally (and doesn't use different approach with global zustand store)

@philipp-spiess
Copy link
Contributor Author

@vovakulikov Yes, this would be great! Do you know why we have zustand there by any chance? I wonder if I miss something by just doing what I do here and hooking it up in the app root 😅

@valerybugakov
Copy link
Member

Cleaning up globbing which, as it turns out, is just a computed value from the setting instead.

Lol, what? 😃

@valerybugakov
Copy link
Member

Do you know why we have zustand there by any chance?

I got the same question when refactoring the root search query state observer. Maybe @fkling can chime in and share more context?

The only reason that comes to my mind based on my limited experience with this part of the codebase is that zustand gives us reactivity. Still, according to the number of setExperimentalFeaturesFromSettings calls, we do not change experimental settings after the app init phase, so there's not much value in keeping this data reactive.

Copy link
Member

@valerybugakov valerybugakov left a comment

Choose a reason for hiding this comment

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

Seeing the prop drilling of the settings cascade gradually disappear will be so pleasant!

I decided to not replace all of the props drilling just yet
I also really want to focus on cleaning up the app shell instead

Love this line of reasoning! We can move faster if we prioritize migrations in the app shell and component under Storm routes (the first one will be the home page). Then we can gradually move route by route into the storm and refactor them incrementally. Sadly it wasn't possible with the react-router migration, but it should work without issues with our internal deprecated APIs!

@fkling
Copy link
Contributor

fkling commented Feb 22, 2023

Do you know why we have zustand there by any chance?

I got the same question when refactoring the root search query state observer. Maybe @fkling can chime in and share more context?

Mainly two reasons:

  • There was a time where we had been very reluctant to introduce new React contexts. Maybe those times have passed?
  • Because of how zustand works callers can pull out a subset of settings without getting re-rendered when the whole settings object changes. However if settings are not changing frequently (which I didn't consider back then) or considering that you navigate between pages anyway to change the settings then that's not really a problem.

I guess it should be relatively straightforward to simply change the internal implementation of useExperimentalFeatures.

@philipp-spiess
Copy link
Contributor Author

Because of how zustand works callers can pull out a subset of settings without getting re-rendered when the whole settings object changes. However if settings are not changing frequently (which I didn't consider back then) or considering that you navigate between pages anyway to change the settings then that's not really a problem.

Oh, I see. Yeah this is something that the Context API doesn't allow (yet, but it doesn't seem a priority either). I know of one place only where we depend on setting for something inside the current page and that is when configuring IDE extensions but it's something that is done at most ones (every subsequent setting change is expected to happen from inside the settings page). I wonder if it would be cleaner if we would just require a browser refresh when settings change. We could get rid of a lot of settings based propagation code if we can consider it readonly and static 🤔

Anyway this is a discussion for a different day :)

Copy link
Contributor

@vdavid vdavid left a comment

Choose a reason for hiding this comment

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

Oh wow, this seems life changing! Thank you! ❤️

@philipp-spiess philipp-spiess merged commit da6d971 into main Feb 22, 2023
@philipp-spiess philipp-spiess deleted the ps/settings-context branch February 22, 2023 14:09
philipp-spiess added a commit that referenced this pull request Feb 23, 2023
This PR changes the `useExperimentalFeatures` hook to use the new
`useSettings` API as discussed in #47979.
valerybugakov pushed a commit that referenced this pull request Feb 28, 2023
…8125)

This PR changes the `useExperimentalFeatures` hook to use the new
`useSettings` API as discussed in #47979.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed storm team/code-exploration Issues owned by the Code Exploration team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants