@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.
@@ -0,0 +1,161 @@
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;
@@ -0,0 +1,42 @@
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;
@@ -0,0 +1,92 @@
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;