feat: implement real-time conversation deletion handling via socket events
This commit is contained in:
@@ -587,6 +587,19 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Conversation deleted by the other party — remove from local state
|
||||||
|
const unsubscribeConvDeleted = socketService.onConversationDeleted(
|
||||||
|
(data: Record<string, unknown>) => {
|
||||||
|
const convId = data.conversationId as string | undefined;
|
||||||
|
if (!convId) return;
|
||||||
|
setConversations((prev) => prev.filter((c) => c.id !== convId));
|
||||||
|
if (currentConversationIdRef.current === convId) {
|
||||||
|
currentConversationIdRef.current = null;
|
||||||
|
setMessages([]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
// Auth error handling (token refresh + reconnect) is handled globally
|
// Auth error handling (token refresh + reconnect) is handled globally
|
||||||
// by PresenceProvider — no duplicate handler needed here.
|
// by PresenceProvider — no duplicate handler needed here.
|
||||||
|
|
||||||
@@ -598,6 +611,7 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
|||||||
unsubscribeStatus();
|
unsubscribeStatus();
|
||||||
unsubscribeRead();
|
unsubscribeRead();
|
||||||
unsubscribeDelivered();
|
unsubscribeDelivered();
|
||||||
|
unsubscribeConvDeleted();
|
||||||
// Clean up typing timeout to prevent memory leak
|
// Clean up typing timeout to prevent memory leak
|
||||||
if (typingTimeoutRef.current) {
|
if (typingTimeoutRef.current) {
|
||||||
clearTimeout(typingTimeoutRef.current);
|
clearTimeout(typingTimeoutRef.current);
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ class SocketService {
|
|||||||
private connectionResponseHandlers: Set<NotificationEventHandler> = new Set();
|
private connectionResponseHandlers: Set<NotificationEventHandler> = new Set();
|
||||||
private connectionHandlers: Set<(connected: boolean) => void> = new Set();
|
private connectionHandlers: Set<(connected: boolean) => void> = new Set();
|
||||||
private authErrorHandlers: Set<() => void> = new Set();
|
private authErrorHandlers: Set<() => void> = new Set();
|
||||||
|
private conversationDeletedHandlers: Set<NotificationEventHandler> = new Set();
|
||||||
private isConnecting = false;
|
private isConnecting = false;
|
||||||
private currentToken: string | null = null;
|
private currentToken: string | null = null;
|
||||||
private listenersAttached = false;
|
private listenersAttached = false;
|
||||||
@@ -157,6 +158,10 @@ class SocketService {
|
|||||||
this.socket.on('connection_response', (data: Record<string, unknown>) => {
|
this.socket.on('connection_response', (data: Record<string, unknown>) => {
|
||||||
this.connectionResponseHandlers.forEach((handler) => handler(data));
|
this.connectionResponseHandlers.forEach((handler) => handler(data));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.socket.on('conversation_deleted', (data: Record<string, unknown>) => {
|
||||||
|
this.conversationDeletedHandlers.forEach((handler) => handler(data));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reconnect with a fresh token (used when the previous token expired)
|
// Reconnect with a fresh token (used when the previous token expired)
|
||||||
@@ -196,6 +201,7 @@ class SocketService {
|
|||||||
this.deliveredHandlers.clear();
|
this.deliveredHandlers.clear();
|
||||||
this.connectionRequestHandlers.clear();
|
this.connectionRequestHandlers.clear();
|
||||||
this.connectionResponseHandlers.clear();
|
this.connectionResponseHandlers.clear();
|
||||||
|
this.conversationDeletedHandlers.clear();
|
||||||
this.connectionHandlers.clear();
|
this.connectionHandlers.clear();
|
||||||
this.authErrorHandlers.clear();
|
this.authErrorHandlers.clear();
|
||||||
}
|
}
|
||||||
@@ -348,6 +354,11 @@ class SocketService {
|
|||||||
return () => this.connectionResponseHandlers.delete(handler);
|
return () => this.connectionResponseHandlers.delete(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onConversationDeleted(handler: NotificationEventHandler): () => void {
|
||||||
|
this.conversationDeletedHandlers.add(handler);
|
||||||
|
return () => this.conversationDeletedHandlers.delete(handler);
|
||||||
|
}
|
||||||
|
|
||||||
onConnectionChange(handler: (connected: boolean) => void): () => void {
|
onConnectionChange(handler: (connected: boolean) => void): () => void {
|
||||||
this.connectionHandlers.add(handler);
|
this.connectionHandlers.add(handler);
|
||||||
return () => this.connectionHandlers.delete(handler);
|
return () => this.connectionHandlers.delete(handler);
|
||||||
|
|||||||
Reference in New Issue
Block a user