-
Notifications
You must be signed in to change notification settings - Fork 419
fix(shared): Invalidate organization memberships query #6530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: d0aae40 The changes in this PR will be included in the next version bump. This PR includes changesets to release 19 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
f3d6c66 to
3e1c15d
Compare
3e1c15d to
3a07b14
Compare
3a07b14 to
6de34b9
Compare
6de34b9 to
4424421
Compare
4424421 to
d0aae40
Compare
📝 WalkthroughWalkthrough
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
♻️ Duplicate comments (1)
packages/shared/src/react/hooks/useOrganizationList.tsx (1)
324-327: Also key offuser.updatedAtto ensure refresh when user changes (duplicate of earlier suggestion)Including
updatedAtstrengthens invalidation when the user object changes without affecting membership count (matches prior review suggestions).Apply this diff:
type: 'userMemberships', userId: user?.id, + updatedAt: user?.updatedAt, memberships: user?.organizationMemberships?.length ?? 0,
🧹 Nitpick comments (3)
.changeset/upset-sides-kneel.md (1)
1-6: Changeset entry looks good; tighten phrasing and punctuationNit: Clarify the wording and add terminal punctuation for clearer release notes.
Apply this diff:
--- '@clerk/shared': patch --- -Invalidate organization memberships based on client user +Invalidate user organization memberships when the current user changes.packages/shared/src/react/hooks/useOrganizationList.tsx (2)
312-328: Consider enabling the query only when a user is available to avoid undefined fetchersThe fetcher is
user?.getOrganizationMembershipswhileenabledcurrently depends only onuserMembershipsParams. IfuserMembershipsis provided butuserisn’t loaded yet, this may pass an undefined fetcher. IfusePagesOrInfinitedoesn’t guard against this, it can cause issues.Apply this diff to the options block to guard on user:
{ keepPreviousData: userMembershipsSafeValues.keepPreviousData, infinite: userMembershipsSafeValues.infinite, - enabled: !!userMembershipsParams, + enabled: !!userMembershipsParams && !!user, },
312-328: Add tests to cover cache key invalidation on membership changesNo tests were added. Please add tests asserting that the query key (or the hook’s behavior) changes when the user’s membership count changes and when
updatedAtchanges, ensuring proper invalidation.I can scaffold a test for
useOrganizationListthat:
- Mocks
useUserContextto emit a user with varyingorganizationMembershipslengths andupdatedAt.- Asserts that the call into
usePagesOrInfinitereceives different key parts across changes.
Would you like me to generate the test file?
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
.changeset/upset-sides-kneel.md(1 hunks)packages/shared/src/react/hooks/useOrganizationList.tsx(1 hunks)
🧰 Additional context used
📓 Path-based instructions (10)
.changeset/**
📄 CodeRabbit Inference Engine (.cursor/rules/monorepo.mdc)
Automated releases must use Changesets.
Files:
.changeset/upset-sides-kneel.md
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)
**/*.{js,jsx,ts,tsx}: All code must pass ESLint checks with the project's configuration
Follow established naming conventions (PascalCase for components, camelCase for variables)
Maintain comprehensive JSDoc comments for public APIs
Use dynamic imports for optional features
All public APIs must be documented with JSDoc
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Profile and optimize critical paths
Validate all inputs and sanitize outputs
Implement proper logging with different levels
Files:
packages/shared/src/react/hooks/useOrganizationList.tsx
**/*.{js,jsx,ts,tsx,json,css,scss,md,yaml,yml}
📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)
Use Prettier for consistent code formatting
Files:
packages/shared/src/react/hooks/useOrganizationList.tsx
packages/**/*.{ts,tsx}
📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)
TypeScript is required for all packages
Files:
packages/shared/src/react/hooks/useOrganizationList.tsx
packages/**/*.{ts,tsx,d.ts}
📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)
Packages should export TypeScript types alongside runtime code
Files:
packages/shared/src/react/hooks/useOrganizationList.tsx
**/*.{ts,tsx}
📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)
Use proper TypeScript error types
**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoidanytype - preferunknownwhen type is uncertain, then narrow with type guards
Useinterfacefor object shapes that might be extended
Usetypefor unions, primitives, and computed types
Preferreadonlyproperties for immutable data structures
Useprivatefor internal implementation details
Useprotectedfor inheritance hierarchies
Usepublicexplicitly for clarity in public APIs
Preferreadonlyfor properties that shouldn't change after construction
Prefer composition and interfaces over deep inheritance chains
Use mixins for shared behavior across unrelated classes
Implement dependency injection for loose coupling
Let TypeScript infer when types are obvious
Useconst assertionsfor literal types:as const
Usesatisfiesoperator for type checking without widening
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Use type-only imports:import type { ... } from ...
Noanytypes without justification
Proper error handling with typed errors
Consistent use ofreadonlyfor immutable data
Proper generic constraints
No unused type parameters
Proper use of utility types instead of manual type construction
Type-only imports where possible
Proper tree-shaking friendly exports
No circular dependencies
Efficient type computations (avoid deep recursion)
Files:
packages/shared/src/react/hooks/useOrganizationList.tsx
**/*.{jsx,tsx}
📄 CodeRabbit Inference Engine (.cursor/rules/development.mdc)
**/*.{jsx,tsx}: Use error boundaries in React components
Minimize re-renders in React components
**/*.{jsx,tsx}: Always use functional components with hooks instead of class components
Follow PascalCase naming for components:UserProfile,NavigationMenu
Keep components focused on a single responsibility - split large components
Limit component size to 150-200 lines; extract logic into custom hooks
Use composition over inheritance - prefer smaller, composable components
Export components as named exports for better tree-shaking
One component per file with matching filename and component name
Use useState for simple state management
Use useReducer for complex state logic
Implement proper state initialization
Use proper state updates with callbacks
Implement proper state cleanup
Use Context API for theme/authentication
Implement proper state selectors
Use proper state normalization
Implement proper state persistence
Use React.memo for expensive components
Implement proper useCallback for handlers
Use proper useMemo for expensive computations
Implement proper virtualization for lists
Use proper code splitting with React.lazy
Implement proper cleanup in useEffect
Use proper refs for DOM access
Implement proper event listener cleanup
Use proper abort controllers for fetch
Implement proper subscription cleanup
Use proper HTML elements
Implement proper ARIA attributes
Use proper heading hierarchy
Implement proper form labels
Use proper button types
Implement proper focus management
Use proper keyboard shortcuts
Implement proper tab order
Use proper skip links
Implement proper focus traps
Implement proper error boundaries
Use proper error logging
Implement proper error recovery
Use proper error messages
Implement proper error fallbacks
Use proper form validation
Implement proper error states
Use proper error messages
Implement proper form submission
Use proper form reset
Use proper component naming
Implement proper file naming
Use proper prop naming
Implement proper...
Files:
packages/shared/src/react/hooks/useOrganizationList.tsx
**/*.{js,ts,tsx,jsx}
📄 CodeRabbit Inference Engine (.cursor/rules/monorepo.mdc)
Support multiple Clerk environment variables (CLERK_, NEXT_PUBLIC_CLERK_, etc.) for configuration.
Files:
packages/shared/src/react/hooks/useOrganizationList.tsx
**/*.tsx
📄 CodeRabbit Inference Engine (.cursor/rules/react.mdc)
**/*.tsx: Use proper type definitions for props and state
Leverage TypeScript's type inference where possible
Use proper event types for handlers
Implement proper generic types for reusable components
Use proper type guards for conditional rendering
Files:
packages/shared/src/react/hooks/useOrganizationList.tsx
**/*
⚙️ CodeRabbit Configuration File
If there are no tests added or modified as part of the PR, please suggest that tests be added to cover the changes.
Files:
packages/shared/src/react/hooks/useOrganizationList.tsx
🧬 Code Graph Analysis (1)
packages/shared/src/react/hooks/useOrganizationList.tsx (1)
packages/react/src/isomorphicClerk.ts (1)
user(673-679)
🔇 Additional comments (1)
packages/shared/src/react/hooks/useOrganizationList.tsx (1)
324-327: No action needed:organizationMembershipsis always defined on UserResourceThe
UserResourceconstructor initializesorganizationMembershipsto an array (empty if not returned from the API), and the code safely falls back to0viauser?.organizationMemberships.length ?? 0. Moreover, theenabledflag (not the membership count) controls whetherusePagesOrInfiniteactually fetches data when you passuserMemberships.No changes are required here—you can safely rely on the existing optional chaining and default.
Likely an incorrect or invalid review comment.
| type: 'userMemberships', | ||
| userId: user?.id, | ||
| memberships: user?.organizationMemberships.length ?? 0, | ||
| }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential runtime crash: optional chain organizationMemberships before accessing .length
If user is defined but organizationMemberships is undefined/null (e.g., not expanded/initialized yet), user?.organizationMemberships.length will throw. Chain through organizationMemberships as well.
Apply this diff:
type: 'userMemberships',
userId: user?.id,
- memberships: user?.organizationMemberships.length ?? 0,
+ memberships: user?.organizationMemberships?.length ?? 0,📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| type: 'userMemberships', | |
| userId: user?.id, | |
| memberships: user?.organizationMemberships.length ?? 0, | |
| }, | |
| type: 'userMemberships', | |
| userId: user?.id, | |
| - memberships: user?.organizationMemberships.length ?? 0, | |
| + memberships: user?.organizationMemberships?.length ?? 0, | |
| }, |
🤖 Prompt for AI Agents
In packages/shared/src/react/hooks/useOrganizationList.tsx around lines 324 to
327, the optional chain stops at user so accessing
user?.organizationMemberships.length can throw if organizationMemberships is
undefined; update the expression to safely chain through organizationMemberships
(e.g., use user?.organizationMemberships?.length ?? 0) so memberships falls back
to 0 when organizationMemberships is undefined or null.
@clerk/agent-toolkit
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/dev-cli
@clerk/elements
@clerk/clerk-expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/clerk-react
@clerk/react-router
@clerk/remix
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/themes
@clerk/types
@clerk/upgrade
@clerk/vue
commit: |
Description
Before:
CleanShot.2025-08-12.at.16.40.16.mp4
After:
CleanShot.2025-08-13.at.11.01.44.mp4
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change
Summary by CodeRabbit