useConnectionManager
Overview
TheuseConnectionManager hook provides a comprehensive solution for managing connection relationships between users. Unlike simple follow relationships, connections are bidirectional and require mutual acceptance - similar to LinkedIn or Facebook connections.
This hook manages the complete connection workflow including:
- Checking connection status
- Sending connection requests (with optional message)
- Accepting or declining received requests
- Withdrawing sent requests
- Disconnecting from established connections
- Loading states and optimistic updates
Usage Example
Parameters & Returns
Parameters
The hook accepts an object with the following field:Returns
The hook returns an object with the following fields:Connection Status
TheconnectionStatus field can have the following values:
Connection Data
TheconnectionData object contains additional information about the connection:
Behavior & Features
Automatic Status Loading
- Automatically fetches the current connection status when the hook is initialized
- Handles loading states during the initial fetch
- Skips loading if the target user is the same as the authenticated user
Connection Request with Message
- Send connection requests with an optional personalized message
- Message parameter is optional - defaults to no message if not provided
State Management
- Prevents multiple simultaneous operations
- Maintains consistent state throughout the component lifecycle
- Automatically updates when the target
userIdchanges - Provides detailed connection metadata (dates, type)
Smart Operations
Each operation function is guarded to only work in appropriate states:sendConnectionRequest: Only works when status is"none"or"declined-received"acceptConnectionRequest: Only works when status is"pending-received"declineConnectionRequest: Only works when status is"pending-received"withdrawConnectionRequest: Only works when status is"pending-sent"disconnectUser: Only works when status is"connected"removeConnectionSmart: Works for any status except"none"and"declined-sent"
Error Handling
- Gracefully handles errors during status fetching
- Defaults to
"none"status if fetch fails - Logs errors to console for debugging purposes
- Operations throw errors that can be caught by calling code
Implementation Notes
Self-Connection Prevention
The hook automatically prevents users from connecting with themselves:- Skips status loading if
userIdmatches the authenticated user’s ID - Prevents all connection operations when targeting the same user
Loading States
The hook provides loading indication during:- Initial status fetch
- Connection request operations
- Accept/decline operations
- Disconnect operations
Status Refresh
After accepting a connection, the hook automatically refreshes to get updated connection data with timestamps.Error Scenarios
The hook handles several error scenarios:- Network errors during status fetching or operations
- Authentication errors if the user is not logged in
- Invalid user IDs that don’t exist in the system
- Invalid operations (e.g., trying to accept when not in pending-received state)
Best Practices
- Handle Loading States: Always check
isLoadingbefore allowing user interactions - Status-Based UI: Render different UI elements based on
connectionStatus - Error Feedback: Implement user-visible error feedback for failed operations
- Prevent Self-Connect: The hook handles this automatically, but your UI should also reflect this
- Connection Messages: Consider prompting users for a message when sending requests

