Mantém badge de novas mensagens enquanto chat está minimizado

This commit is contained in:
esdrasrenan 2025-12-10 21:44:35 -03:00
parent 6ab1789c0f
commit 15a6b5ca87

View file

@ -85,6 +85,8 @@ export function ChatWidget({ ticketId, ticketRef }: ChatWidgetProps) {
setIsLoading(true)
setMessages([])
messagesSubRef.current?.()
// reset contador ao trocar ticket
setUnreadCount(0)
subscribeMachineMessages(
ticketId,
@ -92,8 +94,8 @@ export function ChatWidget({ ticketId, ticketRef }: ChatWidgetProps) {
setIsLoading(false)
setHasSession(payload.hasSession)
hadSessionRef.current = hadSessionRef.current || payload.hasSession
const unread = payload.messages.filter(m => !m.isFromMachine).length
setUnreadCount(unread)
const unreadMessages = payload.messages.filter(m => !m.isFromMachine)
setUnreadCount(unreadMessages.length)
setMessages(prev => {
const existingIds = new Set(prev.map(m => m.id))
const combined = [...prev, ...payload.messages.filter(m => !existingIds.has(m.id))]
@ -104,8 +106,9 @@ export function ChatWidget({ ticketId, ticketRef }: ChatWidgetProps) {
const first = payload.messages[0]
setTicketInfo((prevInfo) => prevInfo ?? { ref: 0, subject: "", agentName: first.authorName ?? "Suporte" })
}
const unreadIds = payload.messages.filter(m => !m.isFromMachine).map(m => m.id as string)
if (unreadIds.length > 0) {
// Só marca como lidas se a janela estiver expandida (evita perder badge ao minimizar)
const unreadIds = unreadMessages.map(m => m.id as string)
if (unreadIds.length > 0 && !isMinimized) {
markMachineMessagesRead(ticketId, unreadIds).catch(err => console.error("mark read falhou", err))
setUnreadCount(0)
}
@ -122,7 +125,7 @@ export function ChatWidget({ ticketId, ticketRef }: ChatWidgetProps) {
messagesSubRef.current?.()
messagesSubRef.current = null
}
}, [ticketId])
}, [ticketId, isMinimized])
// Selecionar arquivo para anexar
const handleAttach = async () => {
@ -219,12 +222,19 @@ export function ChatWidget({ ticketId, ticketRef }: ChatWidgetProps) {
const handleExpand = async () => {
setIsMinimized(false)
setUnreadCount(0)
try {
await invoke("set_chat_minimized", { ticketId, minimized: false })
} catch (err) {
console.error("Erro ao expandir janela:", err)
}
// Marca mensagens como lidas ao expandir
const unreadIds = messages.filter(m => !m.isFromMachine).map(m => m.id as string)
if (unreadIds.length > 0) {
markMachineMessagesRead(ticketId, unreadIds).catch(err => console.error("mark read falhou", err))
setUnreadCount(0)
} else {
setUnreadCount(0)
}
}
const handleClose = () => {