zasuiv0.1
GitHub
Menú

Escaneo y rutas

Audit Log Table

Tabla de un historial de auditoría: hora, código y detalle, con la fila teñida por resultado (éxito, error, aviso, evento).

Historial de una ruta

Notas

Base: ScanLogTable de la pestaña «Logs» de ZasExpress, sin la mezcla con la sesión de escaneo. Solo pinta: búsqueda, filtros y paginación los pone quien la usa (en Distribuidor, el historial de ruta y de bulto).

Instalación

Con la CLI de zasui (ver Introducción): instala el componente con sus dependencias y queda listo para usar.

pnpm dlx @medialityc/zasui add audit-log-table
Ver el código
import { cn } from '@/lib/utils';
import React from 'react';

export type AuditLogStatus = 'success' | 'error' | 'warning' | 'info';

export interface AuditLogRow {
	id: string;
	/** Fecha del evento (ISO o epoch ms). */
	at: string | number;
	status: AuditLogStatus;
	/** Código del bulto u objeto del que habla el evento; sin él, «—». */
	code?: string | null;
	/** Etiqueta corta delante del detalle (p. ej. «Escaneo», «Ruta»). */
	tag?: string | null;
	/** Qué pasó. */
	detail: React.ReactNode;
	/** Motivo o nota; en rojo si es un error, en ámbar si es un aviso. */
	reason?: string | null;
	/** Quién lo hizo. */
	user?: string | null;
}

export interface AuditLogTableProps extends Omit<
	React.ComponentProps<'div'>,
	'children'
> {
	rows: AuditLogRow[];
	/** Encabezado de la columna de código: `Bulto`, `HBL`, `Código`… */
	codeLabel?: string;
	/** Texto del cuerpo vacío, para distinguir «sin datos» de «sin resultados». */
	emptyMessage?: React.ReactNode;
}

const STATUS_STYLES: Record<AuditLogStatus, string> = {
	success:
		'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-300',
	error: 'bg-red-100 text-red-700 dark:bg-red-900/40 dark:text-red-300',
	warning:
		'bg-yellow-100 text-yellow-800 dark:bg-yellow-900/40 dark:text-yellow-300',
	info: 'bg-slate-200 text-slate-700 dark:bg-slate-700 dark:text-slate-200',
};

/**
 * Tabla de un historial de auditoría: hora, código y detalle, con la fila
 * teñida según el resultado. Sale de la tabla de la pestaña «Logs» de
 * zasexpress (`ScanLogTable`), sin la mezcla con la sesión de escaneo.
 */
export const AuditLogTable = ({
	rows,
	codeLabel = 'Código',
	emptyMessage = 'Sin registros.',
	className,
	...props
}: AuditLogTableProps) => (
	<div
		data-slot='audit-log-table'
		className={cn(
			'overflow-auto rounded border border-gray-200 bg-gray-50 dark:border-slate-700 dark:bg-slate-900',
			className,
		)}
		{...props}
	>
		<table className='w-full text-sm'>
			<thead className='sticky top-0 z-10 bg-gray-100 text-xs tracking-wide text-gray-700 uppercase dark:bg-slate-700 dark:text-gray-200'>
				<tr>
					<th className='px-3 py-2 text-left font-semibold'>Hora</th>
					<th className='px-3 py-2 text-left font-semibold'>{codeLabel}</th>
					<th className='px-3 py-2 text-left font-semibold'>Detalle</th>
				</tr>
			</thead>
			<tbody>
				{rows.length === 0 && (
					<tr>
						<td
							colSpan={3}
							className='px-4 py-6 text-center text-sm text-gray-500 dark:text-gray-400'
						>
							{emptyMessage}
						</td>
					</tr>
				)}
				{rows.map((row) => (
					<tr
						key={row.id}
						className={STATUS_STYLES[row.status]}
					>
						<td className='px-3 py-1.5 font-mono text-xs whitespace-nowrap text-gray-500 dark:text-gray-400'>
							{formatTime(row.at)}
						</td>
						<td className='px-3 py-1.5 font-mono text-sm font-semibold whitespace-nowrap text-gray-800 dark:text-gray-100'>
							{row.code || (
								<span className='font-normal text-gray-400 dark:text-gray-500'>
									—
								</span>
							)}
						</td>
						<td className='w-full min-w-0 px-3 py-1.5 text-xs text-gray-600 dark:text-gray-300'>
							<span>
								{row.tag && (
									<span className='rounded bg-gray-200 px-1.5 py-0.5 text-[11px] font-semibold text-gray-700 dark:bg-slate-700 dark:text-gray-200'>
										{row.tag}
									</span>
								)}{' '}
								{row.detail || '—'}
							</span>
							{row.user && (
								<span className='ml-1.5 text-[11px] text-gray-500 dark:text-gray-400'>
									· {row.user}
								</span>
							)}
							{row.reason && (
								<p
									className={cn(
										'mt-0.5 line-clamp-2 text-[11px]',
										row.status === 'error'
											? 'text-red-600 dark:text-red-400'
											: 'text-amber-700 dark:text-amber-400',
									)}
									title={row.reason}
								>
									{row.reason}
								</p>
							)}
						</td>
					</tr>
				))}
			</tbody>
		</table>
	</div>
);

/**
 * Hora del día para lo de hoy y fecha corta para lo anterior: un historial
 * puede arrastrar días y una hora suelta engañaría.
 */
function formatTime(at: string | number): string {
	const date = new Date(at);
	if (Number.isNaN(date.getTime())) return '—';
	const today = new Date();
	const sameDay = date.toDateString() === today.toDateString();
	return sameDay
		? date.toLocaleTimeString('es', { hour: '2-digit', minute: '2-digit' })
		: date.toLocaleString('es', {
				day: '2-digit',
				month: '2-digit',
				hour: '2-digit',
				minute: '2-digit',
			});
}