Skip to content

Conversation

@iagodahlem
Copy link
Member

@iagodahlem iagodahlem commented Aug 12, 2025

Description

Before:

CleanShot.2025-08-12.at.16.40.16.mp4

After:

CleanShot.2025-08-13.at.11.01.44.mp4

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Summary by CodeRabbit

  • New Features
    • Organization membership lists now stay in sync with your account, improving accuracy of counts and smoother pagination when browsing your organizations.
  • Bug Fixes
    • Reduced instances of stale membership data, ensuring the list reflects current memberships without manual refresh.
  • Chores
    • Added release metadata for a patch update to the shared package.

@changeset-bot
Copy link

changeset-bot bot commented Aug 12, 2025

🦋 Changeset detected

Latest commit: d0aae40

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 19 packages
Name Type
@clerk/shared Patch
@clerk/agent-toolkit Patch
@clerk/astro Patch
@clerk/backend Patch
@clerk/chrome-extension Patch
@clerk/clerk-js Patch
@clerk/elements Patch
@clerk/expo-passkeys Patch
@clerk/clerk-expo Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/clerk-react Patch
@clerk/remix Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch
@clerk/vue Patch

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

@vercel
Copy link

vercel bot commented Aug 12, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Project Deployment Preview Comments Updated (UTC)
clerk-js-sandbox Ready Preview Comment Aug 13, 2025 7:20pm

@iagodahlem iagodahlem changed the title fix: update useOrganizationList queries cache keys to include user.up… fix: update useOrganizationList queries cache keys Aug 13, 2025
@iagodahlem iagodahlem force-pushed the iago/fix-stale-organization-list-data branch from f3d6c66 to 3e1c15d Compare August 13, 2025 11:44
@iagodahlem iagodahlem force-pushed the iago/fix-stale-organization-list-data branch from 3e1c15d to 3a07b14 Compare August 13, 2025 13:47
@iagodahlem iagodahlem force-pushed the iago/fix-stale-organization-list-data branch from 3a07b14 to 6de34b9 Compare August 13, 2025 14:02
@iagodahlem iagodahlem force-pushed the iago/fix-stale-organization-list-data branch from 6de34b9 to 4424421 Compare August 13, 2025 19:17
@iagodahlem iagodahlem force-pushed the iago/fix-stale-organization-list-data branch from 4424421 to d0aae40 Compare August 13, 2025 19:19
@iagodahlem iagodahlem marked this pull request as ready for review August 13, 2025 19:29
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 13, 2025

📝 Walkthrough

Walkthrough

  • Adds a changeset entry declaring a patch release for @clerk/shared, noting organization membership invalidation based on the client user.
  • Updates packages/shared/src/react/hooks/useOrganizationList.tsx to pass a new memberships field into the options for usePagesOrInfinite when fetching userMemberships, using user?.organizationMemberships.length ?? 0.
  • No exported/public API signatures are modified.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 off user.updatedAt to ensure refresh when user changes (duplicate of earlier suggestion)

Including updatedAt strengthens 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 punctuation

Nit: 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 fetchers

The fetcher is user?.getOrganizationMemberships while enabled currently depends only on userMembershipsParams. If userMemberships is provided but user isn’t loaded yet, this may pass an undefined fetcher. If usePagesOrInfinite doesn’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 changes

No 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 updatedAt changes, ensuring proper invalidation.

I can scaffold a test for useOrganizationList that:

  • Mocks useUserContext to emit a user with varying organizationMemberships lengths and updatedAt.
  • Asserts that the call into usePagesOrInfinite receives 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

📥 Commits

Reviewing files that changed from the base of the PR and between 15fe106 and d0aae40.

📒 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
Avoid any type - prefer unknown when type is uncertain, then narrow with type guards
Use interface for object shapes that might be extended
Use type for unions, primitives, and computed types
Prefer readonly properties for immutable data structures
Use private for internal implementation details
Use protected for inheritance hierarchies
Use public explicitly for clarity in public APIs
Prefer readonly for 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
Use const assertions for literal types: as const
Use satisfies operator 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 ...
No any types without justification
Proper error handling with typed errors
Consistent use of readonly for 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: organizationMemberships is always defined on UserResource

The UserResource constructor initializes organizationMemberships to an array (empty if not returned from the API), and the code safely falls back to 0 via user?.organizationMemberships.length ?? 0. Moreover, the enabled flag (not the membership count) controls whether usePagesOrInfinite actually fetches data when you pass userMemberships.

No changes are required here—you can safely rely on the existing optional chaining and default.

Likely an incorrect or invalid review comment.

Comment on lines 324 to 327
type: 'userMemberships',
userId: user?.id,
memberships: user?.organizationMemberships.length ?? 0,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Suggested change
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.

@iagodahlem iagodahlem changed the title fix: update useOrganizationList queries cache keys fix(shared): update useOrganizationList queries cache keys Aug 13, 2025
@iagodahlem iagodahlem changed the title fix(shared): update useOrganizationList queries cache keys fix(shared): Invalidate organization memberships query Aug 13, 2025
@LauraBeatris LauraBeatris reopened this Aug 13, 2025
@LauraBeatris LauraBeatris self-requested a review August 13, 2025 19:51
@pkg-pr-new
Copy link

pkg-pr-new bot commented Aug 13, 2025

Open in StackBlitz

@clerk/agent-toolkit

npm i https://pkg.pr.new/@clerk/agent-toolkit@6530

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@6530

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@6530

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@6530

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@6530

@clerk/dev-cli

npm i https://pkg.pr.new/@clerk/dev-cli@6530

@clerk/elements

npm i https://pkg.pr.new/@clerk/elements@6530

@clerk/clerk-expo

npm i https://pkg.pr.new/@clerk/clerk-expo@6530

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@6530

@clerk/express

npm i https://pkg.pr.new/@clerk/express@6530

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@6530

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@6530

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@6530

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@6530

@clerk/clerk-react

npm i https://pkg.pr.new/@clerk/clerk-react@6530

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@6530

@clerk/remix

npm i https://pkg.pr.new/@clerk/remix@6530

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@6530

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@6530

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@6530

@clerk/themes

npm i https://pkg.pr.new/@clerk/themes@6530

@clerk/types

npm i https://pkg.pr.new/@clerk/types@6530

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@6530

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@6530

commit: d0aae40

@LauraBeatris LauraBeatris merged commit 59f1559 into main Aug 13, 2025
57 of 59 checks passed
@LauraBeatris LauraBeatris deleted the iago/fix-stale-organization-list-data branch August 13, 2025 20:25
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.

5 participants