Overview
TheuseChatStore hook provides access to the global chat state and actions. Built with Zustand, it includes persistence via localStorage and manages the complete chat application state.
State properties
Core state
Dictionary of all chat conversations indexed by chat ID
Dictionary of all messages indexed by message ID
Dictionary of all contacts indexed by contact ID
Dictionary of draft messages indexed by chat ID
Array of active typing indicators across all chats
Recent search queries with timestamps
ID of the currently active/selected chat
User state
Current user’s profile information
Current search input value
Whether archived chats are visible
Methods
setActiveChat
Chat ID to activate, or undefined to clear active chat
- Clears
activeChatIdif no chatId provided - Resets
unreadCountto 0 for the selected chat - No-op if chatId doesn’t exist in state
setSearchQuery
New search query string
pushSearchHistory
Search term to add to history
- Updates
lastUsedAttimestamp if term already exists - Sorts history by most recently used
- Limits history to 5 items
updateDraft
Chat ID to update draft for
Partial draft properties to update
clearDraft
Chat ID to clear draft for
sendComposerPayload
Chat ID to send message to
Message content and metadata
- Generates unique message ID
- Creates message with “queued” status
- Clears draft for the chat
- Updates chat’s
lastActivityAtandlastMessagePreview - Schedules automatic status transitions: queued → sending → sent → delivered → read
Message status automatically progresses through lifecycle stages with delays between each transition.
receiveMessage
Chat ID to receive message in
Complete message object to add
- Appends message ID to chat’s
messageIdsarray - Increments
unreadCount(capped at 99) unless chat is active - Updates chat’s
lastActivityAtandlastMessagePreview
markChatAsRead
Chat ID to mark as read
setTypingIndicator
Chat ID where typing is occurring
ID of the user who is typing
Whether the user is currently typing
- Adds indicator with timestamp if
isTypingis true and doesn’t exist - Removes indicator if
isTypingis false and exists - No-op if state already matches desired state
toggleArchive
Chat ID to toggle archive status
resetStore
This clears all message status timers and resets to mock data state.
Persistence
The store automatically persists to localStorage with the following configuration:- Storage key:
chat-store - Version: 1
- Persisted state: chats, messages, contacts, drafts, searchHistory, profile, activeChatId, showArchived
- Excluded from persistence: searchQuery, typingIndicators (ephemeral state)