1
1
import copy from 'copy-to-clipboard' ;
2
2
import type { NavigationTreeNodeType , NavigationTreeProps } from 'ydb-ui-components' ;
3
3
4
+ import type { AppDispatch } from '../../../store' ;
4
5
import { changeUserInput } from '../../../store/reducers/executeQuery' ;
6
+ import type { GetTableSchemaDataParams } from '../../../store/reducers/tableSchemaData' ;
5
7
import { TENANT_PAGES_IDS , TENANT_QUERY_TABS_ID } from '../../../store/reducers/tenant/constants' ;
6
8
import { setQueryTab , setTenantPage } from '../../../store/reducers/tenant/tenant' ;
7
9
import type { QueryMode , QuerySettings } from '../../../types/store/query' ;
8
10
import createToast from '../../../utils/createToast' ;
9
11
import { transformPath } from '../ObjectSummary/transformPath' ;
12
+ import type { SchemaData } from '../Schema/SchemaViewer/types' ;
10
13
import i18n from '../i18n' ;
11
14
12
- import type { SchemaQueryParams } from './schemaQueryTemplates' ;
15
+ import { nodeTableTypeToPathType } from './schema' ;
16
+ import type { TemplateFn } from './schemaQueryTemplates' ;
13
17
import {
14
18
addTableIndex ,
15
19
alterAsyncReplicationTemplate ,
@@ -34,31 +38,60 @@ interface ActionsAdditionalEffects {
34
38
updateQueryExecutionSettings : ( settings ?: Partial < QuerySettings > ) => void ;
35
39
setActivePath : ( path : string ) => void ;
36
40
showCreateDirectoryDialog ?: ( path : string ) => void ;
41
+ getTableSchemaDataPromise ?: (
42
+ params : GetTableSchemaDataParams ,
43
+ ) => Promise < SchemaData [ ] | undefined > ;
44
+ }
45
+
46
+ interface BindActionParams {
47
+ tenantName : string ;
48
+ type : NavigationTreeNodeType ;
49
+ path : string ;
50
+ relativePath : string ;
37
51
}
38
52
39
53
const bindActions = (
40
- schemaQueryParams : SchemaQueryParams ,
41
- dispatch : React . Dispatch < any > ,
54
+ params : BindActionParams ,
55
+ dispatch : AppDispatch ,
42
56
additionalEffects : ActionsAdditionalEffects ,
43
57
) => {
44
- const { setActivePath, updateQueryExecutionSettings, showCreateDirectoryDialog} =
45
- additionalEffects ;
46
-
47
- const inputQuery = ( tmpl : ( params ?: SchemaQueryParams ) => string , mode ?: QueryMode ) => ( ) => {
58
+ const {
59
+ setActivePath,
60
+ updateQueryExecutionSettings,
61
+ showCreateDirectoryDialog,
62
+ getTableSchemaDataPromise,
63
+ } = additionalEffects ;
64
+
65
+ const inputQuery = ( tmpl : TemplateFn , mode ?: QueryMode ) => ( ) => {
48
66
if ( mode ) {
49
67
updateQueryExecutionSettings ( { queryMode : mode } ) ;
50
68
}
51
69
52
- dispatch ( changeUserInput ( { input : tmpl ( schemaQueryParams ) } ) ) ;
70
+ const pathType = nodeTableTypeToPathType [ params . type ] ;
71
+ const withTableData = [ selectQueryTemplate , upsertQueryTemplate ] . includes ( tmpl ) ;
72
+
73
+ const userInputDataPromise =
74
+ withTableData && pathType && getTableSchemaDataPromise
75
+ ? getTableSchemaDataPromise ( {
76
+ path : params . path ,
77
+ tenantName : params . tenantName ,
78
+ type : pathType ,
79
+ } )
80
+ : Promise . resolve ( undefined ) ;
81
+
82
+ userInputDataPromise . then ( ( tableData ) => {
83
+ dispatch ( changeUserInput ( { input : tmpl ( { ...params , tableData} ) } ) ) ;
84
+ } ) ;
85
+
53
86
dispatch ( setTenantPage ( TENANT_PAGES_IDS . query ) ) ;
54
87
dispatch ( setQueryTab ( TENANT_QUERY_TABS_ID . newQuery ) ) ;
55
- setActivePath ( schemaQueryParams . path ) ;
88
+ setActivePath ( params . path ) ;
56
89
} ;
57
90
58
91
return {
59
92
createDirectory : showCreateDirectoryDialog
60
93
? ( ) => {
61
- showCreateDirectoryDialog ( schemaQueryParams . path ) ;
94
+ showCreateDirectoryDialog ( params . path ) ;
62
95
}
63
96
: undefined ,
64
97
createTable : inputQuery ( createTableTemplate , 'script' ) ,
@@ -81,7 +114,7 @@ const bindActions = (
81
114
addTableIndex : inputQuery ( addTableIndex , 'script' ) ,
82
115
copyPath : ( ) => {
83
116
try {
84
- copy ( schemaQueryParams . relativePath ) ;
117
+ copy ( params . relativePath ) ;
85
118
createToast ( {
86
119
name : 'Copied' ,
87
120
title : i18n ( 'actions.copied' ) ,
@@ -101,10 +134,14 @@ const bindActions = (
101
134
type ActionsSet = ReturnType < Required < NavigationTreeProps > [ 'getActions' ] > ;
102
135
103
136
export const getActions =
104
- ( dispatch : React . Dispatch < any > , additionalEffects : ActionsAdditionalEffects , rootPath = '' ) =>
137
+ ( dispatch : AppDispatch , additionalEffects : ActionsAdditionalEffects , rootPath = '' ) =>
105
138
( path : string , type : NavigationTreeNodeType ) => {
106
139
const relativePath = transformPath ( path , rootPath ) ;
107
- const actions = bindActions ( { path, relativePath} , dispatch , additionalEffects ) ;
140
+ const actions = bindActions (
141
+ { path, relativePath, tenantName : rootPath , type} ,
142
+ dispatch ,
143
+ additionalEffects ,
144
+ ) ;
108
145
const copyItem = { text : i18n ( 'actions.copyPath' ) , action : actions . copyPath } ;
109
146
110
147
const DIR_SET : ActionsSet = [
0 commit comments