Skip to content

Commit 6702e4a

Browse files
AGENTS.md: document PlatformContext architecture and usage
1 parent 1779af6 commit 6702e4a

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

AGENTS.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,53 @@ Imports are enforced alphabetically by ESLint. Group order:
9292
- Playwright tests for E2E coverage of critical flows
9393
- The `TZ=UTC` prefix is applied automatically by npm scripts
9494

95+
## Platform Context
96+
97+
The app runs on two platforms — **hosted** (console.redhat.com) and **on-premises** (Cockpit plugin). Platform-specific hooks, API slices, and utilities are abstracted behind a React context so components never branch on the environment directly.
98+
99+
### Architecture
100+
101+
| File | Purpose |
102+
| --- | --- |
103+
| `src/context/platform/types.ts` | `PlatformHooks` type — the contract for `queries`, `mutations`, `env`, and `api` |
104+
| `src/context/platform/index.ts` | `PlatformContext`, `PlatformProvider`, and `usePlatform()` hook |
105+
| `src/context/platform/hosted.ts` | Hosted implementation (`hostedPlatform`) — Unleash flags, hosted RTK Query hooks |
106+
| `src/context/platform/onprem.ts` | On-prem implementation (`onPremPlatform`) — static flags, Cockpit/composer hooks |
107+
108+
### Barrel files
109+
110+
The store barrel files (`src/store/api/backend/index.ts`, `src/store/api/contentSources/index.ts`) no longer contain any `process.env.IS_ON_PREMISE` ternary logic. All platform-switched hooks are now sourced exclusively through `usePlatform()`. The barrels retain only:
111+
112+
- **Type re-exports** (hosted as canonical, prefixed on-prem types where they conflict)
113+
- **Platform-independent hooks** — hosted-only hooks gated at the component/route level (e.g. `useComposeImageMutation`, `useListRepositoriesQuery`)
114+
- **API slice references** (`imageBuilderApi`, `composerApi`, `contentSourcesApi`) for store setup and `invalidateTags`
115+
116+
### Usage
117+
118+
Entry points (`src/AppEntry.tsx`, Cockpit bootstrap) wrap the app with `<PlatformProvider value={hostedPlatform}>` or `<PlatformProvider value={onPremPlatform}>`.
119+
120+
Consumers destructure the specific hooks they need from `usePlatform()`:
121+
122+
```tsx
123+
import { usePlatform } from '@/context/platform';
124+
125+
const { queries: { useGetBlueprintQuery } } = usePlatform();
126+
const { data } = useGetBlueprintQuery({ id });
127+
```
128+
129+
Destructure to the individual hook level — not just `queries`/`mutations`/`api` — so call sites match the original direct-import style and keep diffs minimal.
130+
131+
### PlatformHooks shape
132+
133+
- **`queries`** — RTK Query hooks: `useGetBlueprintQuery`, `useGetBlueprintsQuery`, `useGetComposeStatusQuery`, `useGetArchitecturesQuery`, `useGetDistributionsQuery`, `useGetOscapProfilesQuery`, `useGetOscapCustomizationsQuery`, `useLazyGetBlueprintsQuery`, `useLazyGetOscapCustomizationsQuery`, `useGetComposesQuery`, `useGetBlueprintComposesQuery`
134+
- **`mutations`** — RTK Query mutation hooks: `useCreateBlueprintMutation`, `useUpdateBlueprintMutation`, `useDeleteBlueprintMutation`, `useComposeBlueprintMutation`, `useSearchRpmMutation`, `useListSnapshotsByDateMutation`
135+
- **`env`**`useFlag(flag)` and `useGetEnvironment()`
136+
- **`api`**`backendApi` (the RTK API slice for `invalidateTags` etc.), `contentSourcesApi`, `useBackendPrefetch`
137+
138+
### Testing
139+
140+
Tests use a `mockPlatform` fixture from `src/context/platform/tests/mocks/` and render inside a `PlatformProvider`. When mocking `usePlatform` directly via `vi.mock`, spread `mockPlatform` and override only the hooks under test.
141+
95142
## Feature Flags
96143

97144
Uses Unleash for feature toggles in the hosted service. Import `useFlag` from `src/Utilities/useGetEnvironment.ts` to check flag status:

0 commit comments

Comments
 (0)