forked from frectonz/sql-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtables.tsx
More file actions
247 lines (220 loc) · 6.77 KB
/
Copy pathtables.tsx
File metadata and controls
247 lines (220 loc) · 6.77 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import "react-data-grid/lib/styles.css";
import {
HardDrive,
DatabaseZap,
TableProperties,
Table as TableIcon,
} from "lucide-react";
import { z } from "zod";
import { DataGrid } from "react-data-grid";
import { Link, createFileRoute } from "@tanstack/react-router";
import { useInfiniteQuery, useQuery } from "@tanstack/react-query";
import { CodeBlock, irBlack as CodeDarkTheme } from "react-code-blocks";
import { cn } from "@/lib/utils";
import {
Card,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
import { useTheme } from "@/provider/theme.provider";
import { fetchTable, fetchTableData, fetchTables } from "@/api";
import { InfoCard, InfoCardProps } from "@/components/info-card";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
export const Route = createFileRoute("/tables")({
component: Tables,
loader: () => fetchTables(),
pendingComponent: TablesSkeleton,
validateSearch: z.object({ table: z.string().optional() }),
});
function Tables() {
const data = Route.useLoaderData();
const { table } = Route.useSearch();
if (data.tables.length === 0)
return (
<Card>
<CardHeader className="flex items-center">
<TableIcon className="mb-4 h-12 w-12 text-muted-foreground" />
<CardTitle>No Tables Found</CardTitle>
<CardDescription>The database has no tables.</CardDescription>
</CardHeader>
</Card>
);
const requestedTableIndex = table
? data.tables.findIndex(({ name }) => name === table)
: -1;
const requestedTableMissing = !!table && requestedTableIndex < 0;
const tab = String(Math.max(requestedTableIndex, 0));
return (
<>
{requestedTableMissing && (
<Card className="mb-3">
<CardHeader>
<CardTitle>Table not found</CardTitle>
<CardDescription>
Could not find "{table}". Showing the first available table
instead.
</CardDescription>
</CardHeader>
</Card>
)}
<Tabs key={tab} defaultValue={tab}>
<TabsList>
{data.tables.map((n, i) => (
<TabsTrigger key={i} value={i.toString()}>
<Link to="/tables" search={{ table: n.name }}>
{n.name} [{n.count.toLocaleString()}]
</Link>
</TabsTrigger>
))}
</TabsList>
{data.tables.map(({ name }, i) => (
<TabsContent key={i} value={i.toString()} className="py-4">
<Table name={name} />
</TabsContent>
))}
</Tabs>
</>
);
}
function TablesSkeleton() {
return <Skeleton className="w-[70vw] h-[30px]" />;
}
type Props = {
name: string;
};
function Table({ name }: Props) {
const currentTheme = useTheme();
const { data } = useQuery({
queryKey: ["tables", name],
queryFn: () => fetchTable(name),
});
if (!data) return <TableSkeleton />;
const cards: InfoCardProps[] = [
{
title: "ROW COUNT",
value: data.row_count.toLocaleString(),
description: "The number of rows in the table.",
icon: TableIcon,
},
{
title: "INDEXES",
value: data.index_count.toLocaleString(),
description: "The number of indexes in the table.",
icon: DatabaseZap,
},
{
title: "COLUMNS",
value: data.column_count.toLocaleString(),
description: "The number of columns in the table.",
icon: TableProperties,
},
{
title: "TABLE SIZE",
value: data.table_size,
description: "The size of the table on disk.",
icon: HardDrive,
},
];
return (
<div className="flex flex-1 flex-col gap-4 md:gap-8">
<h2 className="px-2 text-foreground scroll-m-20 border-b pb-2 text-3xl font-semibold tracking-tight first:mt-0">
{data.name}
</h2>
<div className="grid gap-4 md:grid-cols-2 md:gap-8 lg:grid-cols-4">
{cards.map((card, i) => (
<InfoCard
key={i}
title={card.title}
value={card.value}
description={card.description}
icon={card.icon}
/>
))}
</div>
{data.sql && (
<Card className="font-mono text-sm">
<CodeBlock
text={data.sql}
language="sql"
theme={currentTheme === "dark" ? CodeDarkTheme : undefined}
showLineNumbers={false}
customStyle={{
FontFace: "JetBrains Mono",
padding: "10px",
backgroundColor: currentTheme === "dark" ? "#091813" : "#f5faf9",
borderRadius: "10px",
}}
/>
</Card>
)}
<Card className="p-2">
<TableData name={data.name} />
</Card>
</div>
);
}
function TableSkeleton() {
return (
<div className="flex flex-1 flex-col gap-4 md:gap-8">
<div className="flex flex-col gap-2">
<Skeleton className="w-[50vw] h-[50px]" />
<span className="border-b" />
</div>
<div className="grid gap-4 md:grid-cols-2 md:gap-8 lg:grid-cols-4">
<Skeleton className="h-[100px]" />
<Skeleton className="h-[100px]" />
<Skeleton className="h-[100px]" />
<Skeleton className="h-[100px]" />
</div>
<Skeleton className="h-[400px]" />
<Skeleton className="h-[400px]" />
</div>
);
}
function isAtBottom({ currentTarget }: React.UIEvent<HTMLDivElement>): boolean {
return (
currentTarget.scrollTop + 10 >=
currentTarget.scrollHeight - currentTarget.clientHeight
);
}
type TableDataProps = {
name: string;
};
function TableData({ name }: TableDataProps) {
const currentTheme = useTheme();
const { isLoading, data, fetchNextPage, hasNextPage } = useInfiniteQuery({
queryKey: ["tables", "data", name],
queryFn: ({ pageParam }) => fetchTableData(name, pageParam),
initialPageParam: 1,
getNextPageParam: (lastPage, _, lastPageParams) => {
if (lastPage.rows.length === 0) return undefined;
return lastPageParams + 1;
},
});
if (!data) return <Skeleton className="h-[400px]" />;
function handleScroll(event: React.UIEvent<HTMLDivElement>) {
if (isLoading || !isAtBottom(event) || !hasNextPage) return;
fetchNextPage();
}
const columns = data.pages[0].columns.map((col) => ({ key: col, name: col }));
const grouped = data.pages.map((page) =>
page.rows.map((row) =>
row.reduce((acc, curr, i) => {
acc[page.columns[i]] = curr;
return acc;
}, {}),
),
) as never[][];
const rows = [].concat(...grouped);
return (
<DataGrid
rows={rows}
columns={columns}
onScroll={handleScroll}
defaultColumnOptions={{ resizable: true }}
className={cn(currentTheme === "light" ? "rdg-light" : "rdg-dark")}
/>
);
}