From 27e9fdabb4cadcf9140ef2c0b8c04ac01ba3f412 Mon Sep 17 00:00:00 2001 From: pradeepkumar Date: Thu, 19 Mar 2026 09:33:36 +0530 Subject: [PATCH] feat: Implement persistent chat clearing functionality by adding a new service method and integrating it into the `useMessaging` hook. --- src/hooks/useMessaging.ts | 27 ++++++++++++++++++++++++--- src/services/messages.service.ts | 7 +++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/hooks/useMessaging.ts b/src/hooks/useMessaging.ts index 82796ad..5b4a76e 100644 --- a/src/hooks/useMessaging.ts +++ b/src/hooks/useMessaging.ts @@ -346,9 +346,30 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet [] ); - // Clear messages from current conversation view (local only) - const clearMessages = useCallback(() => { - setMessages([]); + // Clear messages from current conversation (persisted to backend) + const clearMessages = useCallback(async () => { + const convId = currentConversationIdRef.current; + if (!convId) return; + + try { + await messagesService.clearChat(convId); + setMessages([]); + // Update conversation preview in the list + setConversations((prev) => + prev.map((c) => + c.id === convId + ? { ...c, lastMessageText: null, lastMessageAt: null, unreadCount: 0, userUnreadCount: 0, agentUnreadCount: 0 } + : c + ) + ); + setCurrentConversation((prev) => + prev && prev.id === convId + ? { ...prev, lastMessageText: null, lastMessageAt: null, unreadCount: 0, userUnreadCount: 0, agentUnreadCount: 0 } + : prev + ); + } catch (err) { + console.error('Failed to clear chat:', err); + } }, []); // Update user online status (called from status change events) diff --git a/src/services/messages.service.ts b/src/services/messages.service.ts index 5d1b594..179abd4 100644 --- a/src/services/messages.service.ts +++ b/src/services/messages.service.ts @@ -72,6 +72,13 @@ class MessagesService { await api.patch(`/messages/conversations/${conversationId}/read`); } + /** + * Clear all messages in a conversation (keep the conversation) + */ + async clearChat(conversationId: string): Promise { + await api.delete(`/messages/conversations/${conversationId}/messages`); + } + /** * Delete a conversation */