Skip to main content

useConnectionManager

Overview

The useConnectionManager 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
Unlike individual hooks, this manager hook handles the complete connection lifecycle in a single interface, making it ideal for implementing connection buttons and managing connection state throughout your user interface.

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

The connectionStatus field can have the following values:

Connection Data

The connectionData 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 userId changes
  • 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 userId matches 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:
  1. Network errors during status fetching or operations
  2. Authentication errors if the user is not logged in
  3. Invalid user IDs that don’t exist in the system
  4. Invalid operations (e.g., trying to accept when not in pending-received state)
All errors are logged to the console, and operations throw errors that you can catch and handle.

Best Practices

  1. Handle Loading States: Always check isLoading before allowing user interactions
  2. Status-Based UI: Render different UI elements based on connectionStatus
  3. Error Feedback: Implement user-visible error feedback for failed operations
  4. Prevent Self-Connect: The hook handles this automatically, but your UI should also reflect this
  5. Connection Messages: Consider prompting users for a message when sending requests

Comparison with useFollowManager