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' ;
56import { TENANT_PAGES_IDS , TENANT_QUERY_TABS_ID } from '../../../store/reducers/tenant/constants' ;
67import { setQueryTab , setTenantPage } from '../../../store/reducers/tenant/tenant' ;
78import type { QueryMode , QuerySettings } from '../../../types/store/query' ;
89import createToast from '../../../utils/createToast' ;
10+ import { getTableDataPromise } from '../../../utils/hooks' ;
911import { transformPath } from '../ObjectSummary/transformPath' ;
1012import i18n from '../i18n' ;
1113
12- import type { SchemaQueryParams } from './schemaQueryTemplates' ;
14+ import { nodeTableTypeToPathType } from './schema' ;
15+ import type { TemplateFn } from './schemaQueryTemplates' ;
1316import {
1417 addTableIndex ,
1518 alterAsyncReplicationTemplate ,
@@ -36,52 +39,74 @@ interface ActionsAdditionalEffects {
3639 showCreateDirectoryDialog ?: ( path : string ) => void ;
3740}
3841
42+ interface AdditionalInputQueryOptions {
43+ mode ?: QueryMode ;
44+ withTableData ?: boolean ;
45+ }
46+
47+ interface BindActionParams {
48+ tenantName : string ;
49+ type : NavigationTreeNodeType ;
50+ path : string ;
51+ relativePath : string ;
52+ }
53+
3954const bindActions = (
40- schemaQueryParams : SchemaQueryParams ,
41- dispatch : React . Dispatch < any > ,
55+ params : BindActionParams ,
56+ dispatch : AppDispatch ,
4257 additionalEffects : ActionsAdditionalEffects ,
4358) => {
4459 const { setActivePath, updateQueryExecutionSettings, showCreateDirectoryDialog} =
4560 additionalEffects ;
4661
47- const inputQuery = ( tmpl : ( params ?: SchemaQueryParams ) => string , mode ?: QueryMode ) => ( ) => {
48- if ( mode ) {
49- updateQueryExecutionSettings ( { queryMode : mode } ) ;
62+ const inputQuery = ( tmpl : TemplateFn , options ?: AdditionalInputQueryOptions ) => ( ) => {
63+ if ( options ?. mode ) {
64+ updateQueryExecutionSettings ( { queryMode : options . mode } ) ;
5065 }
5166
52- dispatch ( changeUserInput ( { input : tmpl ( schemaQueryParams ) } ) ) ;
67+ const pathType = nodeTableTypeToPathType [ params . type ] ;
68+
69+ const userInputDataPromise =
70+ options ?. withTableData && pathType
71+ ? getTableDataPromise ( params . path , params . tenantName , pathType , dispatch )
72+ : Promise . resolve ( undefined ) ;
73+
74+ userInputDataPromise . then ( ( tableData ) => {
75+ dispatch ( changeUserInput ( { input : tmpl ( { ...params , tableData} ) } ) ) ;
76+ } ) ;
77+
5378 dispatch ( setTenantPage ( TENANT_PAGES_IDS . query ) ) ;
5479 dispatch ( setQueryTab ( TENANT_QUERY_TABS_ID . newQuery ) ) ;
55- setActivePath ( schemaQueryParams . path ) ;
80+ setActivePath ( params . path ) ;
5681 } ;
5782
5883 return {
5984 createDirectory : showCreateDirectoryDialog
6085 ? ( ) => {
61- showCreateDirectoryDialog ( schemaQueryParams . path ) ;
86+ showCreateDirectoryDialog ( params . path ) ;
6287 }
6388 : undefined ,
64- createTable : inputQuery ( createTableTemplate , 'script' ) ,
65- createColumnTable : inputQuery ( createColumnTableTemplate , 'script' ) ,
66- createAsyncReplication : inputQuery ( createAsyncReplicationTemplate , 'script' ) ,
67- alterAsyncReplication : inputQuery ( alterAsyncReplicationTemplate , 'script' ) ,
68- dropAsyncReplication : inputQuery ( dropAsyncReplicationTemplate , 'script' ) ,
69- alterTable : inputQuery ( alterTableTemplate , 'script' ) ,
70- selectQuery : inputQuery ( selectQueryTemplate ) ,
71- upsertQuery : inputQuery ( upsertQueryTemplate ) ,
72- createExternalTable : inputQuery ( createExternalTableTemplate , 'script' ) ,
73- dropExternalTable : inputQuery ( dropExternalTableTemplate , 'script' ) ,
74- selectQueryFromExternalTable : inputQuery ( selectQueryTemplate , 'query' ) ,
75- createTopic : inputQuery ( createTopicTemplate , 'script' ) ,
76- alterTopic : inputQuery ( alterTopicTemplate , 'script' ) ,
77- dropTopic : inputQuery ( dropTopicTemplate , 'script' ) ,
78- createView : inputQuery ( createViewTemplate , 'script' ) ,
79- dropView : inputQuery ( dropViewTemplate , 'script' ) ,
80- dropIndex : inputQuery ( dropTableIndex , 'script' ) ,
81- addTableIndex : inputQuery ( addTableIndex , 'script' ) ,
89+ createTable : inputQuery ( createTableTemplate , { mode : 'script' } ) ,
90+ createColumnTable : inputQuery ( createColumnTableTemplate , { mode : 'script' } ) ,
91+ createAsyncReplication : inputQuery ( createAsyncReplicationTemplate , { mode : 'script' } ) ,
92+ alterAsyncReplication : inputQuery ( alterAsyncReplicationTemplate , { mode : 'script' } ) ,
93+ dropAsyncReplication : inputQuery ( dropAsyncReplicationTemplate , { mode : 'script' } ) ,
94+ alterTable : inputQuery ( alterTableTemplate , { mode : 'script' } ) ,
95+ selectQuery : inputQuery ( selectQueryTemplate , { withTableData : true } ) ,
96+ upsertQuery : inputQuery ( upsertQueryTemplate , { withTableData : true } ) ,
97+ createExternalTable : inputQuery ( createExternalTableTemplate , { mode : 'script' } ) ,
98+ dropExternalTable : inputQuery ( dropExternalTableTemplate , { mode : 'script' } ) ,
99+ selectQueryFromExternalTable : inputQuery ( selectQueryTemplate , { mode : 'query' } ) ,
100+ createTopic : inputQuery ( createTopicTemplate , { mode : 'script' } ) ,
101+ alterTopic : inputQuery ( alterTopicTemplate , { mode : 'script' } ) ,
102+ dropTopic : inputQuery ( dropTopicTemplate , { mode : 'script' } ) ,
103+ createView : inputQuery ( createViewTemplate , { mode : 'script' } ) ,
104+ dropView : inputQuery ( dropViewTemplate , { mode : 'script' } ) ,
105+ dropIndex : inputQuery ( dropTableIndex , { mode : 'script' } ) ,
106+ addTableIndex : inputQuery ( addTableIndex , { mode : 'script' } ) ,
82107 copyPath : ( ) => {
83108 try {
84- copy ( schemaQueryParams . relativePath ) ;
109+ copy ( params . relativePath ) ;
85110 createToast ( {
86111 name : 'Copied' ,
87112 title : i18n ( 'actions.copied' ) ,
@@ -101,10 +126,14 @@ const bindActions = (
101126type ActionsSet = ReturnType < Required < NavigationTreeProps > [ 'getActions' ] > ;
102127
103128export const getActions =
104- ( dispatch : React . Dispatch < any > , additionalEffects : ActionsAdditionalEffects , rootPath = '' ) =>
129+ ( dispatch : AppDispatch , additionalEffects : ActionsAdditionalEffects , rootPath = '' ) =>
105130 ( path : string , type : NavigationTreeNodeType ) => {
106131 const relativePath = transformPath ( path , rootPath ) ;
107- const actions = bindActions ( { path, relativePath} , dispatch , additionalEffects ) ;
132+ const actions = bindActions (
133+ { path, relativePath, tenantName : rootPath , type} ,
134+ dispatch ,
135+ additionalEffects ,
136+ ) ;
108137 const copyItem = { text : i18n ( 'actions.copyPath' ) , action : actions . copyPath } ;
109138
110139 const DIR_SET : ActionsSet = [
0 commit comments