File size: 4,504 Bytes
94ac16a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { ReactNode } from "react";
import { Pagination } from "@/components/admin/Pagination";

interface ColumnDef<T> {
  id: string;
  header: string;
  accessorKey?: keyof T & string;
  accessorFn?: (row: T) => ReactNode;
  cell?: (props: { row: T; value: ReactNode }) => ReactNode;
  className?: string;
  sortable?: boolean;
  size?: string;
}

interface DataTableProps<T> {
  data: T[];
  columns: ColumnDef<T>[];
  isLoading?: boolean;
  emptyMessage?: string;
  keyExtractor: (row: T) => string | number;
  page?: number;
  totalPages?: number;
  onPageChange?: (p: number) => void;
  sortColumn?: string;
  sortDirection?: "asc" | "desc";
  onSort?: (column: string) => void;
  actions?: (row: T) => ReactNode;
  classNames?: {
    wrapper?: string;
    table?: string;
    thead?: string;
    th?: string;
    tbody?: string;
    td?: string;
  };
}

function DataTableInner<T>({
  data,
  columns,
  isLoading,
  emptyMessage = "No data.",
  keyExtractor,
  page,
  totalPages,
  onPageChange,
  sortColumn,
  sortDirection,
  onSort,
  actions,
  classNames = {},
}: DataTableProps<T>) {
  const cn = {
    wrapper: classNames.wrapper ?? "bg-[var(--pure-white)] rounded-[var(--radius-xl)] border border-[var(--oat-border)] overflow-hidden",
    table: classNames.table ?? "w-full text-sm",
    thead: classNames.thead ?? "bg-[var(--oat-light)] text-[var(--warm-charcoal)]",
    th: classNames.th ?? "text-left px-4 py-3 font-medium",
    tbody: classNames.tbody ?? "",
    td: classNames.td ?? "px-4 py-3",
  };

  const colCount = columns.length + (actions ? 1 : 0);

  function renderCell(row: T, col: ColumnDef<T>): ReactNode {
    let value: ReactNode;
    if (col.accessorKey !== undefined) {
      value = row[col.accessorKey] as ReactNode;
    } else if (col.accessorFn) {
      value = col.accessorFn(row);
    } else {
      value = null;
    }
    if (col.cell) {
      return col.cell({ row, value });
    }
    return value;
  }

  return (
    <div className={cn.wrapper}>
      <table className={cn.table}>
        <thead className={cn.thead}>
          <tr>
            {columns.map((col) => (
              <th
                key={col.id}
                className={`${cn.th} ${col.size ?? ""} ${col.className ?? ""} ${col.sortable ? "cursor-pointer select-none hover:text-[var(--clay-black)]" : ""}`}
                onClick={col.sortable ? () => onSort?.(col.id) : undefined}
                aria-sort={
                  sortColumn === col.id
                    ? sortDirection === "asc" ? "ascending" : "descending"
                    : undefined
                }
              >
                <span className="inline-flex items-center gap-1">
                  {col.header}
                  {col.sortable && sortColumn === col.id && (
                    <span className="text-xs">{sortDirection === "asc" ? "\u25B2" : "\u25BC"}</span>
                  )}
                </span>
              </th>
            ))}
            {actions && <th className={`${cn.th} text-right`}>Actions</th>}
          </tr>
        </thead>
        <tbody className={cn.tbody}>
          {isLoading ? (
            <tr>
              <td colSpan={colCount} className={`${cn.td} text-center py-16 text-[var(--warm-charcoal)]`}>
                Loading...
              </td>
            </tr>
          ) : data.length === 0 ? (
            <tr>
              <td colSpan={colCount} className={`${cn.td} text-center py-16 text-[var(--warm-charcoal)]`}>
                {emptyMessage}
              </td>
            </tr>
          ) : (
            data.map((row) => (
              <tr
                key={keyExtractor(row)}
                className="border-t border-[var(--oat-border)] hover:bg-[var(--oat-light)]/50 transition-colors"
              >
                {columns.map((col) => (
                  <td key={col.id} className={`${cn.td} ${col.size ?? ""}`}>
                    {renderCell(row, col)}
                  </td>
                ))}
                {actions && (
                  <td className={`${cn.td} text-right`}>
                    {actions(row)}
                  </td>
                )}
              </tr>
            ))
          )}
        </tbody>
      </table>
      {page !== undefined && totalPages !== undefined && onPageChange && (
        <Pagination page={page} totalPages={totalPages} onChange={onPageChange} />
      )}
    </div>
  );
}

export { DataTableInner as DataTable };
export type { ColumnDef, DataTableProps };