From 0afdba1635c90ce25fa164d59faa37fa9fb726e5 Mon Sep 17 00:00:00 2001 From: Seu Nome Date: Mon, 8 Dec 2025 12:22:44 -0300 Subject: [PATCH] fix: corrige chat reabrindo sozinho e melhora mensagens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove window.show() que forçava chat reabrir a cada polling - Chat só abre minimizado quando há NOVAS mensagens (janela não existia) - Se usuário fechou o chat, não reabre automaticamente - Corrige acentuação: "Voce" → "Você", "nao" → "não" - Simplifica toast para "Chat ao vivo iniciado" - Melhora mensagem de erro quando máquina está offline - Loga erro técnico no console ao invés de exibir para usuário 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- apps/desktop/src-tauri/src/chat.rs | 15 ++++++--------- apps/desktop/src/chat/ChatWidget.tsx | 2 +- apps/desktop/src/chat/index.tsx | 2 +- .../src/components/ChatFloatingWidget.tsx | 2 +- src/components/tickets/ticket-chat-panel.tsx | 12 +++++++++++- src/components/tickets/ticket-summary-header.tsx | 16 +++++++++++++--- 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/apps/desktop/src-tauri/src/chat.rs b/apps/desktop/src-tauri/src/chat.rs index 38f0385..d6ec9dc 100644 --- a/apps/desktop/src-tauri/src/chat.rs +++ b/apps/desktop/src-tauri/src/chat.rs @@ -818,26 +818,23 @@ async fn process_chat_update( ); // Mostrar janela de chat minimizada (menos intrusivo que abrir completo) + // Só abre se a janela não existir - se usuário fechou, não reabre automaticamente if let Some(session) = current_sessions.first() { - // Abrir janela se nao existir let label = format!("chat-{}", session.ticket_id); if app.get_webview_window(&label).is_none() { let _ = open_chat_window(app, &session.ticket_id); - // Minimizar imediatamente apos abrir + // Minimizar imediatamente após abrir let _ = set_chat_minimized(app, &session.ticket_id, true); } - // Se ja existe, apenas garantir que esta visivel (pode estar escondida) - else if let Some(window) = app.get_webview_window(&label) { - let _ = window.show(); - } + // Se janela já existe, não força reabrir - respeita decisão do usuário } // Notificacao nativa let notification_title = "Nova mensagem de suporte"; let notification_body = if new_count == 1 { - "Voce recebeu 1 nova mensagem no chat".to_string() + "Você recebeu 1 nova mensagem no chat".to_string() } else { - format!("Voce recebeu {} novas mensagens no chat", new_count) + format!("Você recebeu {} novas mensagens no chat", new_count) }; let _ = app .notification() @@ -929,7 +926,7 @@ pub fn minimize_chat_window(app: &tauri::AppHandle, ticket_id: &str) -> Result<( /// Redimensiona a janela de chat para modo minimizado (chip) ou expandido pub fn set_chat_minimized(app: &tauri::AppHandle, ticket_id: &str, minimized: bool) -> Result<(), String> { let label = format!("chat-{}", ticket_id); - let window = app.get_webview_window(&label).ok_or("Janela nao encontrada")?; + let window = app.get_webview_window(&label).ok_or("Janela não encontrada")?; // Tamanhos - chip minimizado com margem extra para badge (absolute -top-1 -right-1) let (width, height) = if minimized { diff --git a/apps/desktop/src/chat/ChatWidget.tsx b/apps/desktop/src/chat/ChatWidget.tsx index 348c39f..9958919 100644 --- a/apps/desktop/src/chat/ChatWidget.tsx +++ b/apps/desktop/src/chat/ChatWidget.tsx @@ -302,7 +302,7 @@ export function ChatWidget({ ticketId }: ChatWidgetProps) { setMessages(prev => [...prev, { id: response.messageId, body: messageText || (attachmentsToSend.length > 0 ? "[Anexo]" : ""), - authorName: "Voce", + authorName: "Você", isFromMachine: true, createdAt: response.createdAt, attachments: attachmentsToSend.map(a => ({ diff --git a/apps/desktop/src/chat/index.tsx b/apps/desktop/src/chat/index.tsx index afe7f7a..7e99526 100644 --- a/apps/desktop/src/chat/index.tsx +++ b/apps/desktop/src/chat/index.tsx @@ -8,7 +8,7 @@ export function ChatApp() { if (!ticketId) { return (
-

Erro: ticketId nao fornecido

+

Erro: ticketId não fornecido

) } diff --git a/apps/desktop/src/components/ChatFloatingWidget.tsx b/apps/desktop/src/components/ChatFloatingWidget.tsx index c68f5f3..84161c1 100644 --- a/apps/desktop/src/components/ChatFloatingWidget.tsx +++ b/apps/desktop/src/components/ChatFloatingWidget.tsx @@ -172,7 +172,7 @@ export function ChatFloatingWidget({ setMessages(prev => [...prev, { id: response.messageId, body: messageText, - authorName: "Voce", + authorName: "Você", isFromMachine: true, createdAt: response.createdAt, attachments: [], diff --git a/src/components/tickets/ticket-chat-panel.tsx b/src/components/tickets/ticket-chat-panel.tsx index 98cb122..c5abc4c 100644 --- a/src/components/tickets/ticket-chat-panel.tsx +++ b/src/components/tickets/ticket-chat-panel.tsx @@ -138,7 +138,17 @@ export function TicketChatPanel({ ticketId }: TicketChatPanelProps) { toast.info("Já existe uma sessão de chat ativa", { id: "live-chat" }) } } catch (error: unknown) { - const message = error instanceof Error ? error.message : "Não foi possível iniciar o chat" + console.error("[LiveChat] Erro ao iniciar chat:", error) + // Extrair mensagem amigável do erro do Convex + let message = "Não foi possível iniciar o chat" + if (error instanceof Error) { + const errorMsg = error.message.toLowerCase() + if (errorMsg.includes("offline")) { + message = "Máquina offline. Aguarde a máquina ficar online para iniciar o chat." + } else if (errorMsg.includes("não encontrad") || errorMsg.includes("not found")) { + message = "Máquina não encontrada" + } + } toast.error(message, { id: "live-chat" }) } finally { setIsStartingChat(false) diff --git a/src/components/tickets/ticket-summary-header.tsx b/src/components/tickets/ticket-summary-header.tsx index 7b136b6..fd1a1e7 100644 --- a/src/components/tickets/ticket-summary-header.tsx +++ b/src/components/tickets/ticket-summary-header.tsx @@ -351,12 +351,22 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) { actorId: convexUserId as Id<"users">, }) if (result.isNew) { - toast.success("Chat ao vivo iniciado! O cliente será notificado.", { id: "live-chat" }) + toast.success("Chat ao vivo iniciado", { id: "live-chat" }) } else { - toast.info("Já existe uma sessão de chat ativa.", { id: "live-chat" }) + toast.info("Já existe uma sessão de chat ativa", { id: "live-chat" }) } } catch (error: unknown) { - const message = error instanceof Error ? error.message : "Não foi possível iniciar o chat" + console.error("[LiveChat] Erro ao iniciar chat:", error) + // Extrair mensagem amigável do erro do Convex + let message = "Não foi possível iniciar o chat" + if (error instanceof Error) { + const errorMsg = error.message.toLowerCase() + if (errorMsg.includes("offline")) { + message = "Máquina offline. Aguarde a máquina ficar online para iniciar o chat." + } else if (errorMsg.includes("não encontrad") || errorMsg.includes("not found")) { + message = "Máquina não encontrada" + } + } toast.error(message, { id: "live-chat" }) } finally { setIsStartingChat(false)