Overview
TheuseChat hook provides chat management functionality, allowing users to retrieve their chat list, create new chats, and delete existing chats. It integrates with localStorage to persist the current active chat.
Hook Signature
Return Value
The hook returns an object containing the following functions:Functions
chatsList
user(Object): User object containing:id(string/number): User ID
- On success: Array of chat objects
- On error: Error message string
newChat
chat(Object): Chat data object containing chat details
- On success: Newly created chat object
- On error: Error message string
- Creates a new chat via API
- Saves the new chat to localStorage as the active chat
- Returns the created chat data
deleteChat
id(string/number): Chat ID to delete
- On success:
'Chat eliminado'(Chat deleted) - On error: Error message string
Usage Example
Dependencies
This hook requires:useLocalStoragehook for persisting active chatchatServicefor API calls:getChatsById: Fetch chats by user IDcreatChat: Create a new chatdeletechat: Delete a chat by ID
Notes
- All functions use
useCallbackfor optimization - The active chat is automatically saved to localStorage when created
- Error handling returns string messages for easy display
- Success responses return data objects or confirmation strings
- The hook does not manage loading or error states - components should handle these
