@sqlrooms/sql-editor 0.0.0 → 0.0.1-alpha.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/CHANGELOG.md +2 -0
- package/package.json +9 -9
- package/.turbo/turbo-build.log +0 -4
- package/.turbo/turbo-dev.log +0 -7
- package/.turbo/turbo-lint.log +0 -16
- package/eslint.config.js +0 -4
- package/src/CreateTableModal.tsx +0 -161
- package/src/DeleteSqlQueryModal.tsx +0 -42
- package/src/RenameSqlQueryModal.tsx +0 -92
- package/src/SqlEditor.tsx +0 -495
- package/src/SqlEditorModal.tsx +0 -31
- package/src/TablesList.tsx +0 -50
- package/src/index.ts +0 -4
- package/tsconfig.json +0 -8
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## 0.0.1-alpha.0 (2025-01-30)
|
|
6
7
|
|
|
8
|
+
**Note:** Version bump only for package @sqlrooms/sql-editor
|
|
7
9
|
|
|
8
10
|
**Note:** Version bump only for package @sqlrooms/sql-editor
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqlrooms/sql-editor",
|
|
3
|
-
"version": "0.0.0",
|
|
3
|
+
"version": "0.0.1-alpha.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"@hookform/resolvers": "^3.10.0",
|
|
19
|
-
"@sqlrooms/data-table": "0.0.0",
|
|
20
|
-
"@sqlrooms/duckdb": "0.0.0",
|
|
21
|
-
"@sqlrooms/layout": "0.0.0",
|
|
22
|
-
"@sqlrooms/project-config": "0.0.0",
|
|
23
|
-
"@sqlrooms/ui": "0.0.0",
|
|
24
|
-
"@sqlrooms/utils": "0.0.0",
|
|
25
|
-
"apache-arrow": "^
|
|
19
|
+
"@sqlrooms/data-table": "0.0.1-alpha.0",
|
|
20
|
+
"@sqlrooms/duckdb": "0.0.1-alpha.0",
|
|
21
|
+
"@sqlrooms/layout": "0.0.1-alpha.0",
|
|
22
|
+
"@sqlrooms/project-config": "0.0.1-alpha.0",
|
|
23
|
+
"@sqlrooms/ui": "0.0.1-alpha.0",
|
|
24
|
+
"@sqlrooms/utils": "0.0.1-alpha.0",
|
|
25
|
+
"apache-arrow": "^18.1.0",
|
|
26
26
|
"d3-dsv": "^3.0.1",
|
|
27
27
|
"file-saver": "^2.0.5",
|
|
28
28
|
"lucide-react": "^0.323.0",
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"@types/react": "^18.2.48",
|
|
38
38
|
"@types/react-dom": "^18.2.18"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "8e3c0b884db7755c8b892a590af49c0081336d8d"
|
|
41
41
|
}
|
package/.turbo/turbo-build.log
DELETED
package/.turbo/turbo-dev.log
DELETED
package/.turbo/turbo-lint.log
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @sqlrooms/sql-editor@0.0.0 lint /Users/ilya/Workspace/sqlrooms/packages/sql-editor
|
|
3
|
-
> eslint .
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
/Users/ilya/Workspace/sqlrooms/packages/sql-editor/src/CreateTableModal.tsx
|
|
7
|
-
89:10 warning Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free @typescript-eslint/ban-ts-comment
|
|
8
|
-
114:15 warning Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free @typescript-eslint/ban-ts-comment
|
|
9
|
-
129:15 warning Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free @typescript-eslint/ban-ts-comment
|
|
10
|
-
|
|
11
|
-
/Users/ilya/Workspace/sqlrooms/packages/sql-editor/src/RenameSqlQueryModal.tsx
|
|
12
|
-
58:10 warning Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free @typescript-eslint/ban-ts-comment
|
|
13
|
-
62:15 warning Use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free @typescript-eslint/ban-ts-comment
|
|
14
|
-
|
|
15
|
-
✖ 5 problems (0 errors, 5 warnings)
|
|
16
|
-
|
package/eslint.config.js
DELETED
package/src/CreateTableModal.tsx
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Button,
|
|
3
|
-
Dialog,
|
|
4
|
-
DialogContent,
|
|
5
|
-
DialogDescription,
|
|
6
|
-
DialogFooter,
|
|
7
|
-
DialogHeader,
|
|
8
|
-
DialogTitle,
|
|
9
|
-
Form,
|
|
10
|
-
FormControl,
|
|
11
|
-
FormField,
|
|
12
|
-
FormItem,
|
|
13
|
-
FormLabel,
|
|
14
|
-
FormMessage,
|
|
15
|
-
Input,
|
|
16
|
-
Textarea,
|
|
17
|
-
Alert,
|
|
18
|
-
AlertDescription,
|
|
19
|
-
} from '@sqlrooms/ui';
|
|
20
|
-
import {DuckQueryError} from '@sqlrooms/duckdb';
|
|
21
|
-
import {
|
|
22
|
-
SqlQueryDataSource,
|
|
23
|
-
VALID_TABLE_OR_COLUMN_REGEX,
|
|
24
|
-
} from '@sqlrooms/project-config';
|
|
25
|
-
import {FC, useCallback} from 'react';
|
|
26
|
-
import {useForm} from 'react-hook-form';
|
|
27
|
-
import * as z from 'zod';
|
|
28
|
-
import {zodResolver} from '@hookform/resolvers/zod';
|
|
29
|
-
|
|
30
|
-
const formSchema = z.object({
|
|
31
|
-
tableName: z
|
|
32
|
-
.string()
|
|
33
|
-
.min(1, 'Table name is required')
|
|
34
|
-
.regex(
|
|
35
|
-
VALID_TABLE_OR_COLUMN_REGEX,
|
|
36
|
-
'Only letters, digits and underscores are allowed; should not start with a digit',
|
|
37
|
-
),
|
|
38
|
-
query: z.string().min(1, 'Query is required'),
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
export type Props = {
|
|
42
|
-
query: string;
|
|
43
|
-
isOpen: boolean;
|
|
44
|
-
onClose: () => void;
|
|
45
|
-
editDataSource?: SqlQueryDataSource;
|
|
46
|
-
onAddOrUpdateSqlQuery: (
|
|
47
|
-
tableName: string,
|
|
48
|
-
query: string,
|
|
49
|
-
oldTableName?: string,
|
|
50
|
-
) => Promise<void>;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const CreateTableModal: FC<Props> = (props) => {
|
|
54
|
-
const {editDataSource, isOpen, onClose, onAddOrUpdateSqlQuery} = props;
|
|
55
|
-
|
|
56
|
-
const form = useForm<z.infer<typeof formSchema>>({
|
|
57
|
-
resolver: zodResolver(formSchema),
|
|
58
|
-
defaultValues: {
|
|
59
|
-
tableName: editDataSource?.tableName ?? '',
|
|
60
|
-
query: editDataSource?.sqlQuery ?? props.query.trim(),
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
const onSubmit = useCallback(
|
|
65
|
-
async (values: z.infer<typeof formSchema>) => {
|
|
66
|
-
try {
|
|
67
|
-
const {tableName, query} = values;
|
|
68
|
-
await onAddOrUpdateSqlQuery(
|
|
69
|
-
tableName,
|
|
70
|
-
query,
|
|
71
|
-
editDataSource?.tableName,
|
|
72
|
-
);
|
|
73
|
-
form.reset();
|
|
74
|
-
onClose();
|
|
75
|
-
} catch (err) {
|
|
76
|
-
form.setError('root', {
|
|
77
|
-
type: 'manual',
|
|
78
|
-
message:
|
|
79
|
-
err instanceof DuckQueryError ? err.getMessageForUser() : `${err}`,
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
|
-
[onAddOrUpdateSqlQuery, editDataSource?.tableName, onClose, form],
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
return (
|
|
87
|
-
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
|
|
88
|
-
<DialogContent className="sm:max-w-[800px]">
|
|
89
|
-
{/* @ts-ignore */}
|
|
90
|
-
<Form {...form}>
|
|
91
|
-
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
|
92
|
-
<DialogHeader>
|
|
93
|
-
<DialogTitle>
|
|
94
|
-
{editDataSource
|
|
95
|
-
? 'Edit table query'
|
|
96
|
-
: 'Create table from query'}
|
|
97
|
-
</DialogTitle>
|
|
98
|
-
{!editDataSource && (
|
|
99
|
-
<DialogDescription>
|
|
100
|
-
Create a new table from the results of an SQL query.
|
|
101
|
-
</DialogDescription>
|
|
102
|
-
)}
|
|
103
|
-
</DialogHeader>
|
|
104
|
-
|
|
105
|
-
{form.formState.errors.root && (
|
|
106
|
-
<Alert variant="destructive">
|
|
107
|
-
<AlertDescription>
|
|
108
|
-
{form.formState.errors.root.message}
|
|
109
|
-
</AlertDescription>
|
|
110
|
-
</Alert>
|
|
111
|
-
)}
|
|
112
|
-
|
|
113
|
-
<FormField
|
|
114
|
-
// @ts-ignore
|
|
115
|
-
control={form.control}
|
|
116
|
-
name="tableName"
|
|
117
|
-
render={({field}) => (
|
|
118
|
-
<FormItem>
|
|
119
|
-
<FormLabel>Table name:</FormLabel>
|
|
120
|
-
<FormControl>
|
|
121
|
-
<Input {...field} className="font-mono" autoFocus />
|
|
122
|
-
</FormControl>
|
|
123
|
-
<FormMessage />
|
|
124
|
-
</FormItem>
|
|
125
|
-
)}
|
|
126
|
-
/>
|
|
127
|
-
|
|
128
|
-
<FormField
|
|
129
|
-
// @ts-ignore
|
|
130
|
-
control={form.control}
|
|
131
|
-
name="query"
|
|
132
|
-
render={({field}) => (
|
|
133
|
-
<FormItem>
|
|
134
|
-
<FormLabel>SQL query:</FormLabel>
|
|
135
|
-
<FormControl>
|
|
136
|
-
<Textarea
|
|
137
|
-
{...field}
|
|
138
|
-
className="font-mono text-sm bg-secondary min-h-[200px]"
|
|
139
|
-
/>
|
|
140
|
-
</FormControl>
|
|
141
|
-
<FormMessage />
|
|
142
|
-
</FormItem>
|
|
143
|
-
)}
|
|
144
|
-
/>
|
|
145
|
-
|
|
146
|
-
<DialogFooter>
|
|
147
|
-
<Button type="button" variant="outline" onClick={onClose}>
|
|
148
|
-
Cancel
|
|
149
|
-
</Button>
|
|
150
|
-
<Button type="submit" disabled={form.formState.isSubmitting}>
|
|
151
|
-
{editDataSource ? 'Update' : 'Create'}
|
|
152
|
-
</Button>
|
|
153
|
-
</DialogFooter>
|
|
154
|
-
</form>
|
|
155
|
-
</Form>
|
|
156
|
-
</DialogContent>
|
|
157
|
-
</Dialog>
|
|
158
|
-
);
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
export default CreateTableModal;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Dialog,
|
|
3
|
-
DialogContent,
|
|
4
|
-
DialogDescription,
|
|
5
|
-
DialogFooter,
|
|
6
|
-
DialogHeader,
|
|
7
|
-
DialogTitle,
|
|
8
|
-
Button,
|
|
9
|
-
} from '@sqlrooms/ui';
|
|
10
|
-
import React from 'react';
|
|
11
|
-
|
|
12
|
-
interface Props {
|
|
13
|
-
isOpen: boolean;
|
|
14
|
-
onClose: () => void;
|
|
15
|
-
onConfirm: () => void;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const DeleteSqlQueryModal: React.FC<Props> = ({isOpen, onClose, onConfirm}) => {
|
|
19
|
-
return (
|
|
20
|
-
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
|
|
21
|
-
<DialogContent>
|
|
22
|
-
<DialogHeader>
|
|
23
|
-
<DialogTitle>Delete Query</DialogTitle>
|
|
24
|
-
<DialogDescription>
|
|
25
|
-
Are you sure you want to delete this query? This action cannot be
|
|
26
|
-
undone.
|
|
27
|
-
</DialogDescription>
|
|
28
|
-
</DialogHeader>
|
|
29
|
-
<DialogFooter>
|
|
30
|
-
<Button variant="outline" onClick={onClose}>
|
|
31
|
-
Cancel
|
|
32
|
-
</Button>
|
|
33
|
-
<Button variant="destructive" onClick={onConfirm}>
|
|
34
|
-
Delete
|
|
35
|
-
</Button>
|
|
36
|
-
</DialogFooter>
|
|
37
|
-
</DialogContent>
|
|
38
|
-
</Dialog>
|
|
39
|
-
);
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export default DeleteSqlQueryModal;
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Dialog,
|
|
3
|
-
DialogContent,
|
|
4
|
-
DialogFooter,
|
|
5
|
-
DialogHeader,
|
|
6
|
-
DialogTitle,
|
|
7
|
-
Button,
|
|
8
|
-
Form,
|
|
9
|
-
FormControl,
|
|
10
|
-
FormField,
|
|
11
|
-
FormItem,
|
|
12
|
-
FormLabel,
|
|
13
|
-
FormMessage,
|
|
14
|
-
Input,
|
|
15
|
-
} from '@sqlrooms/ui';
|
|
16
|
-
import React from 'react';
|
|
17
|
-
import {useForm} from 'react-hook-form';
|
|
18
|
-
import * as z from 'zod';
|
|
19
|
-
import {zodResolver} from '@hookform/resolvers/zod';
|
|
20
|
-
|
|
21
|
-
const formSchema = z.object({
|
|
22
|
-
queryName: z.string().min(1, 'Query name is required'),
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
type FormData = z.infer<typeof formSchema>;
|
|
26
|
-
|
|
27
|
-
interface Props {
|
|
28
|
-
isOpen: boolean;
|
|
29
|
-
onClose: () => void;
|
|
30
|
-
initialName: string;
|
|
31
|
-
onRename: (newName: string) => void;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const RenameSqlQueryModal: React.FC<Props> = ({
|
|
35
|
-
isOpen,
|
|
36
|
-
onClose,
|
|
37
|
-
initialName,
|
|
38
|
-
onRename,
|
|
39
|
-
}) => {
|
|
40
|
-
const form = useForm<FormData>({
|
|
41
|
-
resolver: zodResolver(formSchema),
|
|
42
|
-
defaultValues: {
|
|
43
|
-
queryName: initialName,
|
|
44
|
-
},
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
function onSubmit(values: FormData) {
|
|
48
|
-
onRename(values.queryName);
|
|
49
|
-
onClose();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return (
|
|
53
|
-
<Dialog open={isOpen} onOpenChange={(open) => !open && onClose()}>
|
|
54
|
-
<DialogContent>
|
|
55
|
-
<DialogHeader>
|
|
56
|
-
<DialogTitle>Rename Query</DialogTitle>
|
|
57
|
-
</DialogHeader>
|
|
58
|
-
{/* @ts-ignore */}
|
|
59
|
-
<Form {...form}>
|
|
60
|
-
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
|
61
|
-
<FormField
|
|
62
|
-
// @ts-ignore
|
|
63
|
-
control={form.control}
|
|
64
|
-
name="queryName"
|
|
65
|
-
render={({field}) => (
|
|
66
|
-
<FormItem>
|
|
67
|
-
<FormLabel>Query Name</FormLabel>
|
|
68
|
-
<FormControl>
|
|
69
|
-
<Input
|
|
70
|
-
{...field}
|
|
71
|
-
autoFocus
|
|
72
|
-
placeholder="Enter query name"
|
|
73
|
-
/>
|
|
74
|
-
</FormControl>
|
|
75
|
-
<FormMessage />
|
|
76
|
-
</FormItem>
|
|
77
|
-
)}
|
|
78
|
-
/>
|
|
79
|
-
<DialogFooter>
|
|
80
|
-
<Button type="button" variant="outline" onClick={onClose}>
|
|
81
|
-
Cancel
|
|
82
|
-
</Button>
|
|
83
|
-
<Button type="submit">Save</Button>
|
|
84
|
-
</DialogFooter>
|
|
85
|
-
</form>
|
|
86
|
-
</Form>
|
|
87
|
-
</DialogContent>
|
|
88
|
-
</Dialog>
|
|
89
|
-
);
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
export default RenameSqlQueryModal;
|
package/src/SqlEditor.tsx
DELETED
|
@@ -1,495 +0,0 @@
|
|
|
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;
|
package/src/SqlEditorModal.tsx
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
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;
|
package/src/TablesList.tsx
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
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
DELETED