Skip to content

Commit f1e9ba2

Browse files
committed
refactor: remove old code, add todo about problem
1 parent 2e54e31 commit f1e9ba2

File tree

6 files changed

+38
-104
lines changed

6 files changed

+38
-104
lines changed
Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,10 @@
11
/*
22
* CloudBeaver - Cloud Database Manager
3-
* Copyright (C) 2020-2024 DBeaver Corp and others
3+
* Copyright (C) 2020-2025 DBeaver Corp and others
44
*
55
* Licensed under the Apache License, Version 2.0.
66
* you may not use this file except in compliance with the License.
77
*/
8-
import { observer } from 'mobx-react-lite';
9-
import { forwardRef } from 'react';
8+
import { importLazyComponent } from '@cloudbeaver/core-blocks';
109

11-
import { ComplexLoader, createComplexLoader } from '@cloudbeaver/core-blocks';
12-
import type { IDefaultExtensions, IEditorProps, IEditorRef } from '@cloudbeaver/plugin-codemirror6';
13-
14-
const loader = createComplexLoader(async function loader() {
15-
const { SQLCodeEditor } = await import('./SQLCodeEditor.js');
16-
return { SQLCodeEditor };
17-
});
18-
19-
export const SQLCodeEditorLoader = observer<IEditorProps & IDefaultExtensions, IEditorRef>(
20-
forwardRef(function SQLCodeEditorLoader(props, ref) {
21-
return <ComplexLoader loader={loader}>{({ SQLCodeEditor }) => <SQLCodeEditor {...props} ref={ref} />}</ComplexLoader>;
22-
}),
23-
);
10+
export const SQLCodeEditorLoader = importLazyComponent(() => import('./SQLCodeEditor.js').then(m => m.SQLCodeEditor));

webapp/packages/plugin-sql-editor-new/src/SQLEditor/useSqlDialectExtension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* CloudBeaver - Cloud Database Manager
3-
* Copyright (C) 2020-2024 DBeaver Corp and others
3+
* Copyright (C) 2020-2025 DBeaver Corp and others
44
*
55
* Licensed under the Apache License, Version 2.0.
66
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@ const codemirrorComplexLoader = createComplexLoader(() => import('@cloudbeaver/p
2828
const SQL_EDITOR_COMPARTMENT = new Compartment();
2929

3030
export function useSqlDialectExtension(dialectInfo: SqlDialectInfo | undefined): [Compartment, Extension] {
31+
//TODO: probably we need to refactor it to lazy approach without triggering suspense
3132
const { SQLDialect, SQL_EDITOR } = useComplexLoader(codemirrorComplexLoader);
3233
const loader = getDialectLoader(dialectInfo?.name);
3334
const dialect = useComplexLoader(loader);
Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/*
22
* CloudBeaver - Cloud Database Manager
3-
* Copyright (C) 2020-2024 DBeaver Corp and others
3+
* Copyright (C) 2020-2025 DBeaver Corp and others
44
*
55
* Licensed under the Apache License, Version 2.0.
66
* you may not use this file except in compliance with the License.
77
*/
8-
import { ConnectionDialectResource, type IConnectionExecutionContextInfo, type IConnectionInfoParams } from '@cloudbeaver/core-connections';
8+
import { ConnectionDialectResource, type IConnectionExecutionContextInfo } from '@cloudbeaver/core-connections';
99
import { injectable } from '@cloudbeaver/core-di';
1010
import { NotificationService } from '@cloudbeaver/core-events';
11-
import type { SqlDialectInfo } from '@cloudbeaver/core-sdk';
1211

1312
@injectable(() => [ConnectionDialectResource, NotificationService])
1413
export class SqlDialectInfoService {
@@ -25,20 +24,4 @@ export class SqlDialectInfoService {
2524
}
2625
return query;
2726
}
28-
29-
getDialectInfo(key: IConnectionInfoParams): SqlDialectInfo | undefined {
30-
return this.connectionDialectResource.get(key);
31-
}
32-
33-
async loadSqlDialectInfo(key: IConnectionInfoParams): Promise<SqlDialectInfo | undefined> {
34-
if (!this.connectionDialectResource.has(key)) {
35-
try {
36-
return this.connectionDialectResource.load(key);
37-
} catch (error: any) {
38-
this.notificationService.logException(error, 'Failed to load SqlDialectInfo');
39-
}
40-
}
41-
42-
return this.getDialectInfo(key);
43-
}
4427
}
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
/*
22
* CloudBeaver - Cloud Database Manager
3-
* Copyright (C) 2020-2024 DBeaver Corp and others
3+
* Copyright (C) 2020-2025 DBeaver Corp and others
44
*
55
* Licensed under the Apache License, Version 2.0.
66
* you may not use this file except in compliance with the License.
77
*/
8-
import { observer } from 'mobx-react-lite';
98

10-
import { ComplexLoader, createComplexLoader } from '@cloudbeaver/core-blocks';
9+
import { importLazyComponent } from '@cloudbeaver/core-blocks';
1110

12-
import type { ISqlEditorProps } from './ISqlEditorProps.js';
13-
14-
const loader = createComplexLoader(async function loader() {
15-
const { SqlEditor } = await import('./SqlEditor.js');
16-
return { SqlEditor };
17-
});
18-
19-
export const SqlEditorLoader = observer<ISqlEditorProps>(function SqlEditorLoader(props) {
20-
return <ComplexLoader loader={loader}>{({ SqlEditor }) => <SqlEditor {...props} />}</ComplexLoader>;
21-
});
11+
export const SqlEditorLoader = importLazyComponent(() => import('./SqlEditor.js').then(m => m.SqlEditor));

webapp/packages/plugin-sql-editor/src/SqlEditor/useSqlEditor.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { useService } from '@cloudbeaver/core-di';
1313
import { CommonDialogService, DialogueStateResult } from '@cloudbeaver/core-dialogs';
1414
import { NotificationService } from '@cloudbeaver/core-events';
1515
import { SyncExecutor } from '@cloudbeaver/core-executor';
16-
import type { SqlCompletionProposal, SqlDialectInfo, SqlScriptInfoFragment } from '@cloudbeaver/core-sdk';
16+
import type { SqlCompletionProposal, SqlScriptInfoFragment } from '@cloudbeaver/core-sdk';
1717
import { createLastPromiseGetter, type LastPromiseGetter, throttleAsync } from '@cloudbeaver/core-utils';
1818

1919
import type { ISqlEditorTabState } from '../ISqlEditorTabState.js';
@@ -29,7 +29,6 @@ import { SqlResultTabsService } from '../SqlResultTabs/SqlResultTabsService.js';
2929
import type { ISQLEditorData } from './ISQLEditorData.js';
3030
import { SqlEditorSettingsService } from '../SqlEditorSettingsService.js';
3131
import { SqlEditorModelService } from '../SqlEditorModel/SqlEditorModelService.js';
32-
import type { ISqlEditorModel } from '../SqlEditorModel/ISqlEditorModel.js';
3332

3433
interface ISQLEditorDataPrivate extends ISQLEditorData {
3534
readonly sqlDialectInfoService: SqlDialectInfoService;
@@ -66,20 +65,22 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData {
6665
const sqlEditorSettingsService = useService(SqlEditorSettingsService);
6766
const sqlEditorModelService = useService(SqlEditorModelService);
6867

69-
const data = useObservableRef<ISQLEditorDataPrivate>(
70-
() => ({
71-
get model(): ISqlEditorModel {
72-
return sqlEditorModelService.getOrCreate(this.state);
73-
},
74-
get dialect(): SqlDialectInfo | undefined {
75-
const executionContext = this.model.dataSource?.executionContext;
76-
if (!executionContext) {
77-
return undefined;
78-
}
68+
const model = sqlEditorModelService.getOrCreate(state);
7969

80-
return this.sqlDialectInfoService.getDialectInfo(createConnectionParam(executionContext.projectId, executionContext.connectionId));
81-
},
70+
const key = getComputed(() => {
71+
const executionContext = model.dataSource?.executionContext;
72+
if (executionContext) {
73+
const context = connectionExecutionContextService.get(executionContext.id)?.context;
74+
if (context) {
75+
return createConnectionParam(context.projectId, context.connectionId);
76+
}
77+
}
78+
return null;
79+
});
80+
const connectionDialectLoader = useResource(useSqlEditor, ConnectionDialectResource, key);
8281

82+
const data = useObservableRef<ISQLEditorDataPrivate>(
83+
() => ({
8384
get readonly(): boolean {
8485
return this.executingScript || this.readonlyState || !!this.model.dataSource?.isReadonly() || !this.editing;
8586
},
@@ -331,17 +332,21 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData {
331332
executeQueryNewTab: action.bound,
332333
showExecutionPlan: action.bound,
333334
executeScript: action.bound,
334-
dialect: computed,
335335
isDisabled: computed,
336336
value: computed,
337337
readonly: computed,
338+
state: observable.ref,
339+
model: observable.ref,
340+
dialect: observable.ref,
338341
hintsLimitIsMet: observable.ref,
339342
readonlyState: observable,
340343
executingScript: observable,
341344
sqlEditorSettingsService: observable.ref,
342345
},
343346
{
344347
state,
348+
model,
349+
dialect: connectionDialectLoader.tryGetData,
345350
connectionExecutionContextService,
346351
sqlQueryService,
347352
sqlDialectInfoService,
@@ -354,27 +359,14 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData {
354359
},
355360
);
356361

357-
const key = getComputed(() => {
358-
const executionContext = data.model.dataSource?.executionContext;
359-
if (executionContext) {
360-
const context = data.connectionExecutionContextService.get(executionContext.id)?.context;
361-
if (context) {
362-
return createConnectionParam(context.projectId, context.connectionId);
363-
}
364-
}
365-
return null;
366-
});
367-
368362
useExecutor({
369-
executor: data.model.dataSource?.onDatabaseModelUpdate,
363+
executor: model.dataSource?.onDatabaseModelUpdate,
370364
handlers: [
371365
function updateDatabaseModels() {
372366
data.loadDatabaseDataModels();
373367
},
374368
],
375369
});
376370

377-
useResource(useSqlEditor, ConnectionDialectResource, key);
378-
379371
return data;
380372
}

webapp/packages/plugin-sql-generator/src/SqlGenerators/GeneratedSqlDialog.tsx

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Licensed under the Apache License, Version 2.0.
66
* you may not use this file except in compliance with the License.
77
*/
8-
import { computed, observable } from 'mobx';
8+
import { observable } from 'mobx';
99
import { observer } from 'mobx-react-lite';
1010
import { useEffect } from 'react';
1111

@@ -21,15 +21,15 @@ import {
2121
useClipboard,
2222
useErrorDetails,
2323
useObservableRef,
24+
useResource,
2425
useS,
2526
useTranslate,
2627
} from '@cloudbeaver/core-blocks';
27-
import { ConnectionInfoResource, createConnectionParam } from '@cloudbeaver/core-connections';
28+
import { ConnectionDialectResource, ConnectionInfoResource, createConnectionParam } from '@cloudbeaver/core-connections';
2829
import { useService } from '@cloudbeaver/core-di';
2930
import type { DialogComponentProps } from '@cloudbeaver/core-dialogs';
30-
import { GQLErrorCatcher, type SqlDialectInfo } from '@cloudbeaver/core-sdk';
31+
import { GQLErrorCatcher } from '@cloudbeaver/core-sdk';
3132
import { useCodemirrorExtensions } from '@cloudbeaver/plugin-codemirror6';
32-
import { SqlDialectInfoService } from '@cloudbeaver/plugin-sql-editor';
3333
import { SQLCodeEditorLoader, useSqlDialectExtension } from '@cloudbeaver/plugin-sql-editor-new';
3434

3535
import style from './GeneratedSqlDialog.module.css';
@@ -45,7 +45,6 @@ export const GeneratedSqlDialog = observer<DialogComponentProps<Payload>>(functi
4545
const copy = useClipboard();
4646
const styles = useS(style);
4747

48-
const sqlDialectInfoService = useService(SqlDialectInfoService);
4948
const sqlGeneratorsResource = useService(SqlGeneratorsResource);
5049
const connectionInfoResource = useService(ConnectionInfoResource);
5150
const connection = connectionInfoResource.getConnectionForNode(payload.pathId);
@@ -55,13 +54,6 @@ export const GeneratedSqlDialog = observer<DialogComponentProps<Payload>>(functi
5554
query: '',
5655
loading: true,
5756
error: new GQLErrorCatcher(),
58-
get dialect(): SqlDialectInfo | undefined {
59-
if (!this.connection?.connected) {
60-
return;
61-
}
62-
63-
return this.sqlDialectInfoService.getDialectInfo(createConnectionParam(this.connection));
64-
},
6557
async load() {
6658
this.error.clear();
6759

@@ -78,12 +70,12 @@ export const GeneratedSqlDialog = observer<DialogComponentProps<Payload>>(functi
7870
query: observable.ref,
7971
loading: observable.ref,
8072
connection: observable.ref,
81-
dialect: computed,
8273
},
83-
{ connection, sqlDialectInfoService },
74+
{ connection },
8475
);
76+
const connectionDialectResource = useResource(GeneratedSqlDialog, ConnectionDialectResource, connection ? createConnectionParam(connection) : null);
77+
const sqlDialect = useSqlDialectExtension(connectionDialectResource.data);
8578

86-
const sqlDialect = useSqlDialectExtension(state.dialect);
8779
const extensions = useCodemirrorExtensions();
8880
extensions.set(...sqlDialect);
8981
const error = useErrorDetails(state.error.exception);
@@ -92,17 +84,6 @@ export const GeneratedSqlDialog = observer<DialogComponentProps<Payload>>(functi
9284
state.load();
9385
}, []);
9486

95-
useEffect(() => {
96-
if (!connection) {
97-
return;
98-
}
99-
100-
sqlDialectInfoService.loadSqlDialectInfo(createConnectionParam(connection)).catch(exception => {
101-
console.error(exception);
102-
console.warn(`Can't get dialect for connection: '${connection.id}'. Default dialect will be used`);
103-
});
104-
});
105-
10687
return (
10788
<CommonDialogWrapper size="large">
10889
<CommonDialogHeader title="app_shared_sql_generators_dialog_title" icon="sql-script" onReject={rejectDialog} />

0 commit comments

Comments
 (0)