feat: Add chat management actions including clear and delete conversation, and enhance session token refresh logic.

This commit is contained in:
pradeepkumar
2026-03-09 23:47:28 +05:30
parent ff5562a573
commit 7d641eec12
6 changed files with 254 additions and 22 deletions

View File

@@ -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,
};