fix(web): org switching now updates sidebar and improve dropdown UX#970
Conversation
- Fix myOrganization tRPC procedure to filter by activeOrganizationId from session instead of returning first membership found - Update Header dropdown trigger to show org name + avatar (matching desktop app UX) with chevron icon - Simplify dropdown to show org switcher submenu and log out button
📝 WalkthroughWalkthroughModified the Header component to use organization-based data for avatar display and added an organization switcher dropdown. Updated the user organization query to filter memberships by both the current user and their active organization from the session. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@packages/trpc/src/router/user/user.ts`:
- Around line 14-22: The code reads ctx.session.session.activeOrganizationId
without guarding for missing session payload; change to use optional chaining
when extracting activeOrganizationId (e.g., const activeOrganizationId =
ctx.session?.session?.activeOrganizationId) and ensure the conditional passed to
db.query.members.findFirst still only includes the organization filter when
activeOrganizationId is defined so members.userId and members.organizationId
checks remain safe; update the logic around db.query.members.findFirst (and any
use in myOrganization) to handle undefined activeOrganizationId gracefully.
| const activeOrganizationId = ctx.session.session.activeOrganizationId; | ||
|
|
||
| const membership = await db.query.members.findFirst({ | ||
| where: eq(members.userId, ctx.session.user.id), | ||
| where: activeOrganizationId | ||
| ? and( | ||
| eq(members.userId, ctx.session.user.id), | ||
| eq(members.organizationId, activeOrganizationId), | ||
| ) | ||
| : eq(members.userId, ctx.session.user.id), |
There was a problem hiding this comment.
Guard against missing session payload before reading activeOrganizationId.
Line 14 assumes ctx.session.session is always defined; if it’s absent (e.g., session variants or partial payloads), this will throw and break myOrganization. The client-side usage already treats it as optional. Consider optional chaining to keep behavior aligned and fail-safe.
✅ Suggested fix
- const activeOrganizationId = ctx.session.session.activeOrganizationId;
+ const activeOrganizationId = ctx.session.session?.activeOrganizationId;📝 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.
| const activeOrganizationId = ctx.session.session.activeOrganizationId; | |
| const membership = await db.query.members.findFirst({ | |
| where: eq(members.userId, ctx.session.user.id), | |
| where: activeOrganizationId | |
| ? and( | |
| eq(members.userId, ctx.session.user.id), | |
| eq(members.organizationId, activeOrganizationId), | |
| ) | |
| : eq(members.userId, ctx.session.user.id), | |
| const activeOrganizationId = ctx.session.session?.activeOrganizationId; | |
| const membership = await db.query.members.findFirst({ | |
| where: activeOrganizationId | |
| ? and( | |
| eq(members.userId, ctx.session.user.id), | |
| eq(members.organizationId, activeOrganizationId), | |
| ) | |
| : eq(members.userId, ctx.session.user.id), |
🤖 Prompt for AI Agents
In `@packages/trpc/src/router/user/user.ts` around lines 14 - 22, The code reads
ctx.session.session.activeOrganizationId without guarding for missing session
payload; change to use optional chaining when extracting activeOrganizationId
(e.g., const activeOrganizationId = ctx.session?.session?.activeOrganizationId)
and ensure the conditional passed to db.query.members.findFirst still only
includes the organization filter when activeOrganizationId is defined so
members.userId and members.organizationId checks remain safe; update the logic
around db.query.members.findFirst (and any use in myOrganization) to handle
undefined activeOrganizationId gracefully.
🧹 Preview Cleanup CompleteThe following preview resources have been cleaned up:
Thank you for your contribution! 🎉 |
Summary
myOrganizationtRPC procedure to filter byactiveOrganizationIdfrom session instead of returning first membership found - this was causing the sidebar to not update when switching orgsTest plan
Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.