All files / laravel-saas/resources/js/Components/dashboard ActivityTable.tsx

0% Statements 0/134
0% Branches 0/1
0% Functions 0/1
0% Lines 0/134

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175                                                                                                                                                                                                                                                                                                                                                             
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from '@/Components/ui/Table';
import { Badge } from '@/Components/ui/Badge';
import { Avatar, AvatarImage, AvatarFallback } from '@/Components/ui/Avatar';
import { Card, CardContent, CardHeader } from '@/Components/ui/Card';
import { cn } from '@/lib/utils';
import { format } from 'date-fns';
 
interface ActivityItem {
  id: string;
  user: {
    name: string;
    email: string;
    avatar?: string;
  };
  action: string;
  target: string;
  status: 'success' | 'pending' | 'failed' | 'warning';
  timestamp: Date;
  metadata?: Record<string, any>;
}
 
interface ActivityTableProps {
  data: ActivityItem[];
  title?: string;
  className?: string;
  showHeader?: boolean;
}
 
const statusConfig = {
  success: {
    variant: 'default' as const,
    label: 'Success',
    className: 'bg-green-100 text-green-800 border-green-200',
  },
  pending: {
    variant: 'secondary' as const,
    label: 'Pending',
    className: 'bg-yellow-100 text-yellow-800 border-yellow-200',
  },
  failed: {
    variant: 'destructive' as const,
    label: 'Failed',
    className: 'bg-red-100 text-red-800 border-red-200',
  },
  warning: {
    variant: 'outline' as const,
    label: 'Warning',
    className: 'bg-orange-100 text-orange-800 border-orange-200',
  },
};
 
export function ActivityTable({ 
  data, 
  title = 'Recent Activity', 
  className,
  showHeader = true 
}: ActivityTableProps) {
  const formatTimestamp = (date: Date) => {
    try {
      return format(date, 'MMM dd, HH:mm');
    } catch {
      return 'Invalid date';
    }
  };
 
  const getStatusBadge = (status: ActivityItem['status']) => {
    const config = statusConfig[status];
    return (
      <Badge 
        variant={config.variant}
        className={cn('text-xs', config.className)}
      >
        {config.label}
      </Badge>
    );
  };
 
  const getUserInitials = (name: string) => {
    return name
      .split(' ')
      .map(n => n[0])
      .join('')
      .toUpperCase()
      .slice(0, 2);
  };
 
  if (data.length === 0) {
    return (
      <Card className={className}>
        {showHeader && (
          <CardHeader>
            <h3 className="text-lg font-semibold">{title}</h3>
          </CardHeader>
        )}
        <CardContent>
          <div className="text-center py-8 text-muted-foreground">
            No recent activity found
          </div>
        </CardContent>
      </Card>
    );
  }
 
  return (
    <Card className={className}>
      {showHeader && (
        <CardHeader>
          <h3 className="text-lg font-semibold">{title}</h3>
        </CardHeader>
      )}
      <CardContent className="p-0">
        <Table>
          <TableHeader>
            <TableRow>
              <TableHead>User</TableHead>
              <TableHead>Action</TableHead>
              <TableHead>Target</TableHead>
              <TableHead>Status</TableHead>
              <TableHead className="text-right">Time</TableHead>
            </TableRow>
          </TableHeader>
          <TableBody>
            {data.map((item) => (
              <TableRow key={item.id} className="hover:bg-muted/50">
                <TableCell>
                  <div className="flex items-center gap-3">
                    <Avatar className="h-8 w-8">
                      <AvatarImage src={item.user.avatar} alt={item.user.name} />
                      <AvatarFallback className="text-xs font-medium">
                        {getUserInitials(item.user.name)}
                      </AvatarFallback>
                    </Avatar>
                    <div className="min-w-0 flex-1">
                      <p className="text-sm font-medium truncate">
                        {item.user.name}
                      </p>
                      <p className="text-xs text-muted-foreground truncate">
                        {item.user.email}
                      </p>
                    </div>
                  </div>
                </TableCell>
                <TableCell>
                  <span className="text-sm font-medium">
                    {item.action}
                  </span>
                </TableCell>
                <TableCell>
                  <span className="text-sm text-muted-foreground">
                    {item.target}
                  </span>
                </TableCell>
                <TableCell>
                  {getStatusBadge(item.status)}
                </TableCell>
                <TableCell className="text-right">
                  <span className="text-sm text-muted-foreground">
                    {formatTimestamp(item.timestamp)}
                  </span>
                </TableCell>
              </TableRow>
            ))}
          </TableBody>
        </Table>
      </CardContent>
    </Card>
  );
}