Skip to content

Commit 47bc8b2

Browse files
committed
fix: Fix the structure of the synchronization table
1 parent 8585826 commit 47bc8b2

1 file changed

Lines changed: 37 additions & 14 deletions

File tree

  • chat2db-client/src/components/Console

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

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import { AIType } from '@/typings/ai';
1919
import i18n from '@/i18n';
2020
import configService from '@/service/config';
2121
// import NewEditor from './NewMonacoEditor';
22-
import styles from './index.less';
22+
import sqlService from '@/service/sql';
2323
import indexedDB from '@/indexedDB';
24+
import styles from './index.less';
2425

2526
enum IPromptType {
2627
NL_2_SQL = 'NL_2_SQL',
@@ -60,14 +61,13 @@ interface IProps {
6061
value?: string;
6162
executeParams: {
6263
databaseName?: string;
63-
dataSourceId?: number;
64+
dataSourceId: number;
6465
type?: DatabaseTypeCode;
6566
consoleId?: number;
6667
schemaName?: string;
6768
consoleName?: string;
6869
status?: ConsoleStatus;
6970
};
70-
tableList?: ITreeNode[];
7171
editorOptions?: IEditorOptions;
7272
aiModel: IAIState;
7373
dispatch: any;
@@ -110,6 +110,8 @@ function Console(props: IProps, ref: ForwardedRef<IConsoleRef>) {
110110
const timerRef = useRef<any>();
111111
const aiFetchIntervalRef = useRef<any>();
112112
const closeEventSource = useRef<any>();
113+
const [tableNameList, setTableNameList] = useState<string[]>([]);
114+
113115
// 上一次同步的console数据
114116
const lastSyncConsole = useRef<any>(defaultValue);
115117
const [saveStatus, setSaveStatus] = useState<ConsoleStatus>(executeParams.status || ConsoleStatus.DRAFT);
@@ -196,6 +198,37 @@ function Console(props: IProps, ref: ForwardedRef<IConsoleRef>) {
196198
}
197199
}, []);
198200

201+
// TODO: 暂时写在这里,后续去掉
202+
useEffect(() => {
203+
if (!props.executeParams) {
204+
return;
205+
}
206+
207+
if(!props.tables || props.tables.length === 0) {
208+
setTableNameList([]);
209+
setSelectedTables([]);
210+
return
211+
}
212+
213+
214+
const { dataSourceId, databaseName, schemaName } = props.executeParams;
215+
sqlService
216+
.getAllTableList({
217+
dataSourceId,
218+
databaseName,
219+
schemaName,
220+
})
221+
.then((data) => {
222+
const tableNameListTemp = data.map((t) => t.name);
223+
setTableNameList(tableNameListTemp);
224+
225+
if (selectedTables.length === 0) {
226+
setSelectedTables(tableNameListTemp.slice(0, 1));
227+
}
228+
});
229+
// debugger
230+
}, [props.tables]);
231+
199232
function timingAutoSave(_status?: ConsoleStatus) {
200233
if (timerRef.current) {
201234
clearInterval(timerRef.current);
@@ -221,16 +254,6 @@ function Console(props: IProps, ref: ForwardedRef<IConsoleRef>) {
221254
}, 5000);
222255
}
223256

224-
const tableListName = useMemo(() => {
225-
const tableList = (props.tables || []).map((t) => t.name);
226-
227-
// 默认选中前八个
228-
// setSelectedTables(tableList.slice(0, 8));
229-
setSelectedTables(tableList.slice(0, 1));
230-
231-
return tableList;
232-
}, [props.tables]);
233-
234257
const handleApiKeyEmptyOrGetQrCode = async (shouldPoll?: boolean) => {
235258
setIsLoading(true);
236259
try {
@@ -512,7 +535,7 @@ function Console(props: IProps, ref: ForwardedRef<IConsoleRef>) {
512535
aiType={aiModel.aiConfig?.aiSqlSource}
513536
remainingUse={aiModel.remainingUse}
514537
remainingBtnLoading={props.remainingBtnLoading}
515-
tables={tableListName}
538+
tables={tableNameList}
516539
onPressEnter={(value: string) => {
517540
// editorRef?.current?.toFocus();
518541
handleAiChat(value, IPromptType.NL_2_SQL);

0 commit comments

Comments
 (0)