feat: Add chat management actions including clear and delete conversation, and enhance session token refresh logic.
This commit is contained in:
@@ -40,6 +40,9 @@ interface UseMessagingReturn {
|
||||
startTyping: () => void;
|
||||
stopTyping: () => void;
|
||||
markAsRead: () => Promise<void>;
|
||||
markAllAsRead: () => Promise<void>;
|
||||
deleteConversation: (conversationId: string) => Promise<void>;
|
||||
clearMessages: () => void;
|
||||
updateUserStatus: (userId: string, isOnline: boolean) => void;
|
||||
hasMoreMessages: boolean;
|
||||
}
|
||||
@@ -314,6 +317,40 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
||||
}
|
||||
}, [isConnected]);
|
||||
|
||||
// Mark all conversations as read
|
||||
const markAllAsRead = useCallback(async () => {
|
||||
const unreadConversations = conversations.filter((c) => c.unreadCount > 0);
|
||||
await Promise.all(
|
||||
unreadConversations.map((c) => messagesService.markAsRead(c.id))
|
||||
);
|
||||
setConversations((prev) =>
|
||||
prev.map((c) => ({ ...c, unreadCount: 0 }))
|
||||
);
|
||||
}, [conversations]);
|
||||
|
||||
// Delete a conversation
|
||||
const deleteConversation = useCallback(
|
||||
async (conversationId: string) => {
|
||||
await messagesService.deleteConversation(conversationId);
|
||||
|
||||
// Remove from list
|
||||
setConversations((prev) => prev.filter((c) => c.id !== conversationId));
|
||||
|
||||
// Clear current if it was the deleted one
|
||||
if (currentConversationIdRef.current === conversationId) {
|
||||
currentConversationIdRef.current = null;
|
||||
setCurrentConversation(null);
|
||||
setMessages([]);
|
||||
}
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
// Clear messages from current conversation view (local only)
|
||||
const clearMessages = useCallback(() => {
|
||||
setMessages([]);
|
||||
}, []);
|
||||
|
||||
// Update user online status (called from status change events)
|
||||
const updateUserStatus = useCallback((userId: string, isOnline: boolean) => {
|
||||
setConversations((prev) =>
|
||||
@@ -478,6 +515,9 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
||||
startTyping,
|
||||
stopTyping,
|
||||
markAsRead,
|
||||
markAllAsRead,
|
||||
deleteConversation,
|
||||
clearMessages,
|
||||
updateUserStatus,
|
||||
hasMoreMessages,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user