'use client'; import * as React from 'react'; import { useQuery } from '@tanstack/react-query'; import type { DateRange } from 'react-day-picker'; import { ALERT_LOG_STATUSES, listAlertLogs, type AlertLog, type AlertLogStatus, } from '@/lib/api/alert-logs'; import { CANAIS_TEMPLATE, CANAL_TEMPLATE_LABEL, TIPO_TEMPLATE_LABEL } from '@/lib/api/alert-templates'; import type { CanalTemplate } from '@/lib/api/alert-templates'; import { formatDateTime } from '@/lib/format'; import { Badge } from '@/components/ui/badge'; import { Button } from '@/components/ui/button'; import { DataTable, type DataTableColumn } from '@/components/ui/data-table'; import { DateRangePicker } from '@/components/ui/date-picker'; import { ResponsiveDialog } from '@/components/ui/responsive-dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { Separator } from '@/components/ui/separator'; const STATUS_LABEL: Record = { enviado: 'Enviado', entregue: 'Entregue', lido: 'Lido', falhou: 'Falhou', }; const STATUS_TONE: Record = { enviado: 'outline', entregue: 'default', lido: 'default', falhou: 'destructive', }; const STATUS_OPTIONS: { value: AlertLogStatus | 'todos'; label: string }[] = [ { value: 'todos', label: 'Todos os status' }, ...ALERT_LOG_STATUSES.map((s) => ({ value: s, label: STATUS_LABEL[s] })), ]; const CANAL_OPTIONS: { value: CanalTemplate | 'todos'; label: string }[] = [ { value: 'todos', label: 'Todos os canais' }, ...CANAIS_TEMPLATE.map((c) => ({ value: c, label: CANAL_TEMPLATE_LABEL[c] })), ]; const LIMIT = 20; export default function AlertLogsPage() { const [canal, setCanal] = React.useState('todos'); const [status, setStatus] = React.useState('todos'); const [periodo, setPeriodo] = React.useState(); const [page, setPage] = React.useState(1); const [detail, setDetail] = React.useState(null); const { data, isLoading } = useQuery({ queryKey: [ 'master', 'alert-logs', { canal, status, de: periodo?.from?.toISOString(), ate: periodo?.to?.toISOString(), page }, ], queryFn: () => listAlertLogs({ canal: canal === 'todos' ? undefined : canal, status: status === 'todos' ? undefined : status, data_inicio: periodo?.from?.toISOString(), data_fim: periodo?.to?.toISOString(), page, limit: LIMIT, }), placeholderData: (prev) => prev, }); const columns: DataTableColumn[] = [ { id: 'tipo', header: 'Tipo', primary: true, cell: (row) => (row.template ? TIPO_TEMPLATE_LABEL[row.template.tipo] : '—'), }, { id: 'canal', header: 'Canal', primary: true, cell: (row) => CANAL_TEMPLATE_LABEL[row.canal] }, { id: 'status', header: 'Status', cell: (row) => {STATUS_LABEL[row.status]}, }, { id: 'enviado_em', header: 'Enviado em', cell: (row) => formatDateTime(row.enviado_em) }, ]; return (

Logs de alerta

Histórico de envios de WhatsApp/e-mail

{ setPeriodo(range); setPage(1); }} placeholder="Período" className="w-full sm:w-64" />
row.id} loading={isLoading} emptyTitle="Nenhum log encontrado" emptyDescription="Ajuste os filtros de período/canal/status." onRowClick={(row) => setDetail(row)} rowActions={() => ( )} pagination={ data ? { page: data.meta.page, limit: data.meta.limit, total: data.meta.total, onPageChange: setPage } : undefined } /> !open && setDetail(null)} title="Detalhe do envio" description={detail ? formatDateTime(detail.enviado_em) : undefined} > {detail && (

Tipo

{detail.template ? TIPO_TEMPLATE_LABEL[detail.template.tipo] : '—'}

Canal

{CANAL_TEMPLATE_LABEL[detail.canal]}

Status

{STATUS_LABEL[detail.status]}

Conteúdo enviado

{detail.conteudo_enviado || '—'}

{detail.erro && (

Erro

{detail.erro}

)}
)}
); }