-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathschemaActions.tsx
310 lines (277 loc) · 11 KB
/
schemaActions.tsx
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import {Copy, PlugConnection} from '@gravity-ui/icons';
import {Flex, Spin} from '@gravity-ui/uikit';
import copy from 'copy-to-clipboard';
import type {NavigationTreeNodeType, NavigationTreeProps} from 'ydb-ui-components';
import type {SnippetParams} from '../../../components/ConnectToDB/types';
import type {AppDispatch} from '../../../store';
import {TENANT_PAGES_IDS, TENANT_QUERY_TABS_ID} from '../../../store/reducers/tenant/constants';
import {setQueryTab, setTenantPage} from '../../../store/reducers/tenant/tenant';
import type {QuerySettings} from '../../../types/store/query';
import createToast from '../../../utils/createToast';
import {insertSnippetToEditor} from '../../../utils/monaco/insertSnippet';
import {transformPath} from '../ObjectSummary/transformPath';
import type {SchemaData} from '../Schema/SchemaViewer/types';
import i18n from '../i18n';
import type {TemplateFn} from './schemaQueryTemplates';
import {
addTableIndex,
alterAsyncReplicationTemplate,
alterTableTemplate,
alterTopicTemplate,
createAsyncReplicationTemplate,
createCdcStreamTemplate,
createColumnTableTemplate,
createExternalTableTemplate,
createTableTemplate,
createTopicTemplate,
createViewTemplate,
dropAsyncReplicationTemplate,
dropExternalTableTemplate,
dropTableIndex,
dropTableTemplate,
dropTopicTemplate,
dropViewTemplate,
manageAutoPartitioningTemplate,
selectQueryTemplate,
upsertQueryTemplate,
} from './schemaQueryTemplates';
interface ActionsAdditionalParams {
updateQueryExecutionSettings: (settings?: Partial<QuerySettings>) => void;
setActivePath: (path: string) => void;
showCreateDirectoryDialog?: (path: string) => void;
getConfirmation?: () => Promise<boolean>;
getConnectToDBDialog?: (params: SnippetParams) => Promise<boolean>;
schemaData?: SchemaData[];
isSchemaDataLoading?: boolean;
}
interface BindActionParams {
tenantName: string;
type: NavigationTreeNodeType;
path: string;
relativePath: string;
}
const bindActions = (
params: BindActionParams,
dispatch: AppDispatch,
additionalEffects: ActionsAdditionalParams,
) => {
const {
setActivePath,
showCreateDirectoryDialog,
getConfirmation,
getConnectToDBDialog,
schemaData,
} = additionalEffects;
const inputQuery = (tmpl: TemplateFn) => () => {
const applyInsert = () => {
//order is important here: firstly we should open query tab and initialize editor (it will be set to window.ydbEditor), after that it is possible to insert snippet
dispatch(setTenantPage(TENANT_PAGES_IDS.query));
dispatch(setQueryTab(TENANT_QUERY_TABS_ID.newQuery));
setActivePath(params.path);
insertSnippetToEditor(tmpl({...params, schemaData}));
};
if (getConfirmation) {
const confirmedPromise = getConfirmation();
confirmedPromise.then((confirmed) => {
if (confirmed) {
applyInsert();
}
});
} else {
applyInsert();
}
};
return {
createDirectory: showCreateDirectoryDialog
? () => {
showCreateDirectoryDialog(params.path);
}
: undefined,
getConnectToDBDialog: () => getConnectToDBDialog?.({database: params.path}),
createTable: inputQuery(createTableTemplate),
createColumnTable: inputQuery(createColumnTableTemplate),
createAsyncReplication: inputQuery(createAsyncReplicationTemplate),
alterAsyncReplication: inputQuery(alterAsyncReplicationTemplate),
dropAsyncReplication: inputQuery(dropAsyncReplicationTemplate),
alterTable: inputQuery(alterTableTemplate),
dropTable: inputQuery(dropTableTemplate),
manageAutoPartitioning: inputQuery(manageAutoPartitioningTemplate),
selectQuery: inputQuery(selectQueryTemplate),
upsertQuery: inputQuery(upsertQueryTemplate),
createExternalTable: inputQuery(createExternalTableTemplate),
dropExternalTable: inputQuery(dropExternalTableTemplate),
selectQueryFromExternalTable: inputQuery(selectQueryTemplate),
createTopic: inputQuery(createTopicTemplate),
alterTopic: inputQuery(alterTopicTemplate),
dropTopic: inputQuery(dropTopicTemplate),
createView: inputQuery(createViewTemplate),
dropView: inputQuery(dropViewTemplate),
dropIndex: inputQuery(dropTableIndex),
addTableIndex: inputQuery(addTableIndex),
createCdcStream: inputQuery(createCdcStreamTemplate),
copyPath: () => {
try {
copy(params.relativePath);
createToast({
name: 'Copied',
title: i18n('actions.copied'),
type: 'success',
});
} catch {
createToast({
name: 'Not copied',
title: i18n('actions.notCopied'),
type: 'error',
});
}
},
};
};
type ActionsSet = ReturnType<Required<NavigationTreeProps>['getActions']>;
interface ActionConfig {
text: string;
action: () => void;
isLoading?: boolean;
}
const getActionWithLoader = ({text, action, isLoading}: ActionConfig) => ({
text: (
<Flex justifyContent="space-between" alignItems="center">
{text}
{isLoading && <Spin size="xs" />}
</Flex>
),
action,
disabled: isLoading,
});
export const getActions =
(dispatch: AppDispatch, additionalEffects: ActionsAdditionalParams, rootPath = '') =>
(path: string, type: NavigationTreeNodeType) => {
const relativePath = transformPath(path, rootPath);
const actions = bindActions(
{path, relativePath, tenantName: rootPath, type},
dispatch,
additionalEffects,
);
const copyItem: ActionsSet[0] = {
text: i18n('actions.copyPath'),
action: actions.copyPath,
iconEnd: <Copy />,
};
const connectToDBItem = {
text: i18n('actions.connectToDB'),
action: actions.getConnectToDBDialog,
iconEnd: <PlugConnection />,
};
const createEntitiesSet = [
{text: i18n('actions.createTable'), action: actions.createTable},
{text: i18n('actions.createColumnTable'), action: actions.createColumnTable},
{
text: i18n('actions.createAsyncReplication'),
action: actions.createAsyncReplication,
},
{text: i18n('actions.createTopic'), action: actions.createTopic},
{text: i18n('actions.createView'), action: actions.createView},
];
const alterTableGroupItem = {
text: i18n('actions.alterTable'),
items: [
{text: i18n('actions.manageColumns'), action: actions.alterTable},
{
text: i18n('actions.manageAutoPartitioning'),
action: actions.manageAutoPartitioning,
},
],
};
const DB_SET: ActionsSet = [[copyItem, connectToDBItem], createEntitiesSet];
const DIR_SET: ActionsSet = [[copyItem], createEntitiesSet];
if (actions.createDirectory) {
const createDirectoryItem = {
text: i18n('actions.createDirectory'),
action: actions.createDirectory,
};
DB_SET.splice(1, 0, [createDirectoryItem]);
DIR_SET.splice(1, 0, [createDirectoryItem]);
}
const ROW_TABLE_SET: ActionsSet = [
[copyItem],
[
alterTableGroupItem,
{text: i18n('actions.dropTable'), action: actions.dropTable},
getActionWithLoader({
text: i18n('actions.selectQuery'),
action: actions.selectQuery,
isLoading: additionalEffects.isSchemaDataLoading,
}),
getActionWithLoader({
text: i18n('actions.upsertQuery'),
action: actions.upsertQuery,
isLoading: additionalEffects.isSchemaDataLoading,
}),
{text: i18n('actions.addTableIndex'), action: actions.addTableIndex},
{text: i18n('actions.createCdcStream'), action: actions.createCdcStream},
],
];
const COLUMN_TABLE_SET: ActionsSet = [
[copyItem],
[
alterTableGroupItem,
{text: i18n('actions.dropTable'), action: actions.dropTable},
{text: i18n('actions.selectQuery'), action: actions.selectQuery},
{text: i18n('actions.upsertQuery'), action: actions.upsertQuery},
],
];
const TOPIC_SET: ActionsSet = [
[copyItem],
[
{text: i18n('actions.alterTopic'), action: actions.alterTopic},
{text: i18n('actions.dropTopic'), action: actions.dropTopic},
],
];
const EXTERNAL_TABLE_SET = [
[copyItem],
[
{
text: i18n('actions.selectQuery'),
action: actions.selectQueryFromExternalTable,
},
],
[{text: i18n('actions.dropTable'), action: actions.dropExternalTable}],
];
const EXTERNAL_DATA_SOURCE_SET = [
[copyItem],
[{text: i18n('actions.createExternalTable'), action: actions.createExternalTable}],
];
const VIEW_SET = [
[copyItem],
[{text: i18n('actions.selectQuery'), action: actions.selectQuery}],
[{text: i18n('actions.dropView'), action: actions.dropView}],
];
const ASYNC_REPLICATION_SET: ActionsSet = [
[copyItem],
[
{text: i18n('actions.alterReplication'), action: actions.alterAsyncReplication},
{text: i18n('actions.dropReplication'), action: actions.dropAsyncReplication},
],
];
const INDEX_SET: ActionsSet = [
[copyItem, {text: i18n('actions.dropIndex'), action: actions.dropIndex}],
];
const JUST_COPY: ActionsSet = [copyItem];
// verbose mapping to guarantee a correct actions set for new node types
// TS will error when a new type is added in the lib but is not mapped here
const nodeTypeToActions: Record<NavigationTreeNodeType, ActionsSet> = {
async_replication: ASYNC_REPLICATION_SET,
database: DB_SET,
directory: DIR_SET,
table: ROW_TABLE_SET,
column_table: COLUMN_TABLE_SET,
index_table: JUST_COPY,
topic: TOPIC_SET,
stream: JUST_COPY,
index: INDEX_SET,
external_table: EXTERNAL_TABLE_SET,
external_data_source: EXTERNAL_DATA_SOURCE_SET,
view: VIEW_SET,
};
return nodeTypeToActions[type];
};