feat: Implement persistent chat clearing functionality by adding a new service method and integrating it into the useMessaging hook.

This commit is contained in:
pradeepkumar
2026-03-19 09:33:36 +05:30
parent 29574ef5d8
commit 27e9fdabb4
2 changed files with 31 additions and 3 deletions

View File

@@ -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)