useAmigos hook manages all friend-related functionality in PARKINMX, including friend lists, friend requests, user search, and suggestions.
Overview
This hook provides a complete social layer for the app, allowing users to:- View their friends list
- Send and receive friend requests
- Search for other users by username
- Get friend suggestions
- Accept or reject friend requests
- Remove existing friends
All friend operations work exclusively with users who have the
role: "client" in Firestore to prevent admins from appearing in searches.Import
Usage
State Values
Array of accepted friends with user data (id, username, nombres, apellidos, photoURL, etc.)
Incoming friend requests with requestId and sender data
Array of user IDs to whom current user has sent pending requests
Suggested users for friend connections (up to 10 random clients)
Users matching the search query (filtered by username, up to 5 results)
Loading state for async operations
Functions
fetchFriends
Loads all accepted friends for the current user.- Queries
friend_requestscollection for accepted relationships - Fetches user data for each friend ID
- Updates
friendsstate with complete user objects
fetchRequests
Loads incoming and outgoing friend requests.- Incoming requests →
requestsstate (with sender data) - Outgoing requests →
sentRequestsstate (user IDs only)
searchUsers
Searches for users by username with debouncing (500ms delay).Search query (username prefix)
- Prefix search using Firestore
>=and<=operators - Filters out current user and non-clients
- Limits results to 5 users
- Automatic debouncing to prevent excessive queries
fetchSuggestions
Gets random user suggestions (clients only).role: "client", excluding the current user.
sendFriendRequest
Sends a friend request to another user.Target user’s Firebase UID
Target user’s username (for display in alerts)
- Checks for existing requests or friendships
- Creates
friend_requestsdocument withstatus: "pending" - Awards 50 XP to both users (sender and recipient)
- Shows success/error alerts
acceptRequest
Accepts an incoming friend request.Document ID of the friend request in Firestore
- Updates request status to
"accepted" - Awards additional 50 XP to both users
- Refreshes friend list and requests
- Shows success alert
rejectRequest
Rejects an incoming friend request.- Updates status to
"rejected"(keeps record for analytics) - Removes from requests list
- Shows confirmation alert
removeFriend
Removes an existing friendship (bi-directional).User ID of the friend to remove
- Shows confirmation dialog
- Finds the accepted
friend_requestsdocument - Deletes the document from Firestore
- Refreshes friend list
- Shows success alert
refreshAll
Convenience function to reload all data.fetchFriends(), fetchRequests(), and fetchSuggestions() in sequence.
Complete Example
AmigosScreen.tsx
Firebase Structure
friend_requests Collection
users Collection (relevant fields)
XP Rewards
Friend interactions award experience points:| Action | XP Awarded | Recipient |
|---|---|---|
| Send request | 50 | Both users |
| Accept request | 50 | Both users |
| Total per friendship | 100 | Each user |
Performance Considerations
- Debounced search: 500ms delay prevents excessive Firestore queries
- Firestore indexes: Required for username prefix search
- Batch queries: Friends fetched with
Promise.allfor parallel loading - Filtered suggestions: Role-based filtering reduces unnecessary data transfer
Related
- Community Friends Guide - User-facing friend system documentation
- Community Ranking - XP and leaderboard system
- User Profile Feature - Profile data structure