11import copy from 'copy-to-clipboard' ;
22import type { NavigationTreeNodeType , NavigationTreeProps } from 'ydb-ui-components' ;
33
4+ import type { AppDispatch } from '../../../store' ;
45import { changeUserInput } from '../../../store/reducers/executeQuery' ;
6+ import type { GetTableSchemaDataParams } from '../../../store/reducers/tableSchemaData' ;
57import { TENANT_PAGES_IDS , TENANT_QUERY_TABS_ID } from '../../../store/reducers/tenant/constants' ;
68import { setQueryTab , setTenantPage } from '../../../store/reducers/tenant/tenant' ;
79import type { QueryMode , QuerySettings } from '../../../types/store/query' ;
810import createToast from '../../../utils/createToast' ;
911import { transformPath } from '../ObjectSummary/transformPath' ;
12+ import type { SchemaData } from '../Schema/SchemaViewer/types' ;
1013import i18n from '../i18n' ;
1114
12- import type { SchemaQueryParams } from './schemaQueryTemplates' ;
15+ import { nodeTableTypeToPathType } from './schema' ;
16+ import type { TemplateFn } from './schemaQueryTemplates' ;
1317import {
1418 addTableIndex ,
1519 alterAsyncReplicationTemplate ,
@@ -34,31 +38,60 @@ interface ActionsAdditionalEffects {
3438 updateQueryExecutionSettings : ( settings ?: Partial < QuerySettings > ) => void ;
3539 setActivePath : ( path : string ) => void ;
3640 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 ;
3751}
3852
3953const bindActions = (
40- schemaQueryParams : SchemaQueryParams ,
41- dispatch : React . Dispatch < any > ,
54+ params : BindActionParams ,
55+ dispatch : AppDispatch ,
4256 additionalEffects : ActionsAdditionalEffects ,
4357) => {
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 ) => ( ) => {
4866 if ( mode ) {
4967 updateQueryExecutionSettings ( { queryMode : mode } ) ;
5068 }
5169
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+
5386 dispatch ( setTenantPage ( TENANT_PAGES_IDS . query ) ) ;
5487 dispatch ( setQueryTab ( TENANT_QUERY_TABS_ID . newQuery ) ) ;
55- setActivePath ( schemaQueryParams . path ) ;
88+ setActivePath ( params . path ) ;
5689 } ;
5790
5891 return {
5992 createDirectory : showCreateDirectoryDialog
6093 ? ( ) => {
61- showCreateDirectoryDialog ( schemaQueryParams . path ) ;
94+ showCreateDirectoryDialog ( params . path ) ;
6295 }
6396 : undefined ,
6497 createTable : inputQuery ( createTableTemplate , 'script' ) ,
@@ -81,7 +114,7 @@ const bindActions = (
81114 addTableIndex : inputQuery ( addTableIndex , 'script' ) ,
82115 copyPath : ( ) => {
83116 try {
84- copy ( schemaQueryParams . relativePath ) ;
117+ copy ( params . relativePath ) ;
85118 createToast ( {
86119 name : 'Copied' ,
87120 title : i18n ( 'actions.copied' ) ,
@@ -101,10 +134,14 @@ const bindActions = (
101134type ActionsSet = ReturnType < Required < NavigationTreeProps > [ 'getActions' ] > ;
102135
103136export const getActions =
104- ( dispatch : React . Dispatch < any > , additionalEffects : ActionsAdditionalEffects , rootPath = '' ) =>
137+ ( dispatch : AppDispatch , additionalEffects : ActionsAdditionalEffects , rootPath = '' ) =>
105138 ( path : string , type : NavigationTreeNodeType ) => {
106139 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+ ) ;
108145 const copyItem = { text : i18n ( 'actions.copyPath' ) , action : actions . copyPath } ;
109146
110147 const DIR_SET : ActionsSet = [
0 commit comments