88import { compressToEncodedURIComponent } from 'lz-string' ;
99import { observable } from 'mobx' ;
1010import { observer } from 'mobx-react-lite' ;
11+ import { useCallback , useEffect } from 'react' ;
1112import styled , { css , use } from 'reshadow' ;
1213
1314import { Button , IconOrImage , useErrorDetails , useObservableRef , useStateDelay , useTranslate } from '@cloudbeaver/core-blocks' ;
15+ import { ConnectionInfoResource , createConnectionParam } from '@cloudbeaver/core-connections' ;
1416import { useService } from '@cloudbeaver/core-di' ;
17+ import { CommonDialogService , DialogueStateResult } from '@cloudbeaver/core-dialogs' ;
1518import { ServerErrorType , ServerInternalError } from '@cloudbeaver/core-sdk' ;
1619import { errorOf } from '@cloudbeaver/core-utils' ;
1720import { ConnectionSchemaManagerService } from '@cloudbeaver/plugin-datasource-context-switch' ;
1821import { NavigationTabsService } from '@cloudbeaver/plugin-navigation-tabs' ;
19- import { SqlDataSourceService } from '@cloudbeaver/plugin-sql-editor' ;
22+ import { LocalStorageSqlDataSource , SqlDataSourceService } from '@cloudbeaver/plugin-sql-editor' ;
23+ import { isSQLEditorTab , SqlEditorNavigatorService } from '@cloudbeaver/plugin-sql-editor-navigation-tab' ;
2024
2125import type { IDatabaseDataModel } from '../DatabaseDataModel/IDatabaseDataModel' ;
26+ import { SqlEditorSessionClosedDialog } from './SqlEditorSessionClosedDialog' ;
2227
2328const style = css `
2429 error {
@@ -110,6 +115,9 @@ export const TableError = observer<Props>(function TableError({ model, loading,
110115 const connectionSchemaManagerService = useService ( ConnectionSchemaManagerService ) ;
111116 const sqlDataSourceService = useService ( SqlDataSourceService ) ;
112117 const navigationTabsService = useService ( NavigationTabsService ) ;
118+ const commonDialogService = useService ( CommonDialogService ) ;
119+ const sqlEditorNavigatorService = useService ( SqlEditorNavigatorService ) ;
120+ const connectionInfo = useService ( ConnectionInfoResource ) ;
113121
114122 const errorInfo = useObservableRef < ErrorInfo > (
115123 ( ) => ( {
@@ -128,11 +136,6 @@ export const TableError = observer<Props>(function TableError({ model, loading,
128136 false ,
129137 ) ;
130138
131- if ( errorInfo . error !== model . source . error ) {
132- errorInfo . error = model . source . error || null ;
133- errorInfo . display = ! ! model . source . error ;
134- }
135-
136139 const internalServerError = errorOf ( model . source . error , ServerInternalError ) ;
137140 const error = useErrorDetails ( model . source . error ) ;
138141 const animated = useStateDelay ( ! ! errorInfo . error && ! loading , 1 ) ;
@@ -175,6 +178,50 @@ export const TableError = observer<Props>(function TableError({ model, loading,
175178 } ;
176179 }
177180
181+ // 处理SQL会话关闭错误的重新打开编辑器逻辑
182+ const handleReopenEditor = useCallback ( async ( ) => {
183+ const contextId = navigationTabsService . getView ( ) ?. context . id ?? '' ;
184+ const dataSource = sqlDataSourceService . get ( contextId ) ;
185+
186+ if ( dataSource ) {
187+ const query = dataSource . script || '' ;
188+ const shouldReopen = await commonDialogService . open ( SqlEditorSessionClosedDialog , { query } ) ;
189+ if ( shouldReopen === true || shouldReopen === DialogueStateResult . Resolved ) {
190+ const relatedTab = navigationTabsService . findTab ( isSQLEditorTab ( tab => tab . id === contextId ) ) ;
191+ if ( relatedTab ) {
192+ await navigationTabsService . closeTab ( relatedTab . id , true ) ;
193+
194+ const executionContext = dataSource ?. executionContext ;
195+
196+ if ( executionContext ) {
197+ const connection = executionContext
198+ ? connectionInfo . get ( createConnectionParam ( executionContext . projectId , executionContext . connectionId ) )
199+ : undefined ;
200+
201+ await sqlEditorNavigatorService . openNewEditor ( {
202+ dataSourceKey : LocalStorageSqlDataSource . key ,
203+ connectionKey : connection && createConnectionParam ( connection ) ,
204+ query : query ,
205+ } ) ;
206+ }
207+ }
208+ }
209+ }
210+ } , [ navigationTabsService , sqlDataSourceService , commonDialogService , connectionInfo , sqlEditorNavigatorService ] ) ;
211+
212+ useEffect ( ( ) => {
213+ const SQL_CONTEXT_ERROR_CODE = '508' ;
214+ if ( errorInfo . error !== model . source . error ) {
215+ errorInfo . error = model . source . error || null ;
216+ errorInfo . display = ! ! model . source . error ;
217+ }
218+ console . log ( error , model . source . error ) ;
219+ const isSqlContextError = error . errorCode === SQL_CONTEXT_ERROR_CODE || / S Q L c o n t e x t .* n o t f o u n d / i. test ( error . message || '' ) ;
220+ if ( isSqlContextError ) {
221+ handleReopenEditor ( ) ;
222+ }
223+ } , [ error . message , handleReopenEditor , model . source . error , errorInfo ] ) ;
224+
178225 return styled ( style ) (
179226 < error { ...use ( { animated, collapsed : ! errorInfo . display , errorHidden } ) } className = { className } >
180227 < error-body >
0 commit comments