feat: Implement persistent chat clearing functionality by adding a new service method and integrating it into the useMessaging hook.
This commit is contained in:
@@ -346,9 +346,30 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
||||
[]
|
||||
);
|
||||
|
||||
// Clear messages from current conversation view (local only)
|
||||
const clearMessages = useCallback(() => {
|
||||
// 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)
|
||||
|
||||
@@ -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<void> {
|
||||
await api.delete(`/messages/conversations/${conversationId}/messages`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a conversation
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user