From 275daa7c6e4438717de87b0e482e1ef9bc1b51c2 Mon Sep 17 00:00:00 2001 From: Esdras Renan Date: Sun, 19 Oct 2025 01:15:55 -0300 Subject: [PATCH] Fix portal comment submission with attachments --- .../portal/portal-ticket-detail.tsx | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/components/portal/portal-ticket-detail.tsx b/src/components/portal/portal-ticket-detail.tsx index ebed0bf..9c0a138 100644 --- a/src/components/portal/portal-ticket-detail.tsx +++ b/src/components/portal/portal-ticket-detail.tsx @@ -39,16 +39,6 @@ const priorityTone: Record = { URGENT: "bg-rose-100 text-rose-700", } -function toHtmlFromText(text: string) { - const escaped = text - .replace(/&/g, "&") - .replace(//g, ">") - .replace(/"/g, """) - .replace(/'/g, "'") - return `

${escaped.replace(/\n/g, "
")}

` -} - interface PortalTicketDetailProps { ticketId: string } @@ -244,16 +234,20 @@ export function PortalTicketDetail({ ticketId }: PortalTicketDetailProps) { return } if (!convexUserId || !ticket) return - const trimmed = comment.trim() - const hasText = trimmed.length > 0 - if (!hasText && attachments.length === 0) { + const rawHtml = comment || "" + const sanitizedHtml = sanitizeEditorHtml(rawHtml) + const plainText = typeof window !== "undefined" + ? new DOMParser().parseFromString(sanitizedHtml, "text/html").body.textContent?.replace(/\u00a0/g, " ").trim() ?? "" + : sanitizedHtml.replace(/<[^>]*>/g, "").replace(/ /g, " ").trim() + const hasMeaningfulText = plainText.length > 0 + if (!hasMeaningfulText && attachments.length === 0) { toast.error("Adicione uma mensagem ou anexe ao menos um arquivo antes de enviar.") return } const toastId = "portal-add-comment" toast.loading("Enviando comentário...", { id: toastId }) try { - const htmlBody = hasText ? sanitizeEditorHtml(toHtmlFromText(trimmed)) : "

" + const htmlBody = hasMeaningfulText ? sanitizedHtml : "

" await addComment({ ticketId: ticket.id as Id<"tickets">, authorId: convexUserId as Id<"users">,