Fix portal comment submission with attachments

This commit is contained in:
Esdras Renan 2025-10-19 01:15:55 -03:00
parent f606ac1570
commit 275daa7c6e

View file

@ -39,16 +39,6 @@ const priorityTone: Record<TicketWithDetails["priority"], string> = {
URGENT: "bg-rose-100 text-rose-700", URGENT: "bg-rose-100 text-rose-700",
} }
function toHtmlFromText(text: string) {
const escaped = text
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;")
return `<p>${escaped.replace(/\n/g, "<br />")}</p>`
}
interface PortalTicketDetailProps { interface PortalTicketDetailProps {
ticketId: string ticketId: string
} }
@ -244,16 +234,20 @@ export function PortalTicketDetail({ ticketId }: PortalTicketDetailProps) {
return return
} }
if (!convexUserId || !ticket) return if (!convexUserId || !ticket) return
const trimmed = comment.trim() const rawHtml = comment || ""
const hasText = trimmed.length > 0 const sanitizedHtml = sanitizeEditorHtml(rawHtml)
if (!hasText && attachments.length === 0) { const plainText = typeof window !== "undefined"
? new DOMParser().parseFromString(sanitizedHtml, "text/html").body.textContent?.replace(/\u00a0/g, " ").trim() ?? ""
: sanitizedHtml.replace(/<[^>]*>/g, "").replace(/&nbsp;/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.") toast.error("Adicione uma mensagem ou anexe ao menos um arquivo antes de enviar.")
return return
} }
const toastId = "portal-add-comment" const toastId = "portal-add-comment"
toast.loading("Enviando comentário...", { id: toastId }) toast.loading("Enviando comentário...", { id: toastId })
try { try {
const htmlBody = hasText ? sanitizeEditorHtml(toHtmlFromText(trimmed)) : "<p></p>" const htmlBody = hasMeaningfulText ? sanitizedHtml : "<p></p>"
await addComment({ await addComment({
ticketId: ticket.id as Id<"tickets">, ticketId: ticket.id as Id<"tickets">,
authorId: convexUserId as Id<"users">, authorId: convexUserId as Id<"users">,