Skip to content

Commit fdd0532

Browse files
committed
feat: 打开console精确定位tab
1 parent 755b093 commit fdd0532

7 files changed

Lines changed: 74 additions & 19 deletions

File tree

chat2db-client/.umirc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default defineConfig({
4444
changeOrigin: true,
4545
},
4646
},
47-
targets:{
47+
targets: {
4848
chrome: 80,
4949
},
5050
headScripts: [

chat2db-client/.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"CLICKHOUSE",
2525
"Cascader",
2626
"DBAI",
27+
"Dmaven",
2728
"Iconfont",
2829
"JDBC",
2930
"KEYPAIR",
@@ -47,6 +48,7 @@
4748
"lsof",
4849
"netstat",
4950
"pgsql",
51+
"pnpm",
5052
"remaininguses",
5153
"sortablejs",
5254
"umijs",

chat2db-client/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
具体转换在 /theme/index.ts 中的 InjectThemeVar
3131
- js 在window._AppThemePack中去取 eg:`window._AppThemePack.controlItemBgActive` // good
3232
- css eg: `background: var(--control-item-bg-active)` // good
33-
- css `color: #fff` ` // bad
33+
- css `color: #fff` // bad
3434

3535
## 如何使用国际化
3636

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default memo<IProps>(function Tab(props) {
8787
>
8888
{
8989
t.value === editingTab ?
90-
<input onChange={(e) => { inputOnChange(e.target.value) }} className={styles.input} autoFocus onBlur={onBlur} type="text" />
90+
<input value={t.label as string} onChange={(e) => { inputOnChange(e.target.value) }} className={styles.input} autoFocus onBlur={onBlur} type="text" />
9191
:
9292
<div className={styles.text} key={t.value} onClick={changeTab.bind(null, t)}>
9393
{t.label}

chat2db-client/src/models/workspace.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface IWorkspaceModelState {
2222
// 双击树node节点
2323
doubleClickTreeNodeData: ITreeNode | undefined;
2424
consoleList: IConsole[];
25+
curConsoleId: number | null;
2526
openConsoleList: IConsole[];
2627
curTableList: ITreeNode[];
2728
}
@@ -30,12 +31,14 @@ export interface IWorkspaceModelType {
3031
namespace: 'workspace';
3132
state: IWorkspaceModelState;
3233
reducers: {
33-
setDatabaseAndSchema: Reducer<IWorkspaceModelState['databaseAndSchema']>;
34-
setCurWorkspaceParams: Reducer<IWorkspaceModelState['curWorkspaceParams']>;
35-
setDoubleClickTreeNodeData: Reducer<any>; //TS TODO:
36-
setConsoleList: Reducer<IWorkspaceModelState['consoleList']>;
37-
setOpenConsoleList: Reducer<IWorkspaceModelState['consoleList']>;
38-
setCurTableList: Reducer<IWorkspaceModelState['curTableList']>;
34+
// TS TODO:
35+
setDatabaseAndSchema: Reducer<IWorkspaceModelState>;
36+
setCurWorkspaceParams: Reducer<IWorkspaceModelState>;
37+
setDoubleClickTreeNodeData: Reducer<IWorkspaceModelState>;
38+
setConsoleList: Reducer<IWorkspaceModelState>;
39+
setOpenConsoleList: Reducer<IWorkspaceModelState>;
40+
setCurConsoleId: Reducer<IWorkspaceModelState>;
41+
setCurTableList: Reducer<IWorkspaceModelState>;
3942
};
4043
effects: {
4144
fetchDatabaseAndSchema: Effect;
@@ -56,6 +59,7 @@ const WorkspaceModel: IWorkspaceModelType = {
5659
consoleList: [],
5760
openConsoleList: [],
5861
curTableList: [],
62+
curConsoleId: null
5963
},
6064

6165
reducers: {
@@ -89,13 +93,22 @@ const WorkspaceModel: IWorkspaceModelType = {
8993
};
9094
},
9195

96+
// 工作台页面打开的console列表
9297
setOpenConsoleList(state, { payload }) {
9398
return {
9499
...state,
95100
openConsoleList: payload,
96101
};
97102
},
98103

104+
// 当前聚焦的console
105+
setCurConsoleId(state, { payload }) {
106+
return {
107+
...state,
108+
curConsoleId: payload
109+
}
110+
},
111+
99112
setCurTableList(state, { payload }) {
100113
return {
101114
...state,

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ const dvaModel = connect(
3434

3535
const WorkspaceLeft = memo<IProps>(function (props) {
3636
const { className, workspaceModel, dispatch } = props;
37-
const { curWorkspaceParams } = workspaceModel;
38-
37+
const { curWorkspaceParams, openConsoleList } = workspaceModel;
3938

4039
function getConsoleList() {
4140
let p: any = {
4241
pageNo: 1,
4342
pageSize: 999,
43+
orderByDesc: false,
4444
tabOpened: ConsoleOpenedStatus.IS_OPEN,
4545
...curWorkspaceParams,
4646
};
@@ -60,7 +60,7 @@ const WorkspaceLeft = memo<IProps>(function (props) {
6060
const addConsole = (params?: ICreateConsole) => {
6161
const { dataSourceId, databaseName, schemaName, databaseType } = curWorkspaceParams
6262
let p = {
63-
name: `new console`,
63+
name: `new console${openConsoleList?.length || ''}`,
6464
ddl: '',
6565
dataSourceId: dataSourceId!,
6666
databaseName: databaseName!,
@@ -70,12 +70,16 @@ const WorkspaceLeft = memo<IProps>(function (props) {
7070
tabOpened: ConsoleOpenedStatus.IS_OPEN,
7171
}
7272
historyService.saveConsole(params || p).then(res => {
73+
dispatch({
74+
type: 'workspace/setCurConsoleId',
75+
payload: res,
76+
});
7377
getConsoleList();
7478
})
7579
}
7680

7781
function createConsole() {
78-
addConsole()
82+
addConsole();
7983
}
8084

8185
return (
@@ -214,6 +218,7 @@ const RenderSaveBox = dvaModel(function (props: any) {
214218
payload: {
215219
pageNo: 1,
216220
pageSize: 999,
221+
orderByDesc: true,
217222
status: ConsoleStatus.RELEASE,
218223
...curWorkspaceParams,
219224
},
@@ -252,14 +257,22 @@ const RenderSaveBox = dvaModel(function (props: any) {
252257
}
253258

254259
function openConsole(data: IConsole) {
260+
255261
let p: any = {
256262
id: data.id,
257263
tabOpened: ConsoleOpenedStatus.IS_OPEN
258264
};
259265
historyServer.updateSavedConsole(p).then((res) => {
266+
267+
dispatch({
268+
type: 'workspace/setCurConsoleId',
269+
payload: data.id,
270+
});
271+
260272
dispatch({
261273
type: 'workspace/fetchGetSavedConsole',
262274
payload: {
275+
orderByDesc: false,
263276
tabOpened: ConsoleOpenedStatus.IS_OPEN,
264277
...curWorkspaceParams
265278
},
@@ -281,6 +294,7 @@ const RenderSaveBox = dvaModel(function (props: any) {
281294
dispatch({
282295
type: 'workspace/fetchGetSavedConsole',
283296
payload: {
297+
orderByDesc: true,
284298
tabOpened: ConsoleOpenedStatus.IS_OPEN,
285299
...curWorkspaceParams
286300
},
@@ -294,8 +308,8 @@ const RenderSaveBox = dvaModel(function (props: any) {
294308
dispatch({
295309
type: 'workspace/fetchGetSavedConsole',
296310
payload: {
311+
orderByDesc: true,
297312
status: ConsoleStatus.RELEASE,
298-
orderByDesc: Boolean,
299313
...curWorkspaceParams
300314
},
301315
callback: (res: any) => {

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import WorkspaceRightItem from '../WorkspaceRightItem';
1111
import { IWorkspaceModelState, IWorkspaceModelType } from '@/models/workspace';
1212
import { IAIState } from '@/models/ai';
1313
import { handleLocalStorageSavedConsole } from '@/utils';
14+
import { useUpdateEffect } from '@/hooks/useUpdateEffect';
1415

1516
interface IProps {
1617
className?: string;
@@ -22,7 +23,7 @@ interface IProps {
2223
const WorkspaceRight = memo<IProps>(function (props) {
2324
const [activeConsoleId, setActiveConsoleId] = useState<number>();
2425
const { className, aiModel, workspaceModel, dispatch } = props;
25-
const { curWorkspaceParams, doubleClickTreeNodeData, openConsoleList } = workspaceModel;
26+
const { curWorkspaceParams, doubleClickTreeNodeData, openConsoleList, curConsoleId } = workspaceModel;
2627

2728
useEffect(() => {
2829
// 这里只处理没有console的情况下
@@ -52,21 +53,46 @@ const WorkspaceRight = memo<IProps>(function (props) {
5253
});
5354
}, [doubleClickTreeNodeData]);
5455

56+
useUpdateEffect(() => {
57+
if (activeConsoleId) {
58+
localStorage.setItem('active-console-id', activeConsoleId.toString())
59+
} else {
60+
localStorage.removeItem('active-console-id')
61+
}
62+
}, [activeConsoleId])
63+
5564
useEffect(() => {
65+
const newActiveConsoleId = curConsoleId || activeConsoleId || Number(localStorage.getItem('active-console-id') || 0);
66+
// 用完之后就清掉curConsoleId
5667
if (!openConsoleList?.length) {
5768
setActiveConsoleId(undefined);
58-
} else if (!activeConsoleId) {
69+
} else if (!newActiveConsoleId) {
5970
setActiveConsoleId(openConsoleList[0].id);
6071
} else {
72+
// 如果你指定了让我打开哪个那我就打开哪个
73+
if (curConsoleId) {
74+
setActiveConsoleId(curConsoleId);
75+
dispatch({
76+
type: 'workspace/setCurConsoleId',
77+
payload: null,
78+
});
79+
return
80+
}
81+
6182
let flag = false;
6283
openConsoleList?.forEach((t) => {
63-
if (t.id === activeConsoleId) {
84+
if (t.id === newActiveConsoleId) {
6485
flag = true;
6586
}
6687
});
67-
if (!flag) {
88+
if (flag) {
89+
setActiveConsoleId(newActiveConsoleId);
90+
} else {
91+
// 如果发现当前列表里并没有newActiveConsoleId
6892
setActiveConsoleId(openConsoleList?.[openConsoleList?.length - 1].id);
6993
}
94+
95+
7096
}
7197
}, [openConsoleList]);
7298

@@ -178,7 +204,7 @@ const WorkspaceRight = memo<IProps>(function (props) {
178204
payload: {
179205
pageNo: 1,
180206
pageSize: 999,
181-
orderByDesc: false,
207+
orderByDesc: true,
182208
status: ConsoleStatus.RELEASE,
183209
...curWorkspaceParams,
184210
},

0 commit comments

Comments
 (0)