@@ -19,8 +19,9 @@ import { AIType } from '@/typings/ai';
1919import i18n from '@/i18n' ;
2020import configService from '@/service/config' ;
2121// import NewEditor from './NewMonacoEditor';
22- import styles from './index.less ' ;
22+ import sqlService from '@/service/sql ' ;
2323import indexedDB from '@/indexedDB' ;
24+ import styles from './index.less' ;
2425
2526enum 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 ) ;
@@ -195,6 +197,37 @@ function Console(props: IProps, ref: ForwardedRef<IConsoleRef>) {
195197 }
196198 } , [ ] ) ;
197199
200+ // TODO: 暂时写在这里,后续去掉
201+ useEffect ( ( ) => {
202+ if ( ! props . executeParams ) {
203+ return ;
204+ }
205+
206+ if ( ! props . tables || props . tables . length === 0 ) {
207+ setTableNameList ( [ ] ) ;
208+ setSelectedTables ( [ ] ) ;
209+ return
210+ }
211+
212+
213+ const { dataSourceId, databaseName, schemaName } = props . executeParams ;
214+ sqlService
215+ . getAllTableList ( {
216+ dataSourceId,
217+ databaseName,
218+ schemaName,
219+ } )
220+ . then ( ( data ) => {
221+ const tableNameListTemp = data . map ( ( t ) => t . name ) ;
222+ setTableNameList ( tableNameListTemp ) ;
223+
224+ if ( selectedTables . length === 0 ) {
225+ setSelectedTables ( tableNameListTemp . slice ( 0 , 1 ) ) ;
226+ }
227+ } ) ;
228+ // debugger
229+ } , [ props . tables ] ) ;
230+
198231 function timingAutoSave ( _status ?: ConsoleStatus ) {
199232 if ( timerRef . current ) {
200233 clearInterval ( timerRef . current ) ;
@@ -220,16 +253,6 @@ function Console(props: IProps, ref: ForwardedRef<IConsoleRef>) {
220253 } , 5000 ) ;
221254 }
222255
223- const tableListName = useMemo ( ( ) => {
224- const tableList = ( props . tables || [ ] ) . map ( ( t ) => t . name ) ;
225-
226- // 默认选中前八个
227- // setSelectedTables(tableList.slice(0, 8));
228- setSelectedTables ( tableList . slice ( 0 , 1 ) ) ;
229-
230- return tableList ;
231- } , [ props . tables ] ) ;
232-
233256 const handleApiKeyEmptyOrGetQrCode = async ( shouldPoll ?: boolean ) => {
234257 setIsLoading ( true ) ;
235258 try {
@@ -511,7 +534,7 @@ function Console(props: IProps, ref: ForwardedRef<IConsoleRef>) {
511534 aiType = { aiModel . aiConfig ?. aiSqlSource }
512535 remainingUse = { aiModel . remainingUse }
513536 remainingBtnLoading = { props . remainingBtnLoading }
514- tables = { tableListName }
537+ tables = { tableNameList }
515538 onPressEnter = { ( value : string ) => {
516539 // editorRef?.current?.toFocus();
517540 handleAiChat ( value , IPromptType . NL_2_SQL ) ;
0 commit comments