Socle for tier-based feature gating (UI-only soft-paywall).
- LicenseContext: machine-level provider (createContext<T|null>, useReducer,
throwing consumer hook), mounted above ProfileProvider in main.tsx so a
profile switch (BrowserRouter key remount) does not reload the license.
Loads edition + info once; exposes { status, edition, features, info, error,
refresh, submitKey }. Boot-error recovery (CWE-703): neutral state + capped
exponential-backoff retry, never the upsell.
- shared/entitlements.ts: FeatureKey (kebab-case), ENTITLEMENTS matrix, pure
isEntitled() fail-closed in Free (CWE-863) — the features[] override is
ignored before edition==="free" is checked.
- useEntitlement(f): { allowed, ready } (ready = status==="ready"), synchronous.
- useIsPremium + its test migrated onto the context (drops the per-call double
invoke); LicenseCard consumes the context. useLicense.ts removed (fully
replaced, no remaining consumers).
- services/entitlements.test.ts: matrix, features[] override, override ignored
in Free, unknown-feature deny-all.
Resolves #297
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
19 lines
778 B
TypeScript
19 lines
778 B
TypeScript
import { useLicenseContext } from "../contexts/LicenseContext";
|
|
import { isEntitled, type FeatureKey } from "../shared/entitlements";
|
|
|
|
/**
|
|
* Synchronous feature-gating hook.
|
|
*
|
|
* Returns `{ allowed, ready }` (NOT a bare boolean) so consumers can suppress
|
|
* the lock/upsell while the license is still loading or errored (`ready` is
|
|
* false) instead of flashing "locked" to a paying user. `allowed` is fail-closed
|
|
* in Free and during boot (edition defaults to "free"), so it is safe to read
|
|
* even before `ready`.
|
|
*/
|
|
export function useEntitlement(feature: FeatureKey): { allowed: boolean; ready: boolean } {
|
|
const { status, edition, features } = useLicenseContext();
|
|
return {
|
|
allowed: isEntitled(feature, edition, features),
|
|
ready: status === "ready",
|
|
};
|
|
}
|