feat(react): add PermissionProvider, usePermissions hook and PermissionDeniedTooltip - #419
Conversation
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughAdds MyOrganization permission context and hook APIs with provider and admin fallback behavior, exports them publicly, and introduces a configurable tooltip for denied controls with accompanying tests. ChangesMyOrganization permissions
Permission denied tooltip
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant PermissionProvider
participant ReactQuery
participant coreClient
participant usePermissions
PermissionProvider->>ReactQuery: query member permissions
ReactQuery->>coreClient: request PERMISSION_MANIFEST permissions
coreClient-->>ReactQuery: return granted permissions
ReactQuery-->>PermissionProvider: update loading and permissions
PermissionProvider-->>usePermissions: expose context values
usePermissions-->>usePermissions: compute permission and tier helpers
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@coderabbitai Review the PR changes |
|
✅ Action performedReview finished.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/react/src/hooks/my-organization/shared/services/use-permissions.ts`:
- Around line 27-46: Separate the public permission hook from its internal
implementation: move the logic from usePermissions in
packages/react/src/hooks/my-organization/shared/services/use-permissions.ts into
use-permissions-service.ts, add the public use-permissions.ts wrapper under
hooks/my-organization, and have it delegate to the service. Update imports in
packages/react/src/providers/__tests__/permission-context.test.tsx and
packages/react/src/hooks/my-organization/shared/services/__tests__/use-permissions.test.tsx
to use the wrapper, and export that wrapper from packages/react/src/index.ts.
In `@packages/react/src/providers/permission-context.tsx`:
- Around line 33-35: Move the PermissionProviderProps interface from
permission-context.tsx into
types/my-organization/permissions/permissions-types.ts, export it there, and
import it in the PermissionProvider implementation. Keep the interface shape
unchanged and remove the local definition.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 95ed0a4b-a6d6-46eb-acef-57088938510d
📒 Files selected for processing (10)
packages/react/src/components/auth0/shared/__tests__/permission-denied-tooltip.test.tsxpackages/react/src/components/auth0/shared/permission-denied-tooltip.tsxpackages/react/src/hooks/my-organization/shared/services/__tests__/use-permissions.test.tsxpackages/react/src/hooks/my-organization/shared/services/use-permissions.tspackages/react/src/index.tspackages/react/src/providers/__tests__/permission-context.test.tsxpackages/react/src/providers/permission-context.tsxpackages/react/src/public-api.tspackages/react/src/tests/utils/__mocks__/core/core-client.mocks.tspackages/react/src/types/my-organization/permissions/permissions-types.ts
Summary
Adds the runtime permission layer on top of PR 1's manifest/utilities: a
PermissionProvidercontext that eagerly fetches the user's permissions, ausePermissionshook to consume them, and aPermissionDeniedTooltipfor gating disabled controls.This is PR 2 of 2 for the permission infrastructure. it targets PR 1's branch (
feat/permission-gating-infrastructure).Why
With the manifest and utilities in place (PR 1), components need a way to (a) load the current user's permissions once at app init and (b) read/check them anywhere in the tree. this follows the Permission Manifest Provider pattern with eager (app-init) fetching, and provides the standardized "missing permission" affordance the gating work will attach to disabled actions.
What
packages/reactproviders/permission-context.tsx—PermissionProvider+PermissionContext. On mount it requests the fullPERMISSION_MANIFEST(comma-separated) fromGET /my-org/user-permissionsvia React Query, caching the granted subset for 5 minutes (PERMISSION_STALE_TIME_MS). Query is keyed bypermissionQueryKeys.list()and gated oncoreClient.hooks/my-organization/use-permissions.ts—usePermissionsconsumes the context and exposespermissions,isLoading,hasProvider,hasPermission,hasAnyPermission,hasAllPermissions,getUserTier, andrefetch. When used outside a provider it falls back to admin-level access (the full manifest) for backwards compatibility with the legacyreadOnlyflow.components/auth0/shared/permission-denied-tooltip.tsx—PermissionDeniedTooltipwraps a (typically disabled) control with a standardized tooltip, defaulting tocommon.errors.forbiddenwith acustomMessageoverride. Children are wrapped in a focusable span so the tooltip still fires on disabled elements.configuration.members.getto the shared core-client test mock.Packages
packages/corepackages/reactexamplesTesting
Unit tests cover: the provider (eager fetch sends the manifest as a comma-separated list, exposes the granted subset, empty set until data resolves, 5-min cache constant); the hook (in-provider values/checks/loading/refetch, and out-of-provider admin fallback); and the tooltip (renders children, standardized + custom messages on hover, no wrapper when disabled).
Checklist
Contributing
Summary by CodeRabbit
New Features
Tests