-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathdata-table.tsx
More file actions
189 lines (177 loc) · 6.42 KB
/
data-table.tsx
File metadata and controls
189 lines (177 loc) · 6.42 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { useCallback, useState } from 'react'
import { Check, ChevronLeft, ChevronRight, Copy } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
import { cn } from '@/lib/utils'
import { useColumnResize } from './use-column-resize'
import type { QueryResult } from './types'
type DataTableProps = {
result: QueryResult | null
isLoading: boolean
page: number
pageSize: number
totalRows: number
onPageChange: (page: number) => void
onPageSizeChange: (size: number) => void
}
const pageSizes = [25, 50, 100, 200]
const formatCellValue = (value: unknown): string => {
if (value === null || value === undefined) {
return 'NULL'
}
if (typeof value === 'object') {
return JSON.stringify(value)
}
return String(value)
}
export const DataTable = ({
result,
isLoading,
page,
pageSize,
totalRows,
onPageChange,
onPageSizeChange,
}: DataTableProps) => {
const columnCount = result?.columns.length ?? 0
const { getColumnWidth, onMouseDown, totalWidth } = useColumnResize(columnCount)
const [copiedCell, setCopiedCell] = useState<string | null>(null)
const handleCopy = useCallback(async (value: string, cellKey: string) => {
await navigator.clipboard.writeText(value)
setCopiedCell(cellKey)
setTimeout(() => setCopiedCell(null), 1500)
}, [])
if (!result) {
return (
<div className="text-muted-foreground flex flex-1 items-center justify-center text-sm">
Select a table or run a query to see results
</div>
)
}
const totalPages = Math.ceil(totalRows / pageSize)
const hasNextPage = page < totalPages - 1
const hasPrevPage = page > 0
return (
<div className="flex flex-1 flex-col overflow-hidden">
{/* Table with horizontal scroll */}
<div className="flex-1 overflow-auto">
<table className="border-collapse" style={{ width: Math.max(totalWidth, 100), tableLayout: 'fixed' }}>
<colgroup>
{result.columns.map((_, i) => (
<col key={i} style={{ width: getColumnWidth(i) }} />
))}
</colgroup>
<thead className="bg-muted/50 sticky top-0">
<tr className="border-b">
{result.columns.map((col, i) => (
<th
key={i}
className="text-muted-foreground relative select-none truncate border-r px-2 py-1.5 text-left text-xs font-medium"
>
{col}
<div
className="absolute top-0 right-0 bottom-0 w-1.5 cursor-col-resize hover:bg-primary/30"
onMouseDown={(e) => onMouseDown(e, i)}
/>
</th>
))}
</tr>
</thead>
<tbody>
{isLoading ? (
<tr>
<td colSpan={columnCount} className="text-muted-foreground px-2 py-8 text-center text-sm">
Loading...
</td>
</tr>
) : result.rows.length === 0 ? (
<tr>
<td colSpan={columnCount} className="text-muted-foreground px-2 py-8 text-center text-sm">
No rows
</td>
</tr>
) : (
result.rows.map((row, rowIndex) => (
<tr key={rowIndex} className="hover:bg-muted/30 border-b transition-colors">
{(row as unknown[]).map((cell, colIndex) => {
const cellValue = formatCellValue(cell)
const cellKey = `${rowIndex}-${colIndex}`
const isCopied = copiedCell === cellKey
const isNull = cell === null || cell === undefined
return (
<td
key={colIndex}
className={cn(
'group relative cursor-pointer truncate border-r px-2 py-1 font-mono text-xs',
isNull && 'text-muted-foreground italic',
)}
title={cellValue}
onClick={() => handleCopy(cellValue, cellKey)}
>
{cellValue}
<span
className={cn(
'absolute top-1/2 right-1 -translate-y-1/2 opacity-0 transition-opacity',
isCopied ? 'opacity-100' : 'group-hover:opacity-60',
)}
>
{isCopied ? <Check className="text-green-500 size-3" /> : <Copy className="size-3" />}
</span>
</td>
)
})}
</tr>
))
)}
</tbody>
</table>
</div>
{/* Pagination */}
<div className="border-t bg-muted/30 flex items-center justify-between px-3 py-2">
<div className="text-muted-foreground text-xs">
{totalRows} row{totalRows !== 1 ? 's' : ''}
{totalPages > 1 && (
<span>
{' '}
· Page {page + 1} of {totalPages}
</span>
)}
</div>
<div className="flex items-center gap-2">
<Select value={String(pageSize)} onValueChange={(v) => onPageSizeChange(Number(v))}>
<SelectTrigger size="sm" className="h-7 w-auto gap-1 text-xs">
<SelectValue />
</SelectTrigger>
<SelectContent>
{pageSizes.map((size) => (
<SelectItem key={size} value={String(size)}>
{size} rows
</SelectItem>
))}
</SelectContent>
</Select>
<div className="flex items-center gap-1">
<Button
variant="ghost"
size="sm"
className="size-7 p-0"
disabled={!hasPrevPage}
onClick={() => onPageChange(page - 1)}
>
<ChevronLeft className="size-3.5" />
</Button>
<Button
variant="ghost"
size="sm"
className="size-7 p-0"
disabled={!hasNextPage}
onClick={() => onPageChange(page + 1)}
>
<ChevronRight className="size-3.5" />
</Button>
</div>
</div>
</div>
</div>
)
}