Skip to content

Commit bb3ac94

Browse files
committed
fix: change tab bug
1 parent 5883b49 commit bb3ac94

File tree

5 files changed

+87
-71
lines changed

5 files changed

+87
-71
lines changed

chat2db-client/src/blocks/Setting/AiSetting/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export default function SettingAI(props: IProps) {
2828
setLoading(true);
2929
try {
3030
const res = await getUser();
31+
// 向cookie中写入当前用户id
32+
const date = new Date('2030-12-30 12:30:00').toUTCString();
33+
document.cookie = `CHAT2DB.USER_ID=${res?.id};Expires=${date}`;
3134
setUserInfo(res);
3235
} finally {
3336
setLoading(false);

chat2db-client/src/components/SearchResult/TableBox/index.tsx

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useMemo, useState } from 'react';
1+
import React, { useEffect, useMemo, useState, useContext } from 'react';
22
import { Dropdown, Input, MenuProps, message, Modal, Space, Popover, Spin, Button } from 'antd';
33
import { BaseTable, ArtColumn, useTablePipeline, features, SortItem } from 'ali-react-table';
44
import styled from 'styled-components';
@@ -26,7 +26,6 @@ import sqlService, { IExportParams, IExecuteSqlParams } from '@/service/sql';
2626

2727
// store
2828
import { useCommonStore } from '@/store/common';
29-
import { useWorkspaceStore } from '@/store/workspace';
3029

3130
// 依赖组件
3231
import ExecuteSQL from '@/components/ExecuteSQL';
@@ -38,6 +37,7 @@ import MonacoEditor from '../../Console/MonacoEditor';
3837
import MyPagination from '../Pagination';
3938
import StatusBar from '../StatusBar';
4039
import RightClickMenu, { AllSupportedMenusType } from '../RightClickMenu';
40+
import { Context } from '../index';
4141

4242
interface ITableProps {
4343
className?: string;
@@ -95,6 +95,7 @@ export default function TableBox(props: ITableProps) {
9595
const { className, outerQueryResultData, tableBoxId } = props;
9696
const [viewTableCellData, setViewTableCellData] = useState<IViewTableCellData | null>(null);
9797
const [, contextHolder] = message.useMessage();
98+
const { activeTabIdRef } = useContext(Context);
9899
const [paginationConfig, setPaginationConfig] = useState<IResultConfig>(defaultPaginationConfig);
99100
// sql查询结果
100101
const [queryResultData, setQueryResultData] = useState<IManageResultData>(outerQueryResultData);
@@ -143,7 +144,6 @@ export default function TableBox(props: ITableProps) {
143144
setFocusedContent: state.setFocusedContent,
144145
};
145146
});
146-
const activeSearchResult = useWorkspaceStore((state) => state.activeTab.activeSearchResult);
147147

148148
const handleExportSQLResult = async (exportType: ExportTypeEnum, exportSize: ExportSizeEnum) => {
149149
const params: IExportParams = {
@@ -875,7 +875,6 @@ export default function TableBox(props: ITableProps) {
875875
maxSize: 1080,
876876
sizes: columnResize,
877877
onChangeSizes: (sizes) => {
878-
console.log(sizes);
879878
sizes[0] = 0;
880879
setColumnResize(sizes);
881880
},
@@ -1212,7 +1211,7 @@ export default function TableBox(props: ITableProps) {
12121211

12131212
return (
12141213
<div className={classnames(className, styles.tableBox, { [styles.noDataTableBox]: !tableData.length })}>
1215-
{activeSearchResult.id === tableBoxId && renderContent()}
1214+
{activeTabIdRef?.current === tableBoxId && renderContent()}
12161215
<Modal
12171216
title={viewTableCellData?.name}
12181217
open={!!viewTableCellData?.name}

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

+43-49
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import React, {
88
ForwardedRef,
99
useImperativeHandle,
1010
Fragment,
11+
createContext,
1112
} from 'react';
1213
import classnames from 'classnames';
1314
import Tabs, { ITabItem } from '@/components/Tabs';
@@ -23,7 +24,6 @@ import i18n from '@/i18n';
2324
import sqlServer, { IExecuteSqlParams } from '@/service/sql';
2425
import { v4 as uuidV4 } from 'uuid';
2526
import { Spin } from 'antd';
26-
import { useWorkspaceStore } from '@/store/workspace';
2727

2828
interface IProps {
2929
className?: string;
@@ -42,12 +42,19 @@ export interface ISearchResultRef {
4242
handleExecuteSQL: (sql: string) => void;
4343
}
4444

45+
interface IContext {
46+
// 这里不用ref的话,会导致切换时闪动
47+
activeTabIdRef: React.MutableRefObject<null | string>
48+
}
49+
50+
export const Context = createContext<IContext>({} as any);
51+
4552
export default forwardRef((props: IProps, ref: ForwardedRef<ISearchResultRef>) => {
4653
const { className, sql, executeSqlParams } = props;
4754
const [resultDataList, setResultDataList] = useState<IManageResultData[]>();
4855
const [tableLoading, setTableLoading] = useState(false);
4956
const controllerRef = useRef<AbortController>();
50-
const setActiveSearchResult = useWorkspaceStore((state) => state.setActiveSearchResult);
57+
const activeTabIdRef = useRef<null | string>(null);
5158

5259
useEffect(() => {
5360
if (sql) {
@@ -92,7 +99,7 @@ export default forwardRef((props: IProps, ref: ForwardedRef<ISearchResultRef>) =
9299
};
93100

94101
const onChange = useCallback((uuid) => {
95-
setActiveSearchResult(uuid);
102+
activeTabIdRef.current = uuid;
96103
}, []);
97104

98105
const renderResult = (queryResultData) => {
@@ -170,59 +177,46 @@ export default forwardRef((props: IProps, ref: ForwardedRef<ISearchResultRef>) =
170177
[resultDataList],
171178
);
172179

173-
// const outputTabAndTabsList = useMemo(() => {
174-
// const params = {
175-
// pageNo: 1,
176-
// pageSize: 10,
177-
// };
178-
179-
// return [
180-
// {
181-
// prefixIcon: <Iconfont key="output" className={styles.outputPrefixIcon} code="&#xe6bb;" />,
182-
// label: 'Output',
183-
// key: 'output',
184-
// children: <Output params={params} />,
185-
// styles: { width: '80px' },
186-
// canClosed: false,
187-
// },
188-
// ...tabsList,
189-
// ];
190-
// }, [tabsList]);
191-
192180
const stopExecuteSql = () => {
193181
controllerRef.current && controllerRef.current.abort();
194182
setResultDataList([]);
195183
setTableLoading(false);
196184
};
197185

198186
return (
199-
<div className={classnames(className, styles.searchResult)}>
200-
{tableLoading ? (
201-
<div className={styles.tableLoading}>
202-
<Spin />
203-
<div className={styles.stopExecuteSql} onClick={stopExecuteSql}>
204-
{i18n('common.button.cancelRequest')}
205-
</div>
206-
</div>
207-
) : (
208-
<>
209-
{tabsList?.length ? (
210-
<Tabs
211-
hideAdd
212-
className={styles.tabs}
213-
onChange={onChange as any}
214-
onEdit={onEdit as any}
215-
items={tabsList}
216-
concealTabHeader={tabsList.length === 1}
217-
/>
218-
) : (
219-
<div className={styles.noData}>
220-
<img src={EmptyImg} />
221-
<p>{i18n('common.text.noData')}</p>
187+
<Context.Provider
188+
value={{
189+
activeTabIdRef: activeTabIdRef,
190+
}}
191+
>
192+
<div className={classnames(className, styles.searchResult)}>
193+
{tableLoading ? (
194+
<div className={styles.tableLoading}>
195+
<Spin />
196+
<div className={styles.stopExecuteSql} onClick={stopExecuteSql}>
197+
{i18n('common.button.cancelRequest')}
222198
</div>
223-
)}
224-
</>
225-
)}
226-
</div>
199+
</div>
200+
) : (
201+
<>
202+
{tabsList?.length ? (
203+
<Tabs
204+
hideAdd
205+
className={styles.tabs}
206+
onChange={onChange as any}
207+
onEdit={onEdit as any}
208+
items={tabsList}
209+
concealTabHeader={tabsList.length === 1}
210+
/>
211+
) : (
212+
<div className={styles.noData}>
213+
<img src={EmptyImg} />
214+
<p>{i18n('common.text.noData')}</p>
215+
</div>
216+
)}
217+
</>
218+
)}
219+
</div>
220+
</Context.Provider>
227221
);
228222
});
+17-17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
export interface IConsoleStore {
22
activeTab: {
33
activeConsole: {
4-
id: string | null;
4+
id: string | number | null;
55
};
66
activeSearchResult: {
7-
id: string | null;
7+
id: string | number | null;
88
};
99
};
10-
setActiveConsole: (id: string | null) => void;
11-
setActiveSearchResult: (id: string | null) => void;
10+
setActiveConsole: (props: IActiveTabData) => void;
11+
setActiveSearchResult: (props: IActiveTabData) => void;
1212
}
1313

14-
export const consoleStore = (set):IConsoleStore => ({
14+
type IActiveTabData = {
15+
id: string | number;
16+
} | null
17+
18+
export const consoleStore = (set): IConsoleStore => ({
1519
activeTab: {
1620
activeConsole: {
1721
id: null,
@@ -20,24 +24,20 @@ export const consoleStore = (set):IConsoleStore => ({
2024
id: null,
2125
},
2226
},
23-
setActiveConsole: (id: string | null) => {
27+
setActiveConsole: (activeConsole: IActiveTabData) => {
2428
return set((state) => ({
2529
activeTab: {
2630
...state.activeTab,
27-
activeConsole: {
28-
id,
29-
},
31+
activeConsole,
3032
},
31-
}))
33+
}));
3234
},
33-
setActiveSearchResult: (id: string | null) => {
35+
setActiveSearchResult: (activeSearchResult: IActiveTabData) => {
3436
return set((state) => ({
3537
activeTab: {
3638
...state.activeTab,
37-
activeSearchResult: {
38-
id,
39-
},
39+
activeSearchResult,
4040
},
41-
}))
42-
}
43-
})
41+
}));
42+
},
43+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ai.chat2db.spi.util;
2+
3+
import ai.chat2db.spi.model.Table;
4+
import ai.chat2db.spi.model.TableColumn;
5+
import org.apache.commons.collections4.CollectionUtils;
6+
7+
public class TableUtils {
8+
9+
public static TableColumn getTableColumn(Table table,String columnName) {
10+
if(table == null || CollectionUtils.isEmpty(table.getColumnList())){
11+
return null ;
12+
}
13+
for (TableColumn tableColumn : table.getColumnList()) {
14+
if(tableColumn.getName().equalsIgnoreCase(columnName)){
15+
return tableColumn ;
16+
}
17+
}
18+
return null;
19+
}
20+
}

0 commit comments

Comments
 (0)