feat: Implement conversation mute and favorite features with UI, API, and state management.
This commit is contained in:
@@ -44,6 +44,8 @@ interface UseMessagingReturn {
|
||||
deleteConversation: (conversationId: string) => Promise<void>;
|
||||
clearMessages: () => void;
|
||||
updateUserStatus: (userId: string, isOnline: boolean) => void;
|
||||
toggleMute: (conversationId: string, muted: boolean) => Promise<void>;
|
||||
toggleFavorite: (conversationId: string, favorited: boolean) => Promise<void>;
|
||||
hasMoreMessages: boolean;
|
||||
}
|
||||
|
||||
@@ -372,6 +374,59 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Toggle mute status (only for current user's side)
|
||||
const toggleMute = useCallback(async (conversationId: string, muted: boolean) => {
|
||||
try {
|
||||
await messagesService.toggleMute(conversationId, muted);
|
||||
// Backend sets the correct field based on caller's role;
|
||||
// optimistically update both fields since we don't know which one the server set,
|
||||
// but the API response and next fetch will have the correct values.
|
||||
// For the current user's perspective, both fields reflect "is this muted for me"
|
||||
const update = (c: Conversation) => {
|
||||
if (c.id !== conversationId) return c;
|
||||
// Determine which field to update based on the user's role in the conversation
|
||||
const userId = (session?.user as any)?.id;
|
||||
const isUser = c.userId === userId;
|
||||
return isUser
|
||||
? { ...c, userMuted: muted }
|
||||
: { ...c, agentMuted: muted };
|
||||
};
|
||||
setConversations((prev) => prev.map(update));
|
||||
setCurrentConversation((prev) => prev ? update(prev) : prev);
|
||||
} catch (err) {
|
||||
console.error('Failed to toggle mute:', err);
|
||||
}
|
||||
}, [session]);
|
||||
|
||||
// Toggle favorite status (only for current user's side)
|
||||
const toggleFavorite = useCallback(async (conversationId: string, favorited: boolean) => {
|
||||
try {
|
||||
await messagesService.toggleFavorite(conversationId, favorited);
|
||||
const userId = (session?.user as any)?.id;
|
||||
const update = (c: Conversation) => {
|
||||
if (c.id !== conversationId) return c;
|
||||
const isUser = c.userId === userId;
|
||||
return isUser
|
||||
? { ...c, userFavorited: favorited }
|
||||
: { ...c, agentFavorited: favorited };
|
||||
};
|
||||
setConversations((prev) => {
|
||||
const updated = prev.map(update);
|
||||
return updated.sort((a, b) => {
|
||||
const aFav = a.userId === userId ? a.userFavorited : a.agentFavorited;
|
||||
const bFav = b.userId === userId ? b.userFavorited : b.agentFavorited;
|
||||
if (aFav !== bFav) return aFav ? -1 : 1;
|
||||
const aTime = a.lastMessageAt ? new Date(a.lastMessageAt).getTime() : 0;
|
||||
const bTime = b.lastMessageAt ? new Date(b.lastMessageAt).getTime() : 0;
|
||||
return bTime - aTime;
|
||||
});
|
||||
});
|
||||
setCurrentConversation((prev) => prev ? update(prev) : prev);
|
||||
} catch (err) {
|
||||
console.error('Failed to toggle favorite:', err);
|
||||
}
|
||||
}, [session]);
|
||||
|
||||
// Update user online status (called from status change events)
|
||||
const updateUserStatus = useCallback((userId: string, isOnline: boolean) => {
|
||||
setConversations((prev) =>
|
||||
@@ -571,6 +626,8 @@ export function useMessaging(options: UseMessagingOptions = {}): UseMessagingRet
|
||||
deleteConversation,
|
||||
clearMessages,
|
||||
updateUserStatus,
|
||||
toggleMute,
|
||||
toggleFavorite,
|
||||
hasMoreMessages,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user