Corrige badge de mensagens nao lidas no chat web e desktop
- Web: markChatRead agora zera unreadByAgent na sessao ativa - Desktop: usa unreadCount do backend ao inves de calcular localmente - Backend: listMachineMessages retorna unreadCount da sessao - Centraliza colunas da tabela de tickets do dispositivo 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
2f766af902
commit
695a44781a
4 changed files with 55 additions and 37 deletions
|
|
@ -94,8 +94,9 @@ export function ChatWidget({ ticketId, ticketRef }: ChatWidgetProps) {
|
|||
setIsLoading(false)
|
||||
setHasSession(payload.hasSession)
|
||||
hadSessionRef.current = hadSessionRef.current || payload.hasSession
|
||||
const unreadMessages = payload.messages.filter(m => !m.isFromMachine)
|
||||
setUnreadCount(unreadMessages.length)
|
||||
// Usa o unreadCount do backend (baseado em unreadByMachine da sessao)
|
||||
const backendUnreadCount = (payload as { unreadCount?: number }).unreadCount ?? 0
|
||||
setUnreadCount(backendUnreadCount)
|
||||
setMessages(prev => {
|
||||
const existingIds = new Set(prev.map(m => m.id))
|
||||
const combined = [...prev, ...payload.messages.filter(m => !existingIds.has(m.id))]
|
||||
|
|
@ -106,11 +107,12 @@ export function ChatWidget({ ticketId, ticketRef }: ChatWidgetProps) {
|
|||
const first = payload.messages[0]
|
||||
setTicketInfo((prevInfo) => prevInfo ?? { ref: 0, subject: "", agentName: first.authorName ?? "Suporte" })
|
||||
}
|
||||
// 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)
|
||||
// Marca como lidas se a janela estiver expandida e houver nao lidas
|
||||
if (backendUnreadCount > 0 && !isMinimized) {
|
||||
const unreadIds = payload.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))
|
||||
}
|
||||
}
|
||||
},
|
||||
(err) => {
|
||||
|
|
@ -139,15 +141,16 @@ export function ChatWidget({ ticketId, ticketRef }: ChatWidgetProps) {
|
|||
return () => window.removeEventListener("resize", handler)
|
||||
}, [])
|
||||
|
||||
// Quando expandir, marcar mensagens como lidas e limpar badge
|
||||
// Quando expandir, marcar mensagens como lidas (o backend zera unreadByMachine e a reatividade atualiza)
|
||||
useEffect(() => {
|
||||
if (isMinimized) return
|
||||
if (unreadCount === 0) return
|
||||
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)
|
||||
}, [isMinimized, messages, ticketId])
|
||||
// Nao setamos unreadCount aqui - o backend vai zerar unreadByMachine e a subscription vai atualizar
|
||||
}, [isMinimized, messages, ticketId, unreadCount])
|
||||
|
||||
// Selecionar arquivo para anexar
|
||||
const handleAttach = async () => {
|
||||
|
|
@ -249,12 +252,8 @@ export function ChatWidget({ ticketId, ticketRef }: ChatWidgetProps) {
|
|||
} 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)
|
||||
// Marca mensagens como lidas ao expandir (o useEffect ja cuida disso quando isMinimized muda)
|
||||
// O backend zera unreadByMachine e a subscription atualiza automaticamente
|
||||
}
|
||||
|
||||
const handleClose = () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue