Skip to content

Commit 01d0c8d

Browse files
author
单贺喜
committed
connection
1 parent 39805b6 commit 01d0c8d

9 files changed

Lines changed: 117 additions & 88 deletions

File tree

chat2db-client/src/components/Console/index.tsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { useEffect, useMemo, useRef, useState } from 'react';
2+
import { connect } from 'umi';
23
import { formatParams } from '@/utils/common';
34
import connectToEventSource from '@/utils/eventSource';
45
import { Button, Spin, message, notification } from 'antd';
@@ -8,11 +9,11 @@ import { format } from 'sql-formatter';
89
import sqlServer from '@/service/sql';
910
import historyServer from '@/service/history';
1011
import { v4 as uuidv4 } from 'uuid';
11-
1212
import styles from './index.less';
1313
import Loading from '../Loading/Loading';
14-
import { DatabaseTypeCode } from '@/constants';
14+
import { DatabaseTypeCode, ConsoleStatus } from '@/constants';
1515
import Iconfont from '../Iconfont';
16+
import { IWorkspaceModelType } from '@/models/workspace';
1617

1718
enum IPromptType {
1819
NL_2_SQL = 'NL_2_SQL',
@@ -54,10 +55,12 @@ interface IProps {
5455
consoleName: string;
5556
};
5657
onExecuteSQL: (value: any) => void;
58+
workspaceModel: IWorkspaceModelType;
59+
dispatch: any;
5760
}
5861

5962
function Console(props: IProps, ref: any) {
60-
const { hasAiChat = true, executeParams, appendValue, isActive } = props;
63+
const { hasAiChat = true, executeParams, appendValue, isActive, dispatch } = props;
6164
const uid = useMemo(() => uuidv4(), []);
6265
const chatResult = useRef('');
6366
const editorRef = useRef<IExportRefFunction>();
@@ -138,17 +141,18 @@ function Console(props: IProps, ref: any) {
138141
const a = editorRef.current?.getAllContent()
139142

140143
let p = {
141-
...executeParams,
142144
id: executeParams.consoleId,
143-
name: executeParams.consoleName,
144-
ddl: value || editorRef.current?.getAllContent()!,
145+
status: ConsoleStatus.RELEASE
145146
};
146-
historyServer.updateWindowTab(p).then((res) => {
147-
// message.success('保存成功');
148-
notification.open({
149-
type: 'success',
150-
message: '保存成功',
151-
});
147+
historyServer.updateSavedConsole(p).then((res) => {
148+
message.success('保存成功');
149+
dispatch({
150+
type: 'workspace/fetchGetSavedConsole'
151+
})
152+
// notification.open({
153+
// type: 'success',
154+
// message: '保存成功',
155+
// });
152156
});
153157
};
154158

@@ -213,4 +217,8 @@ function Console(props: IProps, ref: any) {
213217
);
214218
}
215219

216-
export default Console;
220+
const dvaModel = connect(({ workspace }: { workspace: IWorkspaceModelType }) => ({
221+
workspaceModel: workspace
222+
}))
223+
224+
export default dvaModel(Console);

chat2db-client/src/components/Tabs/index.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@ export interface IOnchangeProps {
1717
interface IProps {
1818
className?: string;
1919
tabs: IOption[];
20+
activeTab?: number;
2021
onChange: (key: IOption['value']) => void;
2122
onEdit: (action: 'add' | 'remove', key?: IOption['value']) => void;
2223
}
2324

2425
export default memo<IProps>(function Tab(props) {
25-
const { className, tabs, onChange, onEdit } = props;
26+
const { className, tabs, onChange, onEdit, activeTab } = props;
2627
const [internalTabs, setInternalTabs] = useState<IOption[]>(lodash.cloneDeep(tabs));
27-
const [activeTab, setActiveTab] = useState(internalTabs[0]?.value);
28+
const [internalActiveTab, setInternalActiveTab] = useState<number | undefined>(internalTabs[0]?.value);
29+
30+
useEffect(() => {
31+
setInternalActiveTab(activeTab)
32+
}, [activeTab])
2833

2934
useEffect(() => {
3035
setInternalTabs(lodash.cloneDeep(tabs));
@@ -37,7 +42,7 @@ export default memo<IProps>(function Tab(props) {
3742
}
3843

3944
function changeTab(data: IOption) {
40-
setActiveTab(data.value);
45+
setInternalActiveTab(data.value);
4146
onChange(data.value)
4247

4348
}
@@ -53,7 +58,7 @@ export default memo<IProps>(function Tab(props) {
5358
{
5459
internalTabs.map(t => {
5560
return <div
56-
className={classnames(styles.tabItem, { [styles.activeTab]: t.value === activeTab })}
61+
className={classnames(styles.tabItem, { [styles.activeTab]: t.value === internalActiveTab })}
5762
key={t.value}
5863
onClick={changeTab.bind(null, t)}
5964
>

chat2db-client/src/constants/common.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,3 @@ export enum ConsoleStatus {
2222
DRAFT = 'DRAFT',
2323
RELEASE = 'RELEASE',
2424
}
25-
26-
/** console顶部注释 */
27-
export const consoleTopComment = `-- Chat2DB自然语言转SQL等AI功能 >> https://github.com/alibaba/Chat2DB/blob/main/CHAT2DB_AI_SQL.md\n\n`;

chat2db-client/src/models/workspace.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { getCurrentWorkspaceDatabase, setCurrentWorkspaceDatabase } from '@/utils/localStorage';
22
import sqlService, { MetaSchemaVO } from '@/service/sql';
3-
import { DatabaseTypeCode } from '@/constants';
3+
import historyService from '@/service/history'
4+
import { DatabaseTypeCode, ConsoleStatus } from '@/constants';
45
import { Effect, Reducer } from 'umi';
5-
import { ITreeNode } from '@/typings';
6+
import { ITreeNode, IConsole } from '@/typings';
67

78
export type ICurWorkspaceParams = {
89
dataSourceId: number;
@@ -14,11 +15,12 @@ export type ICurWorkspaceParams = {
1415

1516
export interface IState {
1617
// 当前连接下的及联databaseAndSchema数据
17-
databaseAndSchema: MetaSchemaVO;
18+
databaseAndSchema: MetaSchemaVO | undefined;
1819
// 当前工作区所需的参数
1920
curWorkspaceParams: ICurWorkspaceParams;
2021
// 双击树node节点
2122
doubleClickTreeNodeData: ITreeNode | undefined;
23+
consoleList: IConsole[];
2224
}
2325

2426
export interface IWorkspaceModelType {
@@ -28,19 +30,22 @@ export interface IWorkspaceModelType {
2830
setDatabaseAndSchema: Reducer<IState['databaseAndSchema']>;
2931
setCurWorkspaceParams: Reducer<IState['curWorkspaceParams']>;
3032
setDoubleClickTreeNodeData: Reducer<any>; //TS TODO:
33+
setConsoleList: Reducer<IState['consoleList']>;
3134
};
3235
effects: {
3336
fetchDatabaseAndSchema: Effect;
37+
fetchGetSavedConsole: Effect;
3438
};
3539
}
3640

3741
const WorkspaceModel: IWorkspaceModelType = {
3842
namespace: 'workspace',
3943

4044
state: {
41-
databaseAndSchema: {},
45+
databaseAndSchema: undefined,
4246
curWorkspaceParams: getCurrentWorkspaceDatabase(),
4347
doubleClickTreeNodeData: undefined,
48+
consoleList: []
4449
},
4550

4651
reducers: {
@@ -66,18 +71,34 @@ const WorkspaceModel: IWorkspaceModelType = {
6671
doubleClickTreeNodeData: payload,
6772
};
6873
},
74+
75+
setConsoleList(state, { payload }) {
76+
return {
77+
...state,
78+
consoleList: payload,
79+
};
80+
},
6981
},
7082

7183
effects: {
72-
*fetchDatabaseAndSchema({ payload }, action) {
73-
const { put } = action;
74-
// ts-ignore
84+
*fetchDatabaseAndSchema({ payload }, { put }) {
7585
const res = yield sqlService.getDatabaseSchemaList(payload);
7686
yield put({
7787
type: 'setDatabaseAndSchema',
7888
payload: res,
7989
});
8090
},
91+
*fetchGetSavedConsole({ payload }, { put }) {
92+
const res = yield historyService.getSavedConsoleList({
93+
pageNo: 1,
94+
pageSize: 999,
95+
status: ConsoleStatus.RELEASE
96+
});
97+
yield put({
98+
type: 'setConsoleList',
99+
payload: res.data,
100+
});
101+
},
81102
},
82103
};
83104

chat2db-client/src/pages/main/workspace/components/WorkspaceLeft/index.tsx

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import { IConnectionModelType } from '@/models/connection';
1010
import { IWorkspaceModelType } from '@/models/workspace';
1111
import Tree from '../Tree';
1212
import { treeConfig } from '../Tree/treeConfig';
13-
import { TreeNodeType } from '@/constants';
13+
import { TreeNodeType, ConsoleStatus } from '@/constants';
1414
import { ITreeNode } from '@/typings';
1515
import { IConsole } from '@/typings';
1616
import styles from './index.less';
17+
import { State } from '@/components/StateIndicator';
1718

1819
interface IProps {
1920
className?: string;
@@ -52,8 +53,8 @@ interface IProps {
5253

5354
function handleDatabaseAndSchema(databaseAndSchema: IWorkspaceModelType['state']['databaseAndSchema']) {
5455
let newCascaderOptions: Option[] = [];
55-
if (databaseAndSchema.databases) {
56-
newCascaderOptions = databaseAndSchema.databases.map(t => {
56+
if (databaseAndSchema?.databases) {
57+
newCascaderOptions = databaseAndSchema?.databases.map(t => {
5758
let schemasList: Option[] = []
5859
if (t.schemas) {
5960
schemasList = t.schemas.map(t => {
@@ -84,11 +85,12 @@ const RenderSelectDatabase = dvaModel(function (props: IProps) {
8485
const { connectionModel, workspaceModel, dispatch } = props;
8586
const { databaseAndSchema, curWorkspaceParams } = workspaceModel;
8687
const { curConnection } = connectionModel;
88+
const [currentSelectedName, setCurrentSelectedName] = useState('');
8789

8890
const cascaderOptions = useMemo(() => {
8991
const res = handleDatabaseAndSchema(databaseAndSchema);
90-
// 如果databaseAndSchema 发生切变 并且没选中确切的database时,需要默认选中第一个
91-
if (!curWorkspaceParams.dataSourceId) {
92+
if (!curWorkspaceParams?.dataSourceId || curWorkspaceParams?.dataSourceId !== curConnection?.id) {
93+
// 如果databaseAndSchema 发生切变 并且没选中确切的database时,需要默认选中第一个
9294
const curWorkspaceParams = {
9395
dataSourceId: curConnection?.id,
9496
databaseSourceName: curConnection?.alias,
@@ -104,6 +106,14 @@ const RenderSelectDatabase = dvaModel(function (props: IProps) {
104106
return res
105107
}, [databaseAndSchema])
106108

109+
useEffect(() => {
110+
if (curWorkspaceParams) {
111+
const { databaseName, schemaName, databaseSourceName } = curWorkspaceParams;
112+
const currentSelectedArr = [databaseSourceName, databaseName, schemaName].filter((t) => t);
113+
setCurrentSelectedName(currentSelectedArr.join('/'));
114+
}
115+
}, [curWorkspaceParams])
116+
107117
const onChange: any = (valueArr: any, selectedOptions: any) => {
108118
let labelArr: string[] = [];
109119
labelArr = selectedOptions.map((t: any) => {
@@ -130,12 +140,6 @@ const RenderSelectDatabase = dvaModel(function (props: IProps) {
130140
</div>
131141
);
132142

133-
function renderCurrentSelected() {
134-
const { databaseName, schemaName, databaseSourceName } = curWorkspaceParams;
135-
const currentSelectedArr = [databaseSourceName, databaseName, schemaName].filter((t) => t);
136-
return currentSelectedArr.join('/');
137-
}
138-
139143
return (
140144
<div className={styles.selectDatabaseBox}>
141145
<Cascader
@@ -146,7 +150,7 @@ const RenderSelectDatabase = dvaModel(function (props: IProps) {
146150
dropdownRender={dropdownRender}
147151
>
148152
<div className={styles.currentDatabase}>
149-
<div className={styles.name}>{renderCurrentSelected() || <span style={{ 'opacity': 0.8 }}>{i18n('workspace.cascader.placeholder')}</span>} </div>
153+
<div className={styles.name}>{currentSelectedName || <span style={{ 'opacity': 0.8 }}>{i18n('workspace.cascader.placeholder')}</span>} </div>
150154
<Iconfont code="&#xe608;" />
151155
</div>
152156
</Cascader>
@@ -165,7 +169,7 @@ const RenderTableBox = dvaModel(function (props: any) {
165169
const [initialData, setInitialData] = useState<ITreeNode[]>([]);
166170

167171
useEffect(() => {
168-
if (curWorkspaceParams.dataSourceId) {
172+
if (curWorkspaceParams?.dataSourceId) {
169173
getInitialData();
170174
}
171175
}, [curWorkspaceParams]);
@@ -177,8 +181,6 @@ const RenderTableBox = dvaModel(function (props: any) {
177181
...curWorkspaceParams,
178182
extraParams: curWorkspaceParams,
179183
}).then((res) => {
180-
console.log(res)
181-
182184
setInitialData(res);
183185
});
184186
}
@@ -194,32 +196,27 @@ const RenderTableBox = dvaModel(function (props: any) {
194196
})
195197

196198
const RenderSaveBox = dvaModel(function (props: any) {
197-
const [savedList, setSaveList] = useState<IConsole[]>([]);
198-
const { workspaceModel } = props;
199-
const { curWorkspaceParams } = workspaceModel;
199+
const { workspaceModel, dispatch } = props;
200+
const { curWorkspaceParams, consoleList } = workspaceModel;
200201

201202
useEffect(() => {
202-
getSaveList();
203-
}, [curWorkspaceParams])
204-
205-
function getSaveList() {
206-
let p: any = {
207-
pageNo: 1,
208-
pageSize: 999,
209-
...curWorkspaceParams
210-
}
211-
212-
historyService.getSaveList(p).then(res => {
213-
setSaveList(res.data)
203+
dispatch({
204+
type: 'workspace/fetchGetSavedConsole',
205+
payload: {
206+
pageNo: 1,
207+
pageSize: 999,
208+
status: ConsoleStatus.RELEASE,
209+
...curWorkspaceParams
210+
}
214211
})
215-
}
212+
}, [curWorkspaceParams])
216213

217214
return <div className={styles.save_box}>
218215
<div className={styles.left_box_title}>Saved</div>
219216
<div className={styles.saveBoxList}>
220-
<LoadingContent data={savedList} handleEmpty>
217+
<LoadingContent data={consoleList} handleEmpty>
221218
{
222-
savedList?.map(t => {
219+
consoleList?.map((t: IConsole) => {
223220
return <div key={t.id} className={styles.saveItem}>
224221
{t.name}
225222
</div>

0 commit comments

Comments
 (0)