Skip to content

Feat/leave-brain#168

Draft
ad1tyayadav wants to merge 5 commits into
weam-ai:mainfrom
ad1tyayadav:feat/leave-brain-feature
Draft

Feat/leave-brain#168
ad1tyayadav wants to merge 5 commits into
weam-ai:mainfrom
ad1tyayadav:feat/leave-brain-feature

Conversation

@ad1tyayadav

@ad1tyayadav ad1tyayadav commented Oct 4, 2025

Copy link
Copy Markdown

Summary

This PR implements the "Leave Brain" functionality for invited users, addressing issue #84. Currently, invited users cannot voluntarily leave Brains they no longer wish to participate in, leading to dashboard clutter and poor user experience. This feature empowers users to manage their Brain memberships autonomously.

Key Changes:

  • Added LeaveBrainButton component with confirmation modal
  • Integrated leave functionality in Brain settings for non-owners
  • Implemented proper API integration with error handling
  • Added redirect to dashboard after successfully leaving a Brain
  • Maintained existing tooltip functionality for team member details

Change Type

  • New feature (non-breaking change which adds functionality)

##Demo

WeamAi.mp4

Testing

Test Configuration:

  • Browser: Chrome, Firefox, Safari
  • User Roles: Test with both Brain owners and invited users
  • Brain Types: Shared Brains with multiple members

Test Instructions:

  1. As an invited user:

    • Navigate to a shared Brain you've been invited to
    • Open Brain settings (Edit Brain modal)
    • Verify "Leave Brain" option is visible
    • Click "Leave Brain" and confirm in the modal
    • Verify redirection to dashboard
    • Confirm you no longer have access to the Brain
  2. As a Brain owner:

    • Verify "Leave Brain" option is hidden
    • Verify only "Archive Brain" option is available
  3. Edge Cases:

    • Test leaving a Brain where you're the only member
    • Test network failure scenarios
    • Verify proper error messages are displayed

Checklist

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • My changes do not introduce new warnings
  • I have written tests demonstrating that my changes are effective or that my feature works
  • Local unit tests pass with my changes

Additional Notes

Backend Integration:

  • Uses the /api/brain/leave endpoint with proper brainId parameter
  • Implements comprehensive error handling for API failures
  • Includes proper loading states during the leave process

UI/UX Considerations:

  • Confirmation modal prevents accidental leaves
  • Clear visual feedback during the process
  • Maintains consistency with existing modal patterns
  • Preserves all existing functionality (tooltips, member management, etc.)

Security:

  • Leave option only available to non-owners
  • Proper authentication checks in API calls
  • No disruption to Brain ownership or other members

Summary by CodeRabbit

  • New Features

    • Added a Leave Brain action with confirmation modal so non-owners can exit shared brains; leaving is persisted for the session.
  • UI Improvements

    • Leave Brain button added in brain lists and the edit modal for eligible users.
    • Edit modal layout refined for clearer ownership, members, and teams; loading states and toasts provide feedback.
  • Security/Access

    • Server-side checks ensure only authenticated, non-owner users can leave a brain and membership is updated.

@chsjd

chsjd commented Oct 6, 2025

Copy link
Copy Markdown
Collaborator

@ad1tyayadav Leave Brain Functionality working. But there is some issues in code structure.

  • Leave brain popup not match with the theme.
  • When click on Leave Brain api in your code it first go to next js api and from next js api it calling leave brain node js api. So this is not as per our code structure. You can review other api how we call it from next js.
  • In this file (nextjs/src/app/api/brain/leave/route.ts) there is static path (http://nodejs:4050/napi/v1/web/brain/${brainId}/leave), we do not need to add static path it will affect on server so review our code and call it same like other api calling.
  • There is some console log found in some files so also remove those.
  • For validation message use constant same like other api do not put it directly.
  • In this file nextjs/src/types/user.ts there is function Sessiontype you added id and token but I think no need of this keys.

@kstij

kstij commented Oct 7, 2025

Copy link
Copy Markdown

@ad1tyayadav Leave Brain Functionality working. But there is some issues in code structure.

  • Leave brain popup not match with the theme.
  • When click on Leave Brain api in your code it first go to next js api and from next js api it calling leave brain node js api. So this is not as per our code structure. You can review other api how we call it from next js.
  • In this file (nextjs/src/app/api/brain/leave/route.ts) there is static path (http://nodejs:4050/napi/v1/web/brain/${brainId}/leave), we do not need to add static path it will affect on server so review our code and call it same like other api calling.
  • There is some console log found in some files so also remove those.
  • For validation message use constant same like other api do not put it directly.
  • In this file nextjs/src/types/user.ts there is function Sessiontype you added id and token but I think no need of this keys.

hi @ad1tyayadav, kindly look into this and let us know if you need any support. thanks

@ad1tyayadav ad1tyayadav marked this pull request as draft October 7, 2025 07:16
@ad1tyayadav ad1tyayadav marked this pull request as ready for review October 7, 2025 18:35
@ad1tyayadav

Copy link
Copy Markdown
Author

@chsjd I’ve made all the requested changes.....let me know if anything else needs to be updated.

@chsjd

chsjd commented Oct 27, 2025

Copy link
Copy Markdown
Collaborator

@ad1tyayadav

The convertToShared functionality was removed but it’s still required.

Missing/removed items:
nextjs/src/actions/brains.ts — convertToSharedAction
nextjs/src/components/Brains/BrainList.tsx — ConvertToSharedModal

Additionally, when I run bash build.sh to create a production build, it fails with these TypeScript errors:

./src/components/Brains/BrainList.tsx:170:30
Type error: Cannot find name 'ShareBrainIcon'.

./src/components/Brains/BrainList.tsx:197:33
Type error: Property 'key' does not exist on type 'CommonListProps'.
export const CommonList = ({ b, key, currentUser, closeSidebar, onBrainLeave }: CommonListProps) => {

So, fix this issue and make sure do not remove existing functionality. Once all done then run it in production mode if all working fine then create pr after then we will review it again.

@ad1tyayadav ad1tyayadav force-pushed the feat/leave-brain-feature branch from 0454626 to 4ff65c6 Compare October 27, 2025 09:08
@chsjd

chsjd commented Oct 28, 2025

Copy link
Copy Markdown
Collaborator

@ad1tyayadav let me know once above commented all issue fixed. Add comment and mention what you fixed so can verify and merge pr.

@ad1tyayadav

ad1tyayadav commented Oct 28, 2025

Copy link
Copy Markdown
Author

@chsjd All issues have been fixed.

Here's what I addressed:-

1. Restored Convert to Shared Functionality

  • Added back convertToSharedAction in nextjs/src/actions/brains.ts
  • Restored ConvertToSharedModal component
  • Added back ShareBrainIcon import

2. Fixed TypeScript Build Errors

  • Removed key from CommonListProps interface
  • Fixed ShareBrainIcon cannot find name error by restoring the import

3. Contant.ts Improvements

  • Added validation message constants to nextjs/src/utils/constant.ts:
    • BRAIN_ID_REQUIRED
    • LEAVE_BRAIN_SUCCESS
    • LEAVE_BRAIN_ERROR
    • CONVERT_TO_SHARED_SUCCESS
    • CONVERT_TO_SHARED_ERROR
  • Updated leaveBrainAction to use constants instead of hardcoded messages
  • Updated ConvertToSharedModal to use constants for success/error messages

5. Leave Brain Feature (Original PR functionality)

  • Leave Brain button only shows for non-owner invited users
  • Proper confirmation modal before leaving
  • Redirects to dashboard after successful leave
  • Uses proper API structure (calls through server actions)
  • All validation messages use constants

Files Changed:

  1. nextjs/src/components/Brains/BrainList.tsx - Fixed TypeScript errors, restored Convert to Shared
  2. nextjs/src/actions/brains.ts - Added leaveBrainAction and convertToSharedAction with constants
  3. nextjs/src/utils/constant.ts - Added validation message constants
  4. nextjs/src/components/Brains/ConvertToSharedModal.tsx - Updated to use constants
  5. nextjs/src/hooks/brains/useLeaveBrain.ts - Uses constants for messages

@chsjd

chsjd commented Oct 28, 2025

Copy link
Copy Markdown
Collaborator

@ad1tyayadav CONVERT TO SHARED functionality still not working and also in sidebar I can not see leave brain icon so how user can leave brain.

Do one thing take a new clone of weam and add leave brain functionality in this. Create a pr after testing and let me know so we will verify and merge it in main.

@ad1tyayadav

Copy link
Copy Markdown
Author

okay I'll do soon

@ad1tyayadav ad1tyayadav marked this pull request as draft October 29, 2025 07:15

@JiwaniZakir JiwaniZakir left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

In leaveBrainAction (brains.ts), getSessionUser() is awaited before the brainId validation check, meaning an unnecessary async session lookup happens even when brainId is empty — the guard should be moved above the getSessionUser() call.

The isOwner comparison in BrainList.tsx (b.user.id === currentUser._id) uses strict equality between two fields that may be of different types — b.user.id likely comes from a serialized API response (string) while currentUser._id may be a MongoDB ObjectId. This comparison should be verified or explicitly coerced to strings (e.g., String(b.user.id) === String(currentUser._id)) to avoid silent permission mismatches where a user who is the owner is shown the "Leave" button instead of the edit controls.

Using MODULE_ACTIONS.UNSHARE as the underlying API action for leaving a brain is semantically ambiguous — if the backend's unshare endpoint is designed to remove any member (not just the caller), then the user_id passed in the body could be manipulated client-side. It's worth confirming the backend enforces that a user can only remove themselves via this flow, or that a dedicated leave endpoint is used instead.

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.

4 participants