Skip to content

Add PlatformContext to replace build-time hook switching#4508

Closed
kingsleyzissou wants to merge 7 commits into
osbuild:mainfrom
kingsleyzissou:feature/platform-context
Closed

Add PlatformContext to replace build-time hook switching#4508
kingsleyzissou wants to merge 7 commits into
osbuild:mainfrom
kingsleyzissou:feature/platform-context

Conversation

@kingsleyzissou

@kingsleyzissou kingsleyzissou commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

Summary

Introduce a PlatformContext to replace build-time process.env.IS_ON_PREMISE ternaries with a runtime React context, enabling both hosted and
on-prem platform implementations to coexist in the same bundle and be testable without separate builds.

Changes

  • Add PlatformContext, usePlatform() hook, and PlatformProvider with a PlatformHooks type contract enforcing compile-time exhaustiveness
    via satisfies
  • Implement hostedPlatform and onPremPlatform as module-level singletons providing platform-specific RTK Query hooks, feature flags, and
    environment helpers
  • Migrate all 40+ consumer components from direct barrel-file imports to usePlatform() destructuring
  • Remove process.env.IS_ON_PREMISE ternary switching from backend and content sources barrel files, replacing with clean re-exports
  • Add Proxy-based mockPlatform fixture for tests and update all test utilities (renderWithRedux, renderLandingPage) to derive platform
    from 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_PREMISE calls.
But it's fundamentally about decoupling the two frontends. There are a couple of reasons for this but the most important one is:

  • if we forget to add a ternary here, the on premise frontend could end up with a hosted hook and things would fail and we might only realise much later

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 useFlag migration yet, that can be done in a follow up along with a general tidy up.

@codecov

codecov Bot commented Jun 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.91549% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.43%. Comparing base (f8f6f58) to head (6702e4a).

Files with missing lines Patch % Lines
src/context/platform/hosted.ts 63.63% 3 Missing and 1 partial ⚠️
src/context/platform/onprem.ts 60.00% 3 Missing and 1 partial ⚠️
...geWizard/steps/Oscap/components/ProfileDetails.tsx 0.00% 1 Missing ⚠️
src/Components/ImagesTable/Status.tsx 75.00% 1 Missing ⚠️

Impacted file tree graph

@@            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     
Files with missing lines Coverage Δ
src/Components/Blueprints/BlueprintDiffModal.tsx 100.00% <100.00%> (ø)
src/Components/Blueprints/BlueprintsPagination.tsx 90.00% <100.00%> (+0.52%) ⬆️
src/Components/Blueprints/BlueprintsSideBar.tsx 88.33% <100.00%> (+0.40%) ⬆️
src/Components/Blueprints/BuildImagesButton.tsx 77.35% <100.00%> (+0.43%) ⬆️
src/Components/Blueprints/DeleteBlueprintModal.tsx 70.00% <100.00%> (+1.03%) ⬆️
...Components/CreateImageWizard/CreateImageWizard.tsx 85.95% <100.00%> (+0.05%) ⬆️
...steps/ImageOutput/components/ImageSourceSelect.tsx 98.68% <100.00%> (+0.01%) ⬆️
...steps/ImageOutput/components/TargetEnvironment.tsx 74.25% <100.00%> (+0.15%) ⬆️
...Wizard/steps/Kernel/components/KernelArguments.tsx 100.00% <100.00%> (ø)
...omponents/CreateImageWizard/steps/Locale/index.tsx 86.95% <100.00%> (+0.59%) ⬆️
... and 26 more

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f8f6f58...6702e4a. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@kingsleyzissou kingsleyzissou force-pushed the feature/platform-context branch 5 times, most recently from 5dfc2a2 to 02aa7cd Compare June 8, 2026 11:22
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.
@kingsleyzissou

Copy link
Copy Markdown
Collaborator Author

Closing this in favour of the webpack alias approach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant