chat: remover placeholder [Anexo]

This commit is contained in:
esdrasrenan 2025-12-12 23:31:35 -03:00
parent 119ada60a6
commit ce5ea5dad5
3 changed files with 42 additions and 21 deletions

View file

@ -14,21 +14,33 @@ const getMessagesSchema = z.object({
limit: z.number().optional(),
})
const postMessageSchema = z.object({
machineToken: z.string().min(1),
ticketId: z.string().min(1),
body: z.string().min(1).max(4000),
attachments: z
.array(
z.object({
storageId: z.string(),
name: z.string(),
size: z.number().optional(),
type: z.string().optional(),
const postMessageSchema = z
.object({
machineToken: z.string().min(1),
ticketId: z.string().min(1),
body: z.string().max(4000).optional(),
attachments: z
.array(
z.object({
storageId: z.string(),
name: z.string(),
size: z.number().optional(),
type: z.string().optional(),
})
)
.optional(),
})
.superRefine((value, ctx) => {
const body = (value.body ?? "").trim()
const hasAttachments = (value.attachments?.length ?? 0) > 0
if (body.length === 0 && !hasAttachments) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Mensagem vazia",
path: ["body"],
})
)
.optional(),
})
}
})
const CORS_METHODS = "POST, OPTIONS"
@ -123,14 +135,18 @@ export async function POST(request: Request) {
}
try {
const body = (payload.body ?? "").trim()
const attachments =
payload.attachments && payload.attachments.length > 0 ? payload.attachments : undefined
// Retry com backoff exponencial para falhas transientes
const result = await withRetry(
() =>
client.mutation(api.liveChat.postMachineMessage, {
machineToken: payload.machineToken,
ticketId: payload.ticketId as Id<"tickets">,
body: payload.body,
attachments: payload.attachments as
body,
attachments: attachments as
| Array<{
storageId: Id<"_storage">
name: string

View file

@ -759,6 +759,10 @@ export function ChatWidget() {
<div className="space-y-3">
{messages.map((msg) => {
const isOwn = String(msg.authorId) === String(viewerId)
const bodyText = msg.body?.trim() ?? ""
const shouldShowBody =
bodyText.length > 0 &&
!(bodyText === "[Anexo]" && (msg.attachments?.length ?? 0) > 0)
return (
<div
key={msg.id}
@ -785,9 +789,7 @@ export function ChatWidget() {
{msg.authorName ?? "Cliente"}
</p>
)}
{msg.body && (
<p className="whitespace-pre-wrap text-sm">{msg.body}</p>
)}
{shouldShowBody && <p className="whitespace-pre-wrap text-sm">{msg.body}</p>}
{/* Anexos da mensagem */}
{msg.attachments && msg.attachments.length > 0 && (
<div className={cn("mt-2 flex flex-wrap gap-1.5", isOwn && "justify-end")}>