diff --git a/webapp/packages/plugin-connections/src/ConnectionForm/SSL/ConnectionFormSSLPart.ts b/webapp/packages/plugin-connections/src/ConnectionForm/SSL/ConnectionFormSSLPart.ts index b7dface7b5..586e080c8d 100644 --- a/webapp/packages/plugin-connections/src/ConnectionForm/SSL/ConnectionFormSSLPart.ts +++ b/webapp/packages/plugin-connections/src/ConnectionForm/SSL/ConnectionFormSSLPart.ts @@ -9,8 +9,8 @@ import { FormPart, type IFormState } from '@cloudbeaver/core-ui'; import type { IExecutionContextProvider } from '@cloudbeaver/core-executor'; import type { IConnectionFormStateRefactored } from '../IConnectionFormStateRefactored.js'; -import type { NetworkHandlerConfigInput } from '@cloudbeaver/core-sdk'; -import { isNotNullDefined, isObjectsEqual } from '@cloudbeaver/core-utils'; +import { type NetworkHandlerConfigInput } from '@cloudbeaver/core-sdk'; +import { isNotNullDefined } from '@cloudbeaver/core-utils'; import { getSSLDriverHandler } from './getSSLDriverHandler.js'; import { createConnectionParam, @@ -19,21 +19,28 @@ import { type NetworkHandlerResource, } from '@cloudbeaver/core-connections'; import { CachedMapAllKey } from '@cloudbeaver/core-resource'; -import { getSSLDefaultConfig } from './getSSLDefaultConfig.js'; import { toJS } from 'mobx'; import { connectionCredentialsStateContext } from '../Contexts/connectionCredentialsStateContext.js'; import { PROPERTY_FEATURE_SECURED } from './PROPERTY_FEATURE_SECURED.js'; import { SSL_CODE_NAME } from './SSL_CODE_NAME.js'; +import type { INetworkHandlerConfig } from '../Options/IConnectionNetworkHanler.js'; +import { getSSLDefaultConfig } from './getSSLDefaultConfig.js'; -// TODO should I have networkHandler state here? -export class ConnectionFormSSLPart extends FormPart { +const DEFAULT_SSL_NETWORK_HANDLER: INetworkHandlerConfig = { + id: SSL_CODE_NAME, + enabled: false, + properties: {}, + secureProperties: {}, +}; + +export class ConnectionFormSSLPart extends FormPart { constructor( formState: IFormState, private readonly dbDriverResource: DBDriverResource, private readonly networkHandlerResource: NetworkHandlerResource, private readonly connectionInfoResource: ConnectionInfoResource, ) { - super(formState); + super(formState, DEFAULT_SSL_NETWORK_HANDLER); } protected override async loader(): Promise { @@ -43,70 +50,54 @@ export class ConnectionFormSSLPart extends FormPart h.id === handler.id); - if (!this.formState.state.config.networkHandlersConfig) { - this.formState.state.config.networkHandlersConfig = []; - } - - if (!this.formState.state.config.networkHandlersConfig.some(state => state.id === handler.id)) { + if (!this.formState.state.config.networkHandlersConfig?.some(state => state.id === handler.id)) { const config: NetworkHandlerConfigInput = initialConfig ? toJS(initialConfig) : getSSLDefaultConfig(handler.id); if (config.secureProperties) { config.properties = { ...config.properties, ...config.secureProperties }; } - this.formState.state.config.networkHandlersConfig.push(config); + this.setInitialState(config); + return; } + + this.setInitialState(initialConfig ?? DEFAULT_SSL_NETWORK_HANDLER); } protected override async format( data: IFormState, contexts: IExecutionContextProvider>, ): Promise { - const config = this.formState.state.config; const credentialsState = contexts.getContext(connectionCredentialsStateContext); - const info = this.connectionInfoResource.get( - createConnectionParam({ - id: data.state.config.connectionId!, - projectId: data.state.projectId, - }), - ); - if (!config.networkHandlersConfig || config.networkHandlersConfig.length === 0 || !config.driverId) { + if (!this.isChanged || !this.formState.state.config.driverId) { return; } - const driver = await this.dbDriverResource.load(config.driverId); + const driver = await this.dbDriverResource.load(this.formState.state.config.driverId); const handlers = await this.networkHandlerResource.load(CachedMapAllKey); - const handler = config.networkHandlersConfig.find( - handler => driver?.applicableNetworkHandlers.includes(handler.id) && handlers.some(h => h.id === handler.id && h.codeName === SSL_CODE_NAME), - ); + const handler = driver?.applicableNetworkHandlers.includes(this.state.id) ? this.state : undefined; const descriptor = handlers.find(h => h.id === handler?.id); if (!handler) { return; } - const initial = info?.networkHandlersConfig?.find(h => h.id === handler.id); const handlerConfig: NetworkHandlerConfigInput = toJS(handler); - handlerConfig.savePassword = handler.savePassword || config.sharedCredentials; + // TODO should I have formstate.config here? + handlerConfig.savePassword = handler.savePassword || this.formState.state.config.sharedCredentials; - const changed = isChanged(handlerConfig, initial); - - if (changed && descriptor) { + if (this.isChanged && descriptor) { for (const descriptorProperty of descriptor.properties) { if (!descriptorProperty.id) { continue; @@ -123,7 +114,7 @@ export class ConnectionFormSSLPart extends FormPart, contexts: IExecutionContextProvider>, ): Promise { - return Promise.resolve(); + const info = this.connectionInfoResource.get(createConnectionParam(this.formState.state.projectId, this.formState.state.config.connectionId!)); + + await this.connectionInfoResource.update(createConnectionParam(this.formState.state.projectId, this.formState.state.config.connectionId!), { + connectionId: this.formState.state.config.connectionId, + networkHandlersConfig: [ + ...(info?.networkHandlersConfig ?? []), + { + ...this.state, + id: this.state.id, + }, + ], + }); } } -function trimSSLConfig(input: NetworkHandlerConfigInput) { +function trimSSLConfig(input: INetworkHandlerConfig): INetworkHandlerConfig { const { secureProperties } = input; if (!secureProperties) { - return; + return input; } if (!Object.keys(secureProperties).length) { - return; + return input; } for (const key in secureProperties) { @@ -184,22 +181,6 @@ function trimSSLConfig(input: NetworkHandlerConfigInput) { secureProperties[key] = secureProperties[key]?.trim(); } } -} -function isChanged(handler: NetworkHandlerConfigInput, initial?: NetworkHandlerConfigInput) { - if (!initial && !handler.enabled) { - return false; - } - - const initialProperties = { ...(initial?.properties ?? {}), ...(initial?.secureProperties ?? {}) }; - - if ( - handler.enabled !== initial?.enabled || - handler.savePassword !== initial?.savePassword || - !isObjectsEqual(handler.properties, initialProperties) - ) { - return true; - } - - return false; + return input; } diff --git a/webapp/packages/plugin-connections/src/ConnectionForm/SSL/SSL.tsx b/webapp/packages/plugin-connections/src/ConnectionForm/SSL/SSL.tsx index 236409b75c..8535ae4341 100644 --- a/webapp/packages/plugin-connections/src/ConnectionForm/SSL/SSL.tsx +++ b/webapp/packages/plugin-connections/src/ConnectionForm/SSL/SSL.tsx @@ -27,23 +27,20 @@ import { useService } from '@cloudbeaver/core-di'; import { ProjectInfoResource } from '@cloudbeaver/core-projects'; import { ServerConfigResource } from '@cloudbeaver/core-root'; import type { NetworkHandlerConfigInput, NetworkHandlerDescriptor } from '@cloudbeaver/core-sdk'; -import type { IFormState, TabContainerPanelComponent } from '@cloudbeaver/core-ui'; +import type { TabContainerPanelComponent } from '@cloudbeaver/core-ui'; import { isSafari } from '@cloudbeaver/core-utils'; import { SAVED_VALUE_INDICATOR } from './SAVED_VALUE_INDICATOR.js'; import styles from './SSL.module.css'; -import type { IConnectionFormStateRefactored } from '../IConnectionFormStateRefactored.js'; +import type { IConnectionFormPropsRefactored } from '../IConnectionFormStateRefactored.js'; import { ConnectionInfoResource, createConnectionParam } from '@cloudbeaver/core-connections'; -interface Props { +interface Props extends IConnectionFormPropsRefactored { handler: NetworkHandlerDescriptor; handlerState: NetworkHandlerConfigInput; - formState: IFormState; - sharedCredentials: boolean; - template: boolean; } -export const SSL: TabContainerPanelComponent = observer(function SSL({ formState, sharedCredentials, template, handler, handlerState }) { +export const SSL: TabContainerPanelComponent = observer(function SSL({ formState, handler, handlerState }) { const translate = useTranslate(); const style = useS(styles); @@ -73,7 +70,7 @@ export const SSL: TabContainerPanelComponent = observer(function SSL({ fo properties={handler.properties} category={null} disabled={disabled || !enabled} - isSaved={p => !!p.id && initialHandler?.secureProperties?.[p.id] === SAVED_VALUE_INDICATOR} + isSaved={p => !!p.id && initialHandler?.secureProperties[p.id] === SAVED_VALUE_INDICATOR} autofillToken={autofillToken} hideEmptyPlaceholder showRememberTip @@ -89,7 +86,7 @@ export const SSL: TabContainerPanelComponent = observer(function SSL({ fo properties={handler.properties} category={category} disabled={disabled || !enabled} - isSaved={p => !!p.id && initialHandler?.secureProperties?.[p.id] === SAVED_VALUE_INDICATOR} + isSaved={p => !!p.id && initialHandler?.secureProperties[p.id] === SAVED_VALUE_INDICATOR} autofillToken={autofillToken} hideEmptyPlaceholder showRememberTip @@ -98,12 +95,14 @@ export const SSL: TabContainerPanelComponent = observer(function SSL({ fo ))} - {credentialsSavingEnabled && !template && !sharedCredentials && ( + {/* TODO use formState.state.config??? */} + {credentialsSavingEnabled && !formState.state.config.template && !formState.state.config.sharedCredentials && ( = observer(function SSLPanel(props) { +export const SSLPanel: TabContainerTabComponent = observer(function SSLPanel(props) { const networkHandlerResource = useResource(SSLPanel, NetworkHandlerResource, CachedMapAllKey); const dbDriverResource = useResource(SSLPanel, DBDriverResource, props.formState.state.config.driverId ?? null); - const SSLPart = getConnectionFormSSLPart(props.formState); const handler = getSSLDriverHandler(networkHandlerResource.resource.values, dbDriverResource.data?.applicableNetworkHandlers ?? []); + const SSLPart = getConnectionFormSSLPart(props.formState); - if (SSLPart.isLoaded() && handler && !props.formState.state.config.networkHandlersConfig?.some(state => state.id === handler?.id)) { - props.formState.state.config.networkHandlersConfig?.push(getSSLDefaultConfig(handler.id)); - } + const handlerState = SSLPart.state; - const handlerState = props.formState.state.config.networkHandlersConfig?.find(h => h.id === handler?.id); - - useAutoLoad(SSLPanel, [SSLPart]); + useAutoLoad(SSLPanel, SSLPart); if (!handler || !handlerState) { return null; } - return ( - - ); + return ; });