|
1
|
import { clx } from "@medusajs/ui" |
|
2
|
import { memo } from "react" |
|
3
|
import { NoRecords, NoResultsProps } from "../../common/empty-table-content" |
|
4
|
import { TableSkeleton } from "../../common/skeleton" |
|
5
|
import { DataTableQuery, DataTableQueryProps } from "./data-table-query" |
|
6
|
import { DataTableRoot, DataTableRootProps } from "./data-table-root" |
|
7
|
|
|
8
|
interface DataTableProps<TData> |
|
9
|
extends Omit<DataTableRootProps<TData>, "noResults">, |
|
10
|
DataTableQueryProps<TData> { |
|
11
|
isLoading?: boolean |
|
12
|
pageSize: number |
|
13
|
queryObject?: Record<string, any> |
|
14
|
noRecords?: Pick<NoResultsProps, "title" | "message"> |
|
15
|
} |
|
16
|
|
|
17
|
// Maybe we should use the memoized version of DataTableRoot |
|
18
|
// const MemoizedDataTableRoot = memo(DataTableRoot) as typeof DataTableRoot |
|
19
|
const MemoizedDataTableQuery = memo(DataTableQuery) as typeof DataTableQuery |
|
20
|
|
|
21
|
/** |
|
22
|
* @deprecated Use the DataTable component from "/components/data-table" instead |
|
23
|
*/ |
|
24
|
export const _DataTable = <TData,>({ |
|
25
|
table, |
|
26
|
columns, |
|
27
|
pagination, |
|
28
|
navigateTo, |
|
29
|
commands, |
|
30
|
count = 0, |
|
31
|
search = false, |
|
32
|
orderBy, |
|
33
|
filters, |
|
34
|
prefix, |
|
35
|
queryObject = {}, |
|
36
|
pageSize, |
|
37
|
isLoading = false, |
|
38
|
noHeader = false, |
|
39
|
layout = "fit", |
|
40
|
noRecords: noRecordsProps = {}, |
|
41
|
}: DataTableProps<TData>) => { |
|
42
|
if (isLoading) { |
|
43
|
return ( |
|
44
|
<TableSkeleton |
|
45
|
layout={layout} |
|
46
|
rowCount={pageSize} |
|
47
|
search={!!search} |
|
48
|
filters={!!filters?.length} |
|
49
|
orderBy={!!orderBy?.length} |
|
50
|
pagination={!!pagination} |
|
51
|
/> |
|
52
|
) |
|
53
|
} |
|
54
|
|
|
55
|
const noQuery = |
|
56
|
Object.values(queryObject).filter((v) => Boolean(v)).length === 0 |
|
57
|
const noResults = !isLoading && count === 0 && !noQuery |
|
58
|
const noRecords = !isLoading && count === 0 && noQuery |
|
59
|
|
|
60
|
if (noRecords) { |
|
61
|
return ( |
|
62
|
<NoRecords |
|
63
|
className={clx({ |
|
64
|
"flex h-full flex-col overflow-hidden": layout === "fill", |
|
65
|
})} |
|
66
|
{...noRecordsProps} |
|
67
|
/> |
|
68
|
) |
|
69
|
} |
|
70
|
|
|
71
|
return ( |
|
72
|
<div |
|
73
|
className={clx("divide-y", { |
|
74
|
"flex h-full flex-col overflow-hidden": layout === "fill", |
|
75
|
})} |
|
76
|
> |
|
77
|
<MemoizedDataTableQuery |
|
78
|
search={search} |
|
79
|
orderBy={orderBy} |
|
80
|
filters={filters} |
|
81
|
prefix={prefix} |
|
82
|
/> |
|
83
|
<DataTableRoot |
|
84
|
table={table} |
|
85
|
count={count} |
|
86
|
columns={columns} |
|
87
|
pagination |
|
88
|
navigateTo={navigateTo} |
|
89
|
commands={commands} |
|
90
|
noResults={noResults} |
|
91
|
noHeader={noHeader} |
|
92
|
layout={layout} |
|
93
|
/> |
|
94
|
</div> |
|
95
|
) |
|
96
|
} |