Add PlatformContext to replace build-time hook switching#4508
Closed
kingsleyzissou wants to merge 7 commits into
Closed
Add PlatformContext to replace build-time hook switching#4508kingsleyzissou wants to merge 7 commits into
kingsleyzissou wants to merge 7 commits into
Conversation
Codecov Report❌ Patch coverage is @@ Coverage Diff @@
## main #4508 +/- ##
==========================================
+ Coverage 76.24% 76.43% +0.18%
==========================================
Files 225 228 +3
Lines 7204 7274 +70
Branches 2663 2669 +6
==========================================
+ Hits 5493 5560 +67
- Misses 1470 1471 +1
- Partials 241 243 +2
... and 1 file with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
5dfc2a2 to
02aa7cd
Compare
Provide the React context plumbing that entry points use to inject the correct platform hooks at runtime instead of relying on build-time environment variable switching.
Define the PlatformHooks type contract with queries, mutations, env, and api sections, then wire up hostedPlatform and onPremPlatform as static objects satisfying the contract. Uses per-field type casts where on-prem base query types diverge from hosted.
AppEntry and AppCockpit now inject their respective platform objects into the React tree so all descendants can access the correct hooks via usePlatform() instead of build-time switching.
Move 35 component and hook files from importing platform-switched hooks directly from barrel files to destructuring them from usePlatform(), preparing for barrel file cleanup.
All 17 process.env.IS_ON_PREMISE ternaries are now dead code since consumers migrated to usePlatform(). The barrel retains type re-exports, platform-independent hooks, and API slice references. Also migrates 4 consumer files missed in the prior consumer migration commit: CreateImageWizard, ProfileDetails, SecurityInformation, and PackageSearch now source their platform-switched hooks from usePlatform() instead of the barrel file.
The two platform-switched hook exports are no longer needed since consumers use usePlatform(). The barrel keeps hosted-only hooks and the contentSourcesApi reference.
02aa7cd to
6702e4a
Compare
Collaborator
Author
|
Closing this in favour of the webpack alias approach |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Introduce a
PlatformContextto replace build-timeprocess.env.IS_ON_PREMISEternaries with a runtime React context, enabling both hosted andon-prem platform implementations to coexist in the same bundle and be testable without separate builds.
Changes
PlatformContext,usePlatform()hook, andPlatformProviderwith aPlatformHookstype contract enforcing compile-time exhaustivenessvia
satisfieshostedPlatformandonPremPlatformas module-level singletons providing platform-specific RTK Query hooks, feature flags, andenvironment helpers
usePlatform()destructuringprocess.env.IS_ON_PREMISEternary switching from backend and content sources barrel files, replacing with clean re-exportsProxy-basedmockPlatformfixture for tests and update all test utilities (renderWithRedux,renderLandingPage) to derive platformfrom Redux state
Motivation for these changes
So this might be a hard sell, but I was looking at ways to reduce the
process.env.IS_ON_PREMISEcalls.But it's fundamentally about decoupling the two frontends. There are a couple of reasons for this but the most important one is:
This approach establishes a type contract here that could help us catch weird bugs at compilation rather than at run time. We can more effectively isolate things so hosted only gets hosted queries & mutations, environment flags and
so on. And on-prem would only get what it knows about.
In the future, it would also make it easier to switch to a mono repo and isolate cockpit and the service even further (this is an entirely separate discussion altogether though).
Notes
I haven't done a
useFlagmigration yet, that can be done in a follow up along with a general tidy up.