@@ -15,7 +15,6 @@ const createTableTemplate = (path: string) => {
1515 PRIMARY KEY (\`id\`)
1616);` ;
1717} ;
18-
1918const alterTableTemplate = ( path : string ) => {
2019 return `ALTER TABLE \`${ path } \`
2120 ADD COLUMN is_deleted Bool;` ;
@@ -32,40 +31,23 @@ const upsertQueryTemplate = (path: string) => {
3231VALUES ( );` ;
3332} ;
3433
35- export const getActions = (
34+ const bindActions = (
35+ path : string ,
3636 dispatch : Dispatch < any > ,
3737 setActivePath : ( path : string ) => void ,
38- ) =>
39- ( path : string , type : NavigationTreeNodeType ) => {
40- const switchTabToQuery = ( ) => {
41- dispatch ( setTopLevelTab ( TenantGeneralTabsIds . query ) ) ;
42- } ;
43-
44- const onCreateTableClick = ( ) => {
45- dispatch ( changeUserInput ( { input : createTableTemplate ( path ) } ) ) ;
46- switchTabToQuery ( ) ;
47- setActivePath ( path ) ;
48- } ;
49-
50- const onAlterTableClick = ( ) => {
51- dispatch ( changeUserInput ( { input : alterTableTemplate ( path ) } ) ) ;
52- switchTabToQuery ( ) ;
53- setActivePath ( path ) ;
54- } ;
55-
56- const onSelectQueryClick = ( ) => {
57- dispatch ( changeUserInput ( { input : selectQueryTemplate ( path ) } ) ) ;
58- switchTabToQuery ( ) ;
59- setActivePath ( path ) ;
60- } ;
38+ ) => {
39+ const inputQuery = ( tmpl : ( path : string ) => string ) => ( ) => {
40+ dispatch ( changeUserInput ( { input : tmpl ( path ) } ) ) ;
41+ dispatch ( setTopLevelTab ( TenantGeneralTabsIds . query ) )
42+ setActivePath ( path ) ;
43+ }
6144
62- const onUpsertQueryClick = ( ) => {
63- dispatch ( changeUserInput ( { input : upsertQueryTemplate ( path ) } ) ) ;
64- switchTabToQuery ( ) ;
65- setActivePath ( path ) ;
66- } ;
67-
68- const onCopyPathClick = ( ) => {
45+ return {
46+ createTable : inputQuery ( createTableTemplate ) ,
47+ alterTable : inputQuery ( alterTableTemplate ) ,
48+ selectQuery : inputQuery ( selectQueryTemplate ) ,
49+ upsertQuery : inputQuery ( upsertQueryTemplate ) ,
50+ copyPath : ( ) => {
6951 navigator . clipboard
7052 . writeText ( path )
7153 . then ( ( ) => {
@@ -82,34 +64,41 @@ export const getActions = (
8264 type : 'error' ,
8365 } ) ;
8466 } ) ;
85- } ;
86-
87- const onOpenPreviewClick = ( ) => {
67+ } ,
68+ openPreview : ( ) => {
8869 dispatch ( setShowPreview ( true ) ) ;
89- switchTabToQuery ( ) ;
70+ dispatch ( setTopLevelTab ( TenantGeneralTabsIds . query ) )
9071 setActivePath ( path ) ;
91- } ;
72+ } ,
73+ } ;
74+ } ;
9275
93- const copyItem = { text : 'Copy path' , action : onCopyPathClick } ;
76+ export const getActions = (
77+ dispatch : Dispatch < any > ,
78+ setActivePath : ( path : string ) => void ,
79+ ) =>
80+ ( path : string , type : NavigationTreeNodeType ) => {
81+ const actions = bindActions ( path , dispatch , setActivePath ) ;
82+ const copyItem = { text : 'Copy path' , action : actions . copyPath } ;
9483
9584 return type === 'table'
9685 ? [
9786 [
98- { text : 'Open preview' , action : onOpenPreviewClick } ,
87+ { text : 'Open preview' , action : actions . openPreview } ,
9988 copyItem ,
10089 ] ,
10190 [
102- { text : 'Alter table...' , action : onAlterTableClick } ,
103- { text : 'Select query...' , action : onSelectQueryClick } ,
104- { text : 'Upsert query...' , action : onUpsertQueryClick } ,
91+ { text : 'Alter table...' , action : actions . alterTable } ,
92+ { text : 'Select query...' , action : actions . selectQuery } ,
93+ { text : 'Upsert query...' , action : actions . upsertQuery } ,
10594 ] ,
10695 ]
10796 : [
10897 [
10998 copyItem ,
11099 ] ,
111100 [
112- { text : 'Create table...' , action : onCreateTableClick } ,
101+ { text : 'Create table...' , action : actions . createTable } ,
113102 ] ,
114103 ] ;
115104 } ;
0 commit comments