@@ -2,21 +2,20 @@ import React, { memo, useRef, useEffect, useState } from 'react';
22import { connect } from 'umi' ;
33import styles from './index.less' ;
44import classnames from 'classnames' ;
5- import { ConsoleOpenedStatus , ConsoleStatus , DatabaseTypeCode , TreeNodeType , operationTypeConfig , OperationType } from '@/constants' ;
5+ import { ConsoleOpenedStatus , ConsoleStatus , DatabaseTypeCode , TreeNodeType , tabTypeConfig , TabType } from '@/constants' ;
66import { IConsole , ICreateConsole } from '@/typings' ;
77import historyService from '@/service/history' ;
88import sqlService from '@/service/sql' ;
99import Tabs , { IOption } from '@/components/Tabs' ;
1010import LoadingContent from '@/components/Loading/LoadingContent' ;
1111import ShortcutKey from '@/components/ShortcutKey' ;
12- import WorkspaceRightItem from '../WorkspaceRightItem' ;
1312import DatabaseTableEditor from '@/blocks/DatabaseTableEditor' ;
13+ import SQLExecute from '@/blocks/SQLExecute' ;
1414import { IWorkspaceModelState , IWorkspaceModelType } from '@/models/workspace' ;
1515import { IAIState } from '@/models/ai' ;
1616import { handleLocalStorageSavedConsole } from '@/utils' ;
1717import { useUpdateEffect } from '@/hooks/useUpdateEffect' ;
18- import Tree from 'antd/es/tree/Tree' ;
19- import Iconfont from '@/components/Iconfont' ;
18+ import { v4 as uuidV4 } from 'uuid' ;
2019
2120interface IProps {
2221 className ?: string ;
@@ -25,12 +24,52 @@ interface IProps {
2524 dispatch : any ;
2625}
2726
27+ export interface ITab {
28+ id : number ;
29+ tabType : TabType ;
30+ icon : string ;
31+ [ key : string ] : any ;
32+ }
33+
2834const WorkspaceRight = memo < IProps > ( function ( props ) {
2935 const [ activeConsoleId , setActiveConsoleId ] = useState < number > ( ) ;
3036 const { className, aiModel, workspaceModel, dispatch } = props ;
31- const { curWorkspaceParams, doubleClickTreeNodeData, openConsoleList, curConsoleId } = workspaceModel ;
37+ const { curWorkspaceParams, doubleClickTreeNodeData, createTabIntro , openConsoleList, curConsoleId } = workspaceModel ;
3238 const openConsoleListRef = useRef ( openConsoleList ) ;
39+ const [ tabList , setTabList ] = useState < ITab [ ] > ( [ ] ) ;
40+
41+ useEffect ( ( ) => {
42+ const newTabList = openConsoleList ?. map ( t => {
43+ return {
44+ ...t ,
45+ tabType : TabType . CONSOLE ,
46+ icon : tabTypeConfig [ t . operationType ] ?. icon || tabTypeConfig . console . icon ,
47+ }
48+ } )
49+ setTabList ( newTabList || [ ] )
50+ } , [ openConsoleList ] )
51+
52+ // 监听编辑表事件
53+ useEffect ( ( ) => {
54+ if ( createTabIntro ) {
55+ const id : any = uuidV4 ( ) ;
56+ setTabList ( [ ...tabList , {
57+ id,
58+ tabType : createTabIntro . tabType ,
59+ icon : tabTypeConfig [ createTabIntro . tabType as TabType ] ?. icon ,
60+ name : `edit-${ createTabIntro . treeNodeData . name } ` ,
61+ } ] )
62+ setActiveConsoleId ( id ) ;
63+
64+ // 用完之后就清掉createTabIntro
65+ dispatch ( {
66+ type : 'workspace/setCreateTabIntro' ,
67+ payload : null ,
68+ } )
69+ }
70+ } , [ createTabIntro ] )
3371
72+ // 监听双击树节点事件 生成console
3473 useEffect ( ( ) => {
3574 if ( ! doubleClickTreeNodeData ) {
3675 return ;
@@ -239,7 +278,6 @@ const WorkspaceRight = memo<IProps>(function (props) {
239278 }
240279 } , [ openConsoleList ] ) ;
241280
242-
243281 function createConsole ( params : {
244282 doubleClickTreeNodeData : any ,
245283 name : string ,
@@ -288,8 +326,8 @@ const WorkspaceRight = memo<IProps>(function (props) {
288326 } ) ;
289327 }
290328
291- function onChange ( key : number | string ) {
292- setActiveConsoleId ( + key ) ;
329+ function onChange ( key : any ) {
330+ setActiveConsoleId ( key ) ;
293331 }
294332
295333 const onEdit = ( action : 'add' | 'remove' , key ?: number ) => {
@@ -315,7 +353,7 @@ const WorkspaceRight = memo<IProps>(function (props) {
315353 type : databaseType ,
316354 status : ConsoleStatus . DRAFT ,
317355 tabOpened : ConsoleOpenedStatus . IS_OPEN ,
318- operationType : OperationType . CONSOLE ,
356+ operationType : TabType . CONSOLE ,
319357 } ;
320358 historyService . saveConsole ( params ?. newConsole || p ) . then ( ( res ) => {
321359 params ?. callback ?.( res ) ;
@@ -403,7 +441,7 @@ const WorkspaceRight = memo<IProps>(function (props) {
403441
404442 return (
405443 < div className = { classnames ( styles . box , className ) } >
406- < LoadingContent data = { openConsoleList } handleEmpty empty = { renderEmpty ( ) } >
444+ < LoadingContent data = { tabList } handleEmpty empty = { renderEmpty ( ) } >
407445 < div className = { styles . tabBox } >
408446 < Tabs
409447 className = { styles . tabs }
@@ -412,37 +450,41 @@ const WorkspaceRight = memo<IProps>(function (props) {
412450 editableName = { true }
413451 editableNameOnBlur = { editableNameOnBlur }
414452 activeTab = { activeConsoleId }
415- tabs = { ( openConsoleList || [ ] ) ?. map ( ( t , i ) => {
453+ tabs = { ( tabList || [ ] ) ?. map ( ( t , i ) => {
416454 return {
417- prefixIcon : operationTypeConfig [ t . operationType ] ?. icon || operationTypeConfig . console . icon ,
455+ prefixIcon : t . icon ,
418456 label : t . name ,
419457 value : t . id ,
420458 } ;
421459 } ) }
422460 />
423461 </ div >
424- { openConsoleList ?. map ( ( t , index ) => {
462+ { tabList ?. map ( ( t , index ) => {
425463 return (
426464 < div
427465 key = { t . id }
428466 className = { classnames ( styles . consoleBox , { [ styles . activeConsoleBox ] : activeConsoleId === t . id } ) }
429467 >
430- < DatabaseTableEditor />
431- { /* <WorkspaceRightItem
432- isActive={activeConsoleId === t.id}
433- data={{
434- initDDL: t.ddl,
435- databaseName: curWorkspaceParams.databaseName!,
436- dataSourceId: curWorkspaceParams.dataSourceId!,
437- type: curWorkspaceParams.databaseType!,
438- schemaName: curWorkspaceParams?.schemaName!,
439- consoleId: t.id,
440- consoleName: t.name,
441- }}
442- workspaceModel={workspaceModel}
443- aiModel={aiModel}
444- dispatch={dispatch}
445- /> */ }
468+ {
469+ [ TabType . CONSOLE , TabType . FUNCTION , TabType . PROCEDURE , TabType . TRIGGER , TabType . VIEW ] . includes ( t . tabType ) && < SQLExecute
470+ isActive = { activeConsoleId === t . id }
471+ data = { {
472+ initDDL : t . ddl ,
473+ databaseName : curWorkspaceParams . databaseName ! ,
474+ dataSourceId : curWorkspaceParams . dataSourceId ! ,
475+ type : curWorkspaceParams . databaseType ! ,
476+ schemaName : curWorkspaceParams ?. schemaName ! ,
477+ consoleId : t . id ,
478+ consoleName : t . name ,
479+ } }
480+ workspaceModel = { workspaceModel }
481+ aiModel = { aiModel }
482+ dispatch = { dispatch }
483+ />
484+ }
485+ {
486+ t . tabType === TabType . EditTable && < DatabaseTableEditor />
487+ }
446488 </ div >
447489 ) ;
448490 } ) }
0 commit comments