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