All files / components/audit-logs AuditLogsTable.tsx

88.23% Statements 15/17
100% Branches 12/12
90% Functions 9/10
88.23% Lines 15/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                                                    49x 90x   21x   21x   21x   27x       49x         90x           90x                                           90x                   90x                 90x                   90x                                 49x       90x                                      
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Eye } from 'lucide-react';
import { format } from 'date-fns';
import type { AuditLogItem } from '@/services/audit.service';
import { DataTable, type Column } from '@/components/common/DataTable';
 
interface AuditLogsTableProps {
    logs: AuditLogItem[];
    totalItems: number;
    page: number;
    pageSize: number;
    onPageChange: (page: number) => void;
    onPageSizeChange: (pageSize: number) => void;
    sortBy: string;
    sortOrder: 'asc' | 'desc';
    onSort: (column: string) => void;
    onView: (log: AuditLogItem) => void;
    isLoading?: boolean;
}
 
export default function AuditLogsTable({
    logs,
    totalItems,
    page,
    pageSize,
    onPageChange,
    onPageSizeChange,
    sortBy,
    sortOrder,
    onSort,
    onView,
    isLoading,
}: AuditLogsTableProps) {
    const getOperationBadge = (operation: string) => {
        switch (operation) {
            case 'CREATE':
                return <Badge variant="default" className="bg-green-500 hover:bg-green-600">CREATE</Badge>;
            case 'UPDATE':
                return <Badge variant="secondary" className="bg-blue-500 hover:bg-blue-600 text-white">UPDATE</Badge>;
            case 'DELETE':
                return <Badge variant="destructive">DELETE</Badge>;
            default:
                return <Badge variant="outline">{operation}</Badge>;
        }
    };
 
    const columns: Column<AuditLogItem>[] = [
        {
            header: 'Timestamp',
            accessorKey: 'occurred_at',
            sortable: true,
            render: (log) => <span className="whitespace-nowrap">{format(new Date(log.occurred_at), 'yyyy-MM-dd HH:mm:ss')}</span>
        },
        {
            header: 'Operation',
            accessorKey: 'operation',
            sortable: true,
            render: (log) => getOperationBadge(log.operation)
        },
        {
            header: 'Collection',
            accessorKey: 'table_name',
            sortable: true,
            className: 'font-medium'
        },
        {
            header: 'Record ID',
            accessorKey: 'record_id',
            className: 'font-mono text-xs'
        },
        {
            header: 'Column',
            accessorKey: 'column_name'
        },
        {
            header: 'Old Value',
            accessorKey: 'old_value',
            className: 'max-w-[200px] truncate',
            render: (log) => (
                <span className="text-muted-foreground italic" title={log.old_value || 'None'}>
                    {log.old_value || '-'}
                </span>
            )
        },
        {
            header: 'New Value',
            accessorKey: 'new_value',
            className: 'max-w-[200px] truncate',
            render: (log) => (
                <span title={log.new_value || 'None'}>
                    {log.new_value || '-'}
                </span>
            )
        },
        {
            header: 'User',
            accessorKey: 'user_name',
            render: (log) => (
                <div className="flex flex-col">
                    <span className="text-sm font-medium">{log.user_name}</span>
                    <span className="text-xs text-muted-foreground">{log.user_email}</span>
                </div>
            )
        },
        {
            header: 'Actions',
            className: 'text-right',
            render: (log) => (
                <div className="flex justify-end">
                    <Button
                        variant="ghost"
                        size="icon"
                        onClick={(e) => {
                            e.stopPropagation();
                            onView(log);
                        }}
                        title="View Details"
                    >
                        <Eye className="h-4 w-4" />
                    </Button>
                </div>
            )
        }
    ];
 
    return (
        <DataTable
            data={logs}
            columns={columns}
            keyExtractor={(log) => log.id}
            isLoading={isLoading}
            totalItems={totalItems}
            pagination={{
                page,
                pageSize,
                onPageChange,
                onPageSizeChange
            }}
            sorting={{
                sortBy,
                sortOrder,
                onSort
            }}
            onRowClick={onView}
            noDataMessage="No audit logs found."
        />
    );
}