File size: 4,461 Bytes
e3278e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from 'react';
import {
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeaderCell,
  TableRow,
  Text,
  Badge,
  Icon,
  Button,
  Card,
} from "@tremor/react";
import { Tooltip } from "antd";

interface Column {
  header: string;
  accessor: string;
  cellRenderer?: (value: any, row: any) => React.ReactNode;
  width?: string;
  style?: React.CSSProperties;
}

interface Action<T = any> {
    icon?: React.ComponentType<any>;
    onClick: (item: T) => void;
    condition?: () => boolean;
    tooltip?: string;
}

interface DeleteModalProps {
  isOpen: boolean;
  onConfirm: () => void;
  onCancel: () => void;
  title: string;
  message: string;
}

interface DataTableProps {
  data: any[];
  columns: Column[];
  actions?: Action[];
  emptyMessage?: string;
  deleteModal?: DeleteModalProps;
}

const DataTable: React.FC<DataTableProps> = ({
  data,
  columns,
  actions,
  emptyMessage = "No data available",
  deleteModal
}) => {
  const renderCell = (column: Column, row: any) => {
    const value = row[column.accessor];
    
    if (column.cellRenderer) {
      return column.cellRenderer(value, row);
    }

    // Default cell rendering based on value type
    if (Array.isArray(value)) {
      return (
        <div style={{ display: "flex", flexDirection: "column" }}>
          {value.length === 0 ? (
            <Badge size="xs" className="mb-1" color="red">
              <Text>None</Text>
            </Badge>
          ) : (
            value.map((item: any, index: number) => (
              <Badge
                key={index}
                size="xs"
                className="mb-1"
                color="blue"
              >
                <Text>
                  {String(item).length > 30
                    ? `${String(item).slice(0, 30)}...`
                    : item}
                </Text>
              </Badge>
            ))
          )}
        </div>
      );
    }

    return value?.toString() || '';
  };

  return (
    <Card className="w-full mx-auto flex-auto overflow-y-auto max-h-[40vh]">
      <Table>
        <TableHead>
          <TableRow>
            {columns.map((column, index) => (
              <TableHeaderCell key={index}>{column.header}</TableHeaderCell>
            ))}
            {actions && actions.length > 0 && (
              <TableHeaderCell>Actions</TableHeaderCell>
            )}
          </TableRow>
        </TableHead>

        <TableBody>
          {data && data.length > 0 ? (
            data.map((row, rowIndex) => (
              <TableRow key={rowIndex}>
                {columns.map((column, colIndex) => (
                  <TableCell
                    key={colIndex}
                    style={{
                      maxWidth: column.width || "4px",
                      whiteSpace: "pre-wrap",
                      overflow: "hidden",
                      ...column.style
                    }}
                  >
                    {column.accessor === 'id' ? (
                      <Tooltip title={row[column.accessor]}>
                        {renderCell(column, row)}
                      </Tooltip>
                    ) : (
                      renderCell(column, row)
                    )}
                  </TableCell>
                ))}
                {actions && actions.length > 0 && (
                  <TableCell>
                    {actions.map((action, actionIndex) => (
                      // @ts-ignore
                      action.condition?.(row) !== false && (
                        <Tooltip key={actionIndex} title={action.tooltip}>
                          <Icon 
                            // @ts-ignore
                            icon={action.icon}
                            size="sm"
                            onClick={() => action.onClick(row)}
                            className="cursor-pointer mx-1"
                          />
                        </Tooltip>
                      )
                    ))}
                  </TableCell>
                )}
              </TableRow>
            ))
          ) : (
            <TableRow>
              <TableCell colSpan={columns.length + (actions ? 1 : 0)}>
                <Text className="text-center">{emptyMessage}</Text>
              </TableCell>
            </TableRow>
          )}
        </TableBody>
      </Table>
 
    </Card>
  );
};

export default DataTable;
export type { Action, Column, DataTableProps, DeleteModalProps };