@sqlrooms/data-table 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/package.json +6 -6
- package/.turbo/turbo-build.log +0 -4
- package/.turbo/turbo-dev.log +0 -7
- package/.turbo/turbo-lint.log +0 -31
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/eslint.config.js +0 -4
- package/src/DataTableModal.tsx +0 -44
- package/src/DataTablePaginated.tsx +0 -296
- package/src/DataTableVirtualized.tsx +0 -190
- package/src/QueryDataTable.tsx +0 -117
- package/src/index.ts +0 -6
- package/src/useArrowDataTable.tsx +0 -112
- package/tsconfig.json +0 -8
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import {Button, Popover, PopoverContent, PopoverTrigger} from '@sqlrooms/ui';
|
|
2
|
-
import {ClipboardIcon} from '@heroicons/react/24/outline';
|
|
3
|
-
import {shorten} from '@sqlrooms/utils';
|
|
4
|
-
import {createColumnHelper} from '@tanstack/react-table';
|
|
5
|
-
import {ColumnDef} from '@tanstack/table-core';
|
|
6
|
-
import * as arrow from 'apache-arrow';
|
|
7
|
-
import {useMemo} from 'react';
|
|
8
|
-
|
|
9
|
-
const columnHelper = createColumnHelper();
|
|
10
|
-
|
|
11
|
-
// TODO: support fetch the result chunks lazily https://github.com/duckdb/duckdb-wasm/tree/master/packages/duckdb-wasm#query-execution
|
|
12
|
-
|
|
13
|
-
type UseArrowDataTableResult = {
|
|
14
|
-
data: ArrayLike<any>;
|
|
15
|
-
columns: ColumnDef<any, any>[];
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export type ArrowColumnMeta = {
|
|
19
|
-
type: arrow.DataType;
|
|
20
|
-
isNumeric: boolean;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const MAX_VALUE_LENGTH = 64;
|
|
24
|
-
|
|
25
|
-
function valueToString(type: arrow.DataType, value: any): string {
|
|
26
|
-
if (value === null || value === undefined) {
|
|
27
|
-
return 'NULL';
|
|
28
|
-
}
|
|
29
|
-
if (arrow.DataType.isTimestamp(type)) {
|
|
30
|
-
switch (typeof value) {
|
|
31
|
-
case 'number':
|
|
32
|
-
case 'bigint':
|
|
33
|
-
return new Date(Number(value)).toISOString();
|
|
34
|
-
case 'string':
|
|
35
|
-
return new Date(value).toISOString();
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (arrow.DataType.isTime(type)) {
|
|
39
|
-
switch (typeof value) {
|
|
40
|
-
case 'number':
|
|
41
|
-
case 'bigint':
|
|
42
|
-
return new Date(Number(value) / 1000).toISOString().substring(11, 19);
|
|
43
|
-
case 'string':
|
|
44
|
-
return new Date(value).toISOString().substring(11, 19);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
if (arrow.DataType.isDate(type)) {
|
|
48
|
-
if (value instanceof Date) {
|
|
49
|
-
return value.toISOString();
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return String(value);
|
|
53
|
-
}
|
|
54
|
-
// Only use for small tables or in combination with pagination
|
|
55
|
-
export default function useArrowDataTable(
|
|
56
|
-
table: arrow.Table | undefined,
|
|
57
|
-
): UseArrowDataTableResult | undefined {
|
|
58
|
-
const data = useMemo(() => ({length: table?.numRows ?? 0}), [table]);
|
|
59
|
-
const columns = useMemo(() => {
|
|
60
|
-
if (!table) return undefined;
|
|
61
|
-
const columns: ColumnDef<any, any>[] = [];
|
|
62
|
-
for (const field of table.schema.fields) {
|
|
63
|
-
columns.push(
|
|
64
|
-
columnHelper.accessor((_row, i) => table.getChild(field.name)?.get(i), {
|
|
65
|
-
cell: (info) => {
|
|
66
|
-
const value = info.getValue();
|
|
67
|
-
const valueStr = valueToString(field.type, value);
|
|
68
|
-
|
|
69
|
-
return valueStr.length > MAX_VALUE_LENGTH ? (
|
|
70
|
-
<Popover>
|
|
71
|
-
<PopoverTrigger asChild>
|
|
72
|
-
<span className="cursor-pointer">
|
|
73
|
-
{shorten(`${valueStr}`, MAX_VALUE_LENGTH)}
|
|
74
|
-
</span>
|
|
75
|
-
</PopoverTrigger>
|
|
76
|
-
<PopoverContent className="font-mono text-xs w-auto max-w-[500px]">
|
|
77
|
-
<div className="space-y-2">
|
|
78
|
-
<div className="font-medium">{`"${field.name}" (${field.type})`}</div>
|
|
79
|
-
<div className="relative">
|
|
80
|
-
{valueStr}
|
|
81
|
-
<Button
|
|
82
|
-
variant="ghost"
|
|
83
|
-
size="icon"
|
|
84
|
-
className="absolute top-0 right-0 h-6 w-6"
|
|
85
|
-
onClick={() => navigator.clipboard.writeText(valueStr)}
|
|
86
|
-
>
|
|
87
|
-
<ClipboardIcon className="h-3 w-3" />
|
|
88
|
-
</Button>
|
|
89
|
-
</div>
|
|
90
|
-
</div>
|
|
91
|
-
</PopoverContent>
|
|
92
|
-
</Popover>
|
|
93
|
-
) : (
|
|
94
|
-
valueStr
|
|
95
|
-
);
|
|
96
|
-
},
|
|
97
|
-
header: field.name,
|
|
98
|
-
meta: {
|
|
99
|
-
type: field.type,
|
|
100
|
-
isNumeric:
|
|
101
|
-
arrow.DataType.isFloat(field.type) ||
|
|
102
|
-
arrow.DataType.isDecimal(field.type) ||
|
|
103
|
-
arrow.DataType.isInt(field.type),
|
|
104
|
-
},
|
|
105
|
-
}),
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
return columns;
|
|
109
|
-
}, [table]);
|
|
110
|
-
|
|
111
|
-
return data && columns ? {data, columns} : undefined;
|
|
112
|
-
}
|