@sqlrooms/sql-editor 0.0.0
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/.turbo/turbo-build.log +4 -0
- package/.turbo/turbo-dev.log +7 -0
- package/.turbo/turbo-lint.log +16 -0
- package/CHANGELOG.md +8 -0
- package/LICENSE.md +9 -0
- package/dist/CreateTableModal.d.ts +12 -0
- package/dist/CreateTableModal.d.ts.map +1 -0
- package/dist/CreateTableModal.js +51 -0
- package/dist/DeleteSqlQueryModal.d.ts +9 -0
- package/dist/DeleteSqlQueryModal.d.ts.map +1 -0
- package/dist/DeleteSqlQueryModal.js +6 -0
- package/dist/RenameSqlQueryModal.d.ts +10 -0
- package/dist/RenameSqlQueryModal.d.ts.map +1 -0
- package/dist/RenameSqlQueryModal.js +26 -0
- package/dist/SqlEditor.d.ts +15 -0
- package/dist/SqlEditor.d.ts.map +1 -0
- package/dist/SqlEditor.js +206 -0
- package/dist/SqlEditorModal.d.ts +5 -0
- package/dist/SqlEditorModal.d.ts.map +1 -0
- package/dist/SqlEditorModal.js +11 -0
- package/dist/TablesList.d.ts +12 -0
- package/dist/TablesList.d.ts.map +1 -0
- package/dist/TablesList.js +8 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/eslint.config.js +4 -0
- package/package.json +41 -0
- package/src/CreateTableModal.tsx +161 -0
- package/src/DeleteSqlQueryModal.tsx +42 -0
- package/src/RenameSqlQueryModal.tsx +92 -0
- package/src/SqlEditor.tsx +495 -0
- package/src/SqlEditorModal.tsx +31 -0
- package/src/TablesList.tsx +50 -0
- package/src/index.ts +4 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DataTableVirtualized,
|
|
3
|
+
QueryDataTable,
|
|
4
|
+
useArrowDataTable,
|
|
5
|
+
} from '@sqlrooms/data-table';
|
|
6
|
+
import {
|
|
7
|
+
DuckQueryError,
|
|
8
|
+
escapeId,
|
|
9
|
+
getDuckTables,
|
|
10
|
+
useDuckDb,
|
|
11
|
+
} from '@sqlrooms/duckdb';
|
|
12
|
+
import {SqlEditorConfig} from '@sqlrooms/project-config';
|
|
13
|
+
import {
|
|
14
|
+
Button,
|
|
15
|
+
DropdownMenu,
|
|
16
|
+
DropdownMenuContent,
|
|
17
|
+
DropdownMenuItem,
|
|
18
|
+
DropdownMenuTrigger,
|
|
19
|
+
SpinnerPane,
|
|
20
|
+
Tabs,
|
|
21
|
+
TabsContent,
|
|
22
|
+
TabsList,
|
|
23
|
+
TabsTrigger,
|
|
24
|
+
Textarea,
|
|
25
|
+
ResizablePanelGroup,
|
|
26
|
+
ResizablePanel,
|
|
27
|
+
ResizableHandle,
|
|
28
|
+
} from '@sqlrooms/ui';
|
|
29
|
+
import {genRandomStr, generateUniqueName} from '@sqlrooms/utils';
|
|
30
|
+
import {Table} from 'apache-arrow';
|
|
31
|
+
import {csvFormat} from 'd3-dsv';
|
|
32
|
+
import {saveAs} from 'file-saver';
|
|
33
|
+
import {
|
|
34
|
+
BookOpenIcon,
|
|
35
|
+
DownloadIcon,
|
|
36
|
+
MoreVerticalIcon,
|
|
37
|
+
PlayIcon,
|
|
38
|
+
PlusIcon,
|
|
39
|
+
} from 'lucide-react';
|
|
40
|
+
import React, {useCallback, useEffect, useRef, useState} from 'react';
|
|
41
|
+
import CreateTableModal, {
|
|
42
|
+
Props as CreateTableModalProps,
|
|
43
|
+
} from './CreateTableModal';
|
|
44
|
+
import DeleteSqlQueryModal from './DeleteSqlQueryModal';
|
|
45
|
+
import RenameSqlQueryModal from './RenameSqlQueryModal';
|
|
46
|
+
import {TablesList} from './TablesList';
|
|
47
|
+
|
|
48
|
+
const DEFAULT_QUERY = '';
|
|
49
|
+
|
|
50
|
+
export type Props = {
|
|
51
|
+
schema?: string;
|
|
52
|
+
isOpen: boolean;
|
|
53
|
+
documentationPanel?: React.ReactNode;
|
|
54
|
+
sqlEditorConfig: SqlEditorConfig;
|
|
55
|
+
onChange: (config: SqlEditorConfig) => void;
|
|
56
|
+
onClose: () => void;
|
|
57
|
+
onAddOrUpdateSqlQuery: CreateTableModalProps['onAddOrUpdateSqlQuery'];
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const SqlEditor: React.FC<Props> = (props) => {
|
|
61
|
+
const {
|
|
62
|
+
schema = 'main',
|
|
63
|
+
documentationPanel,
|
|
64
|
+
onAddOrUpdateSqlQuery,
|
|
65
|
+
sqlEditorConfig,
|
|
66
|
+
onChange,
|
|
67
|
+
} = props;
|
|
68
|
+
const duckConn = useDuckDb();
|
|
69
|
+
|
|
70
|
+
const [showDocs, setShowDocs] = useState(false);
|
|
71
|
+
const [tables, setTables] = useState<string[]>([]);
|
|
72
|
+
const [tablesLoading, setTablesLoading] = useState(false);
|
|
73
|
+
const [tablesError, setTablesError] = useState<Error | null>(null);
|
|
74
|
+
|
|
75
|
+
const [results, setResults] = useState<Table>();
|
|
76
|
+
const resultsTableData = useArrowDataTable(results);
|
|
77
|
+
const [loading, setLoading] = useState(false);
|
|
78
|
+
const [selectedTable, setSelectedTable] = useState<string>();
|
|
79
|
+
|
|
80
|
+
const [error, setError] = useState<string | null>(null);
|
|
81
|
+
|
|
82
|
+
const fetchTables = useCallback(async () => {
|
|
83
|
+
if (!duckConn.conn) return;
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
setTablesLoading(true);
|
|
87
|
+
setTablesError(null);
|
|
88
|
+
const tablesList = await getDuckTables(schema);
|
|
89
|
+
setTables(tablesList);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
console.error(e);
|
|
92
|
+
setTablesError(e as Error);
|
|
93
|
+
} finally {
|
|
94
|
+
setTablesLoading(false);
|
|
95
|
+
}
|
|
96
|
+
}, [duckConn.conn, schema]);
|
|
97
|
+
|
|
98
|
+
useEffect(() => {
|
|
99
|
+
void fetchTables();
|
|
100
|
+
}, [fetchTables]);
|
|
101
|
+
|
|
102
|
+
const runQuery = async (q: string) => {
|
|
103
|
+
const conn = duckConn.conn;
|
|
104
|
+
try {
|
|
105
|
+
setError(null);
|
|
106
|
+
setLoading(true);
|
|
107
|
+
await conn.query(`SET search_path = ${schema}`);
|
|
108
|
+
const results = await conn.query(q);
|
|
109
|
+
await conn.query(`SET search_path = main`);
|
|
110
|
+
setResults(results);
|
|
111
|
+
} catch (e) {
|
|
112
|
+
setResults(undefined);
|
|
113
|
+
setError(
|
|
114
|
+
(e instanceof DuckQueryError
|
|
115
|
+
? e.getMessageForUser()
|
|
116
|
+
: 'Query failed') ?? e,
|
|
117
|
+
);
|
|
118
|
+
console.error(e);
|
|
119
|
+
} finally {
|
|
120
|
+
setLoading(false);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const handleSelectTable = (table: string) => {
|
|
125
|
+
setSelectedTable(table);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const handleRunQuery = async () => {
|
|
129
|
+
setSelectedTable(undefined);
|
|
130
|
+
const textarea = document.querySelector(
|
|
131
|
+
`textarea[id="${sqlEditorConfig.selectedQueryId}"]`,
|
|
132
|
+
);
|
|
133
|
+
const selectedText =
|
|
134
|
+
textarea instanceof HTMLTextAreaElement
|
|
135
|
+
? textarea?.value.substring(
|
|
136
|
+
textarea.selectionStart,
|
|
137
|
+
textarea.selectionEnd,
|
|
138
|
+
)
|
|
139
|
+
: undefined;
|
|
140
|
+
|
|
141
|
+
const queryToRun = selectedText || currentQuery;
|
|
142
|
+
await runQuery(queryToRun);
|
|
143
|
+
void fetchTables();
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const getQueryIndexById = (id: string) => {
|
|
147
|
+
return sqlEditorConfig.queries.findIndex((q) => q.id === id);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const getCurrentQueryIndex = () => {
|
|
151
|
+
return getQueryIndexById(sqlEditorConfig.selectedQueryId);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const handleTabChange = (value: string) => {
|
|
155
|
+
onChange({
|
|
156
|
+
...sqlEditorConfig,
|
|
157
|
+
selectedQueryId: value,
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
const handleUpdateQuery = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
|
|
162
|
+
if (!sqlEditorConfig) return;
|
|
163
|
+
|
|
164
|
+
const currentIndex = getCurrentQueryIndex();
|
|
165
|
+
const newQueries = [...sqlEditorConfig.queries];
|
|
166
|
+
if (!newQueries[currentIndex]) return;
|
|
167
|
+
newQueries[currentIndex] = {
|
|
168
|
+
...newQueries[currentIndex],
|
|
169
|
+
query: e.target.value,
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
onChange({
|
|
173
|
+
...sqlEditorConfig,
|
|
174
|
+
queries: newQueries,
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
const handleRunQueryRef = useRef(handleRunQuery);
|
|
178
|
+
handleRunQueryRef.current = handleRunQuery;
|
|
179
|
+
|
|
180
|
+
useEffect(() => {
|
|
181
|
+
const handleKeyDown = (evt: Event) => {
|
|
182
|
+
if (
|
|
183
|
+
evt instanceof KeyboardEvent &&
|
|
184
|
+
evt.key === 'Enter' &&
|
|
185
|
+
(evt.metaKey || evt.ctrlKey || evt.shiftKey)
|
|
186
|
+
) {
|
|
187
|
+
void handleRunQueryRef.current();
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
globalThis.addEventListener('keydown', handleKeyDown);
|
|
191
|
+
return () => {
|
|
192
|
+
globalThis.removeEventListener('keydown', handleKeyDown);
|
|
193
|
+
};
|
|
194
|
+
}, []);
|
|
195
|
+
|
|
196
|
+
const handleExport = () => {
|
|
197
|
+
if (!results) return;
|
|
198
|
+
const blob = new Blob([csvFormat(results.toArray())], {
|
|
199
|
+
type: 'text/plain;charset=utf-8',
|
|
200
|
+
});
|
|
201
|
+
saveAs(blob, `export-${genRandomStr(5)}.csv`);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const [createTableModalOpen, setCreateTableModalOpen] = useState(false);
|
|
205
|
+
|
|
206
|
+
const handleCreateTable = useCallback(() => {
|
|
207
|
+
setCreateTableModalOpen(true);
|
|
208
|
+
}, []);
|
|
209
|
+
|
|
210
|
+
const handleToggleDocs = useCallback(() => {
|
|
211
|
+
setShowDocs(!showDocs);
|
|
212
|
+
}, [showDocs]);
|
|
213
|
+
|
|
214
|
+
const currentQuery =
|
|
215
|
+
sqlEditorConfig.queries[getCurrentQueryIndex()]?.query ?? DEFAULT_QUERY;
|
|
216
|
+
|
|
217
|
+
const [queryToDelete, setQueryToDelete] = useState<string | null>(null);
|
|
218
|
+
|
|
219
|
+
const [queryToRename, setQueryToRename] = useState<{
|
|
220
|
+
id: string;
|
|
221
|
+
name: string;
|
|
222
|
+
} | null>(null);
|
|
223
|
+
|
|
224
|
+
const handleStartRename = (
|
|
225
|
+
queryId: string,
|
|
226
|
+
currentName: string,
|
|
227
|
+
event: React.MouseEvent,
|
|
228
|
+
) => {
|
|
229
|
+
event.preventDefault();
|
|
230
|
+
setQueryToRename({id: queryId, name: currentName});
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const handleFinishRename = (newName: string) => {
|
|
234
|
+
if (queryToRename) {
|
|
235
|
+
const newQueries = sqlEditorConfig.queries.map((q) =>
|
|
236
|
+
q.id === queryToRename.id ? {...q, name: newName || q.name} : q,
|
|
237
|
+
);
|
|
238
|
+
onChange({
|
|
239
|
+
...sqlEditorConfig,
|
|
240
|
+
queries: newQueries,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
setQueryToRename(null);
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const handleDeleteQuery = (queryId: string, event: React.MouseEvent) => {
|
|
247
|
+
event.stopPropagation();
|
|
248
|
+
const currentIndex = getQueryIndexById(queryId);
|
|
249
|
+
setQueryToDelete(queryId);
|
|
250
|
+
|
|
251
|
+
// Pre-select the previous query if we're deleting the current one
|
|
252
|
+
if (queryId === sqlEditorConfig.selectedQueryId && currentIndex > 0) {
|
|
253
|
+
const prevId = sqlEditorConfig.queries[currentIndex - 1]?.id;
|
|
254
|
+
if (prevId) {
|
|
255
|
+
onChange({
|
|
256
|
+
...sqlEditorConfig,
|
|
257
|
+
selectedQueryId: prevId,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const handleNewQuery = () => {
|
|
264
|
+
const newQueries = [...sqlEditorConfig.queries];
|
|
265
|
+
const newQuery = {
|
|
266
|
+
id: genRandomStr(8),
|
|
267
|
+
name: generateUniqueName(
|
|
268
|
+
'Untitled',
|
|
269
|
+
newQueries.map((q) => q.name),
|
|
270
|
+
),
|
|
271
|
+
query: DEFAULT_QUERY,
|
|
272
|
+
};
|
|
273
|
+
newQueries.push(newQuery);
|
|
274
|
+
|
|
275
|
+
onChange({
|
|
276
|
+
...sqlEditorConfig,
|
|
277
|
+
queries: newQueries,
|
|
278
|
+
selectedQueryId: newQuery.id,
|
|
279
|
+
});
|
|
280
|
+
};
|
|
281
|
+
|
|
282
|
+
return (
|
|
283
|
+
<>
|
|
284
|
+
<div className="absolute right-12">
|
|
285
|
+
<Button
|
|
286
|
+
size="sm"
|
|
287
|
+
variant={showDocs ? 'secondary' : 'outline'}
|
|
288
|
+
onClick={handleToggleDocs}
|
|
289
|
+
>
|
|
290
|
+
<BookOpenIcon className="w-4 h-4 mr-2" />
|
|
291
|
+
SQL reference
|
|
292
|
+
</Button>
|
|
293
|
+
</div>
|
|
294
|
+
<div className="flex flex-col w-full gap-2">
|
|
295
|
+
<div className="flex items-center gap-2 ml-1 mr-10 mb-2">
|
|
296
|
+
<h2 className="text-lg font-semibold">SQL Editor</h2>
|
|
297
|
+
</div>
|
|
298
|
+
<div className="flex-grow h-full bg-muted">
|
|
299
|
+
<ResizablePanelGroup direction="horizontal" className="h-full">
|
|
300
|
+
<ResizablePanel defaultSize={20}>
|
|
301
|
+
{tablesLoading ? (
|
|
302
|
+
<SpinnerPane h="100%" />
|
|
303
|
+
) : tablesError ? (
|
|
304
|
+
<div className="p-4 text-red-500">
|
|
305
|
+
Error loading tables: {tablesError.message}
|
|
306
|
+
</div>
|
|
307
|
+
) : (
|
|
308
|
+
<TablesList
|
|
309
|
+
schema="information_schema"
|
|
310
|
+
tableNames={tables}
|
|
311
|
+
selectedTable={selectedTable}
|
|
312
|
+
onSelect={handleSelectTable}
|
|
313
|
+
/>
|
|
314
|
+
)}
|
|
315
|
+
</ResizablePanel>
|
|
316
|
+
<ResizableHandle withHandle />
|
|
317
|
+
<ResizablePanel defaultSize={showDocs ? 50 : 80}>
|
|
318
|
+
<ResizablePanelGroup direction="vertical" className="h-full">
|
|
319
|
+
<ResizablePanel defaultSize={50}>
|
|
320
|
+
<div className="flex flex-col h-full gap-2">
|
|
321
|
+
<Tabs
|
|
322
|
+
value={sqlEditorConfig.selectedQueryId}
|
|
323
|
+
onValueChange={handleTabChange}
|
|
324
|
+
className="flex flex-col flex-grow overflow-hidden"
|
|
325
|
+
>
|
|
326
|
+
<div className="flex items-center gap-2 border-b border-border">
|
|
327
|
+
<Button
|
|
328
|
+
size="sm"
|
|
329
|
+
onClick={() => void handleRunQuery()}
|
|
330
|
+
className="uppercase"
|
|
331
|
+
>
|
|
332
|
+
<PlayIcon className="w-4 h-4 mr-2" />
|
|
333
|
+
Run
|
|
334
|
+
</Button>
|
|
335
|
+
<TabsList className="flex-1">
|
|
336
|
+
{sqlEditorConfig.queries.map((q) => (
|
|
337
|
+
<div key={q.id} className="relative">
|
|
338
|
+
<TabsTrigger
|
|
339
|
+
value={q.id}
|
|
340
|
+
className="min-w-[60px] px-6 pr-8"
|
|
341
|
+
>
|
|
342
|
+
{q.name}
|
|
343
|
+
</TabsTrigger>
|
|
344
|
+
<DropdownMenu>
|
|
345
|
+
<DropdownMenuTrigger asChild>
|
|
346
|
+
<div
|
|
347
|
+
className="absolute right-0 top-1/2 -translate-y-1/2 h-6 w-6 flex items-center justify-center cursor-pointer hover:bg-accent rounded-sm"
|
|
348
|
+
onClick={(e) => e.stopPropagation()}
|
|
349
|
+
>
|
|
350
|
+
<MoreVerticalIcon className="h-3 w-3" />
|
|
351
|
+
</div>
|
|
352
|
+
</DropdownMenuTrigger>
|
|
353
|
+
<DropdownMenuContent>
|
|
354
|
+
<DropdownMenuItem
|
|
355
|
+
onClick={(e) => {
|
|
356
|
+
e.stopPropagation();
|
|
357
|
+
handleStartRename(q.id, q.name, e);
|
|
358
|
+
}}
|
|
359
|
+
>
|
|
360
|
+
Rename
|
|
361
|
+
</DropdownMenuItem>
|
|
362
|
+
{sqlEditorConfig.queries.length > 1 && (
|
|
363
|
+
<DropdownMenuItem
|
|
364
|
+
onClick={(e) => {
|
|
365
|
+
e.stopPropagation();
|
|
366
|
+
handleDeleteQuery(q.id, e);
|
|
367
|
+
}}
|
|
368
|
+
className="text-red-500"
|
|
369
|
+
>
|
|
370
|
+
Delete
|
|
371
|
+
</DropdownMenuItem>
|
|
372
|
+
)}
|
|
373
|
+
</DropdownMenuContent>
|
|
374
|
+
</DropdownMenu>
|
|
375
|
+
</div>
|
|
376
|
+
))}
|
|
377
|
+
</TabsList>
|
|
378
|
+
<Button
|
|
379
|
+
size="icon"
|
|
380
|
+
variant="ghost"
|
|
381
|
+
onClick={handleNewQuery}
|
|
382
|
+
className="ml-2"
|
|
383
|
+
>
|
|
384
|
+
<PlusIcon className="h-4 w-4" />
|
|
385
|
+
</Button>
|
|
386
|
+
</div>
|
|
387
|
+
{sqlEditorConfig.queries.map((q) => (
|
|
388
|
+
<TabsContent
|
|
389
|
+
key={q.id}
|
|
390
|
+
value={q.id}
|
|
391
|
+
className="flex-grow data-[state=active]:flex-grow"
|
|
392
|
+
>
|
|
393
|
+
<Textarea
|
|
394
|
+
id={q.id}
|
|
395
|
+
value={q.query}
|
|
396
|
+
onChange={handleUpdateQuery}
|
|
397
|
+
className="h-full font-mono text-sm resize-none bg-muted"
|
|
398
|
+
/>
|
|
399
|
+
</TabsContent>
|
|
400
|
+
))}
|
|
401
|
+
</Tabs>
|
|
402
|
+
</div>
|
|
403
|
+
</ResizablePanel>
|
|
404
|
+
<ResizableHandle withHandle />
|
|
405
|
+
<ResizablePanel defaultSize={50}>
|
|
406
|
+
<div className="h-full overflow-hidden bg-muted text-sm">
|
|
407
|
+
{loading ? (
|
|
408
|
+
<SpinnerPane h="100%" />
|
|
409
|
+
) : selectedTable ? (
|
|
410
|
+
<QueryDataTable
|
|
411
|
+
query={`SELECT * FROM ${schema}.${escapeId(selectedTable)}`}
|
|
412
|
+
/>
|
|
413
|
+
) : error ? (
|
|
414
|
+
<div className="w-full h-full p-5 overflow-auto">
|
|
415
|
+
<pre className="text-xs leading-tight text-red-500">
|
|
416
|
+
{error}
|
|
417
|
+
</pre>
|
|
418
|
+
</div>
|
|
419
|
+
) : resultsTableData ? (
|
|
420
|
+
<div className="flex-grow overflow-hidden flex flex-col relative">
|
|
421
|
+
<DataTableVirtualized {...resultsTableData} />
|
|
422
|
+
<div className="absolute bottom-0 right-0 flex gap-2 p-2">
|
|
423
|
+
<Button
|
|
424
|
+
size="sm"
|
|
425
|
+
disabled={!resultsTableData}
|
|
426
|
+
onClick={handleCreateTable}
|
|
427
|
+
>
|
|
428
|
+
<PlusIcon className="w-4 h-4 mr-2" />
|
|
429
|
+
Create table
|
|
430
|
+
</Button>
|
|
431
|
+
<Button
|
|
432
|
+
size="sm"
|
|
433
|
+
disabled={!results}
|
|
434
|
+
onClick={handleExport}
|
|
435
|
+
>
|
|
436
|
+
<DownloadIcon className="w-4 h-4 mr-2" />
|
|
437
|
+
Export
|
|
438
|
+
</Button>
|
|
439
|
+
</div>
|
|
440
|
+
</div>
|
|
441
|
+
) : null}
|
|
442
|
+
</div>
|
|
443
|
+
</ResizablePanel>
|
|
444
|
+
</ResizablePanelGroup>
|
|
445
|
+
</ResizablePanel>
|
|
446
|
+
{showDocs && (
|
|
447
|
+
<>
|
|
448
|
+
<ResizableHandle withHandle />
|
|
449
|
+
<ResizablePanel defaultSize={30}>
|
|
450
|
+
{documentationPanel}
|
|
451
|
+
</ResizablePanel>
|
|
452
|
+
</>
|
|
453
|
+
)}
|
|
454
|
+
</ResizablePanelGroup>
|
|
455
|
+
</div>
|
|
456
|
+
<CreateTableModal
|
|
457
|
+
query={currentQuery}
|
|
458
|
+
isOpen={createTableModalOpen}
|
|
459
|
+
onClose={() => setCreateTableModalOpen(false)}
|
|
460
|
+
onAddOrUpdateSqlQuery={onAddOrUpdateSqlQuery}
|
|
461
|
+
/>
|
|
462
|
+
<DeleteSqlQueryModal
|
|
463
|
+
isOpen={queryToDelete !== null}
|
|
464
|
+
onClose={() => setQueryToDelete(null)}
|
|
465
|
+
onConfirm={() => {
|
|
466
|
+
const newQueries = sqlEditorConfig.queries.filter(
|
|
467
|
+
(q) => q.id !== queryToDelete,
|
|
468
|
+
);
|
|
469
|
+
const deletedIndex = getQueryIndexById(queryToDelete!);
|
|
470
|
+
|
|
471
|
+
const selectedQueryId =
|
|
472
|
+
newQueries[Math.min(deletedIndex, newQueries.length - 1)]?.id ||
|
|
473
|
+
newQueries[0]?.id;
|
|
474
|
+
if (selectedQueryId) {
|
|
475
|
+
onChange({
|
|
476
|
+
...sqlEditorConfig,
|
|
477
|
+
queries: newQueries,
|
|
478
|
+
selectedQueryId,
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
setQueryToDelete(null);
|
|
482
|
+
}}
|
|
483
|
+
/>
|
|
484
|
+
<RenameSqlQueryModal
|
|
485
|
+
isOpen={queryToRename !== null}
|
|
486
|
+
onClose={() => setQueryToRename(null)}
|
|
487
|
+
initialName={queryToRename?.name ?? ''}
|
|
488
|
+
onRename={handleFinishRename}
|
|
489
|
+
/>
|
|
490
|
+
</div>
|
|
491
|
+
</>
|
|
492
|
+
);
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
export default SqlEditor;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import {SpinnerPane} from '@sqlrooms/ui';
|
|
4
|
+
import {
|
|
5
|
+
Dialog,
|
|
6
|
+
DialogContent,
|
|
7
|
+
DialogHeader,
|
|
8
|
+
DialogOverlay,
|
|
9
|
+
DialogTitle,
|
|
10
|
+
} from '@sqlrooms/ui';
|
|
11
|
+
import React, {Suspense} from 'react';
|
|
12
|
+
import SqlEditor, {Props} from './SqlEditor';
|
|
13
|
+
|
|
14
|
+
const SqlEditorModal: React.FC<Props> = (props) => {
|
|
15
|
+
const {isOpen, onClose} = props;
|
|
16
|
+
return (
|
|
17
|
+
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
|
|
18
|
+
<DialogOverlay className="bg-background/80" />
|
|
19
|
+
<DialogContent className="max-w-[100vw] max-h-[100vh] w-[100vw] h-[100vh] p-3">
|
|
20
|
+
<DialogHeader className="sr-only">
|
|
21
|
+
<DialogTitle>SQL Editor</DialogTitle>
|
|
22
|
+
</DialogHeader>
|
|
23
|
+
<Suspense fallback={<SpinnerPane h="100%" />}>
|
|
24
|
+
<SqlEditor {...props} />
|
|
25
|
+
</Suspense>
|
|
26
|
+
</DialogContent>
|
|
27
|
+
</Dialog>
|
|
28
|
+
);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default SqlEditorModal;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import {Button} from '@sqlrooms/ui';
|
|
2
|
+
import {TableIcon} from 'lucide-react';
|
|
3
|
+
import type {FC} from 'react';
|
|
4
|
+
|
|
5
|
+
type Props = {
|
|
6
|
+
schema: string;
|
|
7
|
+
tableNames: string[];
|
|
8
|
+
selectedTable?: string;
|
|
9
|
+
onSelect: (name: string) => void;
|
|
10
|
+
onChange?: () => void;
|
|
11
|
+
renderTableButton?: (
|
|
12
|
+
tableName: string,
|
|
13
|
+
onSelect: Props['onSelect'],
|
|
14
|
+
) => React.ReactNode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const TablesList: FC<Props> = (props) => {
|
|
18
|
+
const {
|
|
19
|
+
tableNames,
|
|
20
|
+
selectedTable,
|
|
21
|
+
onSelect,
|
|
22
|
+
renderTableButton = (tableName: string, onSelect: Props['onSelect']) => (
|
|
23
|
+
<Button
|
|
24
|
+
className="w-full justify-start font-normal overflow-hidden whitespace-normal min-h-[25px] text-sm text-left break-words select-text"
|
|
25
|
+
variant={selectedTable === tableName ? 'secondary' : 'ghost'}
|
|
26
|
+
size="sm"
|
|
27
|
+
onClick={() => onSelect(tableName)}
|
|
28
|
+
>
|
|
29
|
+
<TableIcon className="h-4 w-4" />
|
|
30
|
+
{tableName}
|
|
31
|
+
</Button>
|
|
32
|
+
),
|
|
33
|
+
} = props;
|
|
34
|
+
|
|
35
|
+
return (
|
|
36
|
+
<div className="h-full bg-background/10 px-2 py-4 overflow-auto">
|
|
37
|
+
<ul className="space-y-1">
|
|
38
|
+
{tableNames.map((tableName, i) => (
|
|
39
|
+
<li key={i}>
|
|
40
|
+
<div className="flex items-center gap-1">
|
|
41
|
+
{renderTableButton(tableName, onSelect)}
|
|
42
|
+
</div>
|
|
43
|
+
</li>
|
|
44
|
+
))}
|
|
45
|
+
</ul>
|
|
46
|
+
</div>
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export {TablesList};
|
package/src/index.ts
ADDED