Skip to content

Commit

Permalink
Automatic selection table
Browse files Browse the repository at this point in the history
  • Loading branch information
shanhexi committed Nov 28, 2023
1 parent fb9ee97 commit a440573
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useMemo, useState, memo } from 'react';
import { IBoundInfo } from '../../index';
import React, { useEffect, useMemo, useState, memo, useContext } from 'react';
import { IBoundInfo, IntelligentEditorContext } from '../../index';
import { Dropdown } from 'antd';
import { useConnectionStore } from '@/pages/main/store/connection';
import connectionService from '@/service/connection';
Expand Down Expand Up @@ -36,6 +36,7 @@ const emptyOption = {

const SelectBoundInfo = memo((props: IProps) => {
const { boundInfo, setBoundInfo } = props;
const { selectedTables, setSelectedTables, setTableNameList } = useContext(IntelligentEditorContext);
const connectionList = useConnectionStore((state) => state.connectionList);
const [databaseNameList, setDatabaseNameList] = useState<IOption<string>[]>([emptyOption]);
const [schemaList, setSchemaList] = useState<IOption<string>[]>([emptyOption]);
Expand Down Expand Up @@ -213,10 +214,9 @@ const SelectBoundInfo = memo((props: IProps) => {
boundInfo.databaseName,
boundInfo.schemaName,
);
// setTableNameList(tableNameListTemp);
// if (selectedTables.length === 0) {
// setSelectedTables(tableNameListTemp.slice(0, 1));
// }

setTableNameList(tableNameListTemp);
setSelectedTables(tableNameListTemp.slice(0, 1));
});
};

Expand Down
38 changes: 20 additions & 18 deletions chat2db-client/src/components/ConsoleEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ import configService from '@/service/config';
import styles from './index.less';

// ----- hooks -----
import { useModuleData } from './hooks/useModuleData';
import { useSaveEditorData } from './hooks/useSaveEditorData';

// ----- store -----
import { useSettingStore, fetchRemainingUse, setAiConfig } from '@/store/setting';
import { useSettingStore, fetchRemainingUse, setAiConfig } from '@/store/setting';

enum IPromptType {
NL_2_SQL = 'NL_2_SQL',
Expand Down Expand Up @@ -81,9 +80,11 @@ export interface IConsoleRef {
}

interface IIntelligentEditorContext {
boundInfo: IBoundInfo;
setBoundInfo: (params: IBoundInfo) => void;
isActive: boolean;
tableNameList: string[];
setTableNameList: (tables: string[]) => void;
selectedTables: string[];
setSelectedTables: (tables: string[]) => void;
}

export const IntelligentEditorContext = createContext<IIntelligentEditorContext>({} as any);
Expand Down Expand Up @@ -112,16 +113,15 @@ function ConsoleEditor(props: IProps, ref: ForwardedRef<IConsoleRef>) {
const [isStream, setIsStream] = useState(false);
const aiFetchIntervalRef = useRef<any>();
const closeEventSource = useRef<any>();
const {aiConfig,hasWhite,remainingUse} = useSettingStore(state => {
const { aiConfig, hasWhite, remainingUse } = useSettingStore((state) => {
return {
aiConfig: state.aiConfig,
hasWhite: state.hasWhite,
remainingUse: state.remainingUse,
}
};
});

// ---------------- new-code ----------------
const { tableNameList, selectedTables, setSelectedTables } = useModuleData({ boundInfo: props.boundInfo });
const { saveConsole } = useSaveEditorData({
editorRef,
boundInfo: props.boundInfo,
Expand All @@ -134,10 +134,7 @@ function ConsoleEditor(props: IProps, ref: ForwardedRef<IConsoleRef>) {
/**
* 当前选择的AI类型是Chat2DBAI
*/
const isChat2DBAI = useMemo(
() => aiConfig?.aiSqlSource === AIType.CHAT2DBAI,
[aiConfig?.aiSqlSource],
);
const isChat2DBAI = useMemo(() => aiConfig?.aiSqlSource === AIType.CHAT2DBAI, [aiConfig?.aiSqlSource]);

useEffect(() => {
handleSelectTableSyncModel();
Expand Down Expand Up @@ -179,13 +176,13 @@ function ConsoleEditor(props: IProps, ref: ForwardedRef<IConsoleRef>) {
}
if (apiKey) {
setPopularizeModal(false);

setAiConfig({
...(aiConfig || {}),
apiKey,
apiKey,
});

fetchRemainingUse(apiKey)
fetchRemainingUse(apiKey);
}
}, 3000);
}
Expand Down Expand Up @@ -235,7 +232,7 @@ function ConsoleEditor(props: IProps, ref: ForwardedRef<IConsoleRef>) {
closeEventSource.current();
setIsStream(false);
if (isChat2DBAI) {
fetchRemainingUse(apiKey)
fetchRemainingUse(apiKey);
}
if (isNL2SQL) {
editorRef?.current?.setValue('\n');
Expand Down Expand Up @@ -273,7 +270,7 @@ function ConsoleEditor(props: IProps, ref: ForwardedRef<IConsoleRef>) {
setIsLoading(false);
hasErrorToLogin && handleApiKeyEmptyOrGetQrCode(true);
// hasErrorToInvite && handleClickRemainBtn();
fetchRemainingUse(apiKey)
fetchRemainingUse(apiKey);
return;
}

Expand Down Expand Up @@ -367,12 +364,17 @@ function ConsoleEditor(props: IProps, ref: ForwardedRef<IConsoleRef>) {
setSyncTableModel(hasAiAccess ? SyncModelType.AUTO : SyncModelType.MANUAL);
};

const [selectedTables, setSelectedTables] = useState<string[]>([]);
const [tableNameList, setTableNameList] = useState<string[]>([]);

return (
<IntelligentEditorContext.Provider
value={{
boundInfo,
setBoundInfo,
isActive,
tableNameList,
setTableNameList,
selectedTables,
setSelectedTables,
}}
>
<div className={styles.console} ref={ref as any}>
Expand Down

0 comments on commit a440573

Please sign in to comment.