frontend: move web config access into config context/provider#3916
frontend: move web config access into config context/provider#3916smokyisthatyou wants to merge 4 commits intoBitBoxSwiss:masterfrom
Conversation
|
nice, please make one more change/commit. Currently each component still does a get request when using getConfig. Instead it could call getConfig once on startup and keep the "config" object in the context. getConfig could then either read out the local "config" object, or context could expose "config" object along with setConfig. I am not sure what is better. With this all components that depend on "config" object can read from the context without doing an additional api call. We want the source of truth still to be the backend, so in the context it should update the "config" object after setConfig and fetch from the api. Also the api function in utils should be moved into frontends/web/src/api/config.ts |
|
concept ACK |
30401e1 to
ea60d60
Compare
ea60d60 to
4da6264
Compare
|
@thisconnect moving this from draft, rebased |
|
@thisconnect ptal whenever you have time 🙏 |
| }; | ||
| export type { TConfig }; | ||
|
|
||
| let pendingConfig: Partial<TConfig> = {}; |
There was a problem hiding this comment.
Thinking about what it takes to move this into the react context.
The only place getConfig is used outside of react (component, hook, context) is in i18n/config.ts which is called once on app start. Here we could just use getConfig from api.
Similarly setConfig is used by i18n/i18n.ts, which is called once on startup (maybe not necessary) and when the user actively changes the language. Here I believe we cannot use setConfig from context, but there might be some workarounds (not 100% sure).
Other than for that we could move pendingConfig + the logic in setConfig into a context in the future.
There was a problem hiding this comment.
I think we should test/merge as is and in the future PR's:
- try to move this pending variable and logic into context.
- type config fully
both changes can be done independently
| getConfig().then(({ backend }) => setUsesProxy(backend.proxy.useProxy)); | ||
| }, []); | ||
| const { config } = useConfig(); | ||
| const usesProxy = (config?.backend?.proxy as { useProxy?: boolean } | undefined)?.useProxy; |
There was a problem hiding this comment.
Once we have config object fully typed in a future PR, this type casting can be simplified a lot or even removed completely.
| const frontend = config?.frontend; | ||
| if (frontend && typeof frontend === 'object') { | ||
| if (frontend.guideShown !== undefined) { | ||
| setGuideShown(Boolean(frontend.guideShown)); |
There was a problem hiding this comment.
Once we fully type TConfig in the future these safeguards can mostly be removed.
e991fa0 to
8fb8728
Compare
ed28dd9 to
925a212
Compare
|
@jadzeidan while working on this pr i think i introduced an old behaviour of the app that was opening the guide on first run. i implemented the code thinking it was the current expected flow, but @thisconnect pointed out this was actually deprecated in v4.40. personally i’m not a huge fan of the auto-opening guide for aesthetic reasons, though I can see the utility. |
|
thanks @smokyisthatyou for finding this 😄 I agree that guide shouldn't open automatically, so let's keep the "new" behaviour. |
96ad8f0 to
d730016
Compare
- Introduce ConfigProvider and ConfigContext and wrap app with provider - Fetch config once when the app loads and expose config + setConfig via useConfig - Refactor config consumers to read from context instead of calling getConfig per component
Stop auto-opening the guide when no frontend config is present. This prevents the guide overlay from blocking interactions on fresh starts.
- Add TFrontendConfig with known frontend settings flags and keep index signature - Update consumers to use typed config.frontend keys and drop casts/guards
- Add TBackendConfig and related types to api/config and use them in TConfig - Introduce TConfigUpdate so setConfig can accept partial backend/frontend updates - Update utils/config and ConfigProvider/Context to use TConfigUpdate merge + pendingConfig - Drop remaining backend casts in offline-error, electrum-servers, and RatesProvider
d730016 to
cacf20f
Compare
Add ConfigContext + ConfigProvider in frontends/web and refactor React components/hooks to consume getConfig/setConfig through useConfig() instead of importing utils/config directly. Keep non-React config consumers on the shared utils/config implementation path, and update related hook dependency arrays.
Before asking for reviews, here is a check list of the most common things you might need to consider:
cc @thisconnect