Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
const MAIN_PROPERTY_PORT_KEY = 'port';
const MAIN_PROPERTY_SERVER_KEY = 'server';

const defaultStateGetter = (connectionId?: string, credentials?: Record<string, any>) =>
const defaultStateGetter = (connectionId?: string) =>
({
connectionId,
configurationType: DriverConfigurationType.Manual,
credentials: credentials ?? {},
credentials: {},
mainPropertyValues: {},
expertSettingsValues: {},
networkHandlersConfig: [],
Expand Down Expand Up @@ -151,7 +151,7 @@
return state;
}

get connectionKey() {

Check warning on line 154 in webapp/packages/plugin-connections/src/ConnectionForm/Options/ConnectionFormOptionsPart.ts

View workflow job for this annotation

GitHub Actions / Frontend / Lint

Missing return type on function
if (!this.initialState.connectionId || !this.formState.state.projectId) {
return null;
}
Expand Down Expand Up @@ -185,14 +185,12 @@

protected override async loader(): Promise<void> {
if (this.formState.mode === 'create') {
const credentials = this.state.authModelId
? getObjectPropertyDefaults(await this.getConnectionAuthModelProperties(this.state.authModelId))
: undefined;

this.setInitialState(defaultStateGetter(this.initialState.connectionId ?? this.formState.state.connectionId, credentials));
const defaults = await this.getDefaults();

await this.setDriverId(this.state.driverId);

this.setInitialState(defaults);

return;
}

Expand Down Expand Up @@ -340,9 +338,39 @@
this.state.authModelId = modelId;
}

private async getDefaults() {
const config = defaultStateGetter(this.initialState.connectionId ?? this.formState.state.connectionId);

if (this.state.driverId) {
config.driverId = this.state.driverId;
}

if (this.state.authModelId) {
const authProperties = await this.getConnectionAuthModelProperties(this.state.authModelId);
config.credentials = getObjectPropertyDefaults(authProperties);
}

if (config.driverId) {
const driver = await this.dbDriverResource.load(config.driverId, ['includeMainProperties']);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

includes is pattern we refused to support a while ago. it would be nice to create new resource DBDriverMainPropertiesResource and reuse it here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not scope of the ticket, code is already exist in this part


if (config.mainPropertyValues) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we get config defaultStateGetter
in this helper mainPropertyValues is always an object

so this check is always true
probably there is a miss-match with the zod type here:

mainPropertyValues: schema.record(schema.string(), schema.any()).optional(),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, types are from zod schema, there mainPropertyValues is optional

for (const property of driver.mainProperties) {
// We don't use getObjectPropertyDefaults because, in this case, the backend returns default values in the value field.
const value = getObjectPropertyDefaultValue(property) || getObjectPropertyValue(property);

if (property.id) {
config.mainPropertyValues[property.id] = value;
}
}
}
}

return config;
}

protected override async format(
data: IFormState<IConnectionFormState>,

Check warning on line 372 in webapp/packages/plugin-connections/src/ConnectionForm/Options/ConnectionFormOptionsPart.ts

View workflow job for this annotation

GitHub Actions / Frontend / Lint

'data' is defined but never used
contexts: IExecutionContextProvider<IFormState<IConnectionFormState>>,

Check warning on line 373 in webapp/packages/plugin-connections/src/ConnectionForm/Options/ConnectionFormOptionsPart.ts

View workflow job for this annotation

GitHub Actions / Frontend / Lint

'contexts' is defined but never used
): Promise<void> {
if (!this.state.driverId || !this.formState.state.projectId) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
},
];

export const Options: TabContainerPanelComponent<IConnectionFormProps> = observer(function Options({ formState, tabId }) {

Check warning on line 92 in webapp/packages/plugin-connections/src/ConnectionForm/Options/Options.tsx

View workflow job for this annotation

GitHub Actions / Frontend / Lint

'tabId' is defined but never used
const isAdmin = usePermission(EAdminPermission.admin);
const serverConfigResource = useResource(Options, ServerConfigResource, undefined);
const projectInfoResource = useService(ProjectInfoResource);
Expand All @@ -113,7 +113,7 @@

const driverMap = useResource(Options, DBDriverResource, {
key: optionsPart.state.driverId || null,
includes: ['includeProviderProperties', 'includeMainProperties', 'includeDriverProperties'] as const,
includes: ['includeProviderProperties', 'includeMainProperties'] as const,
});

const driver = driverMap.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@
connectionId: config.connectionId,
}).setMode(config.connectionId ? FormMode.Edit : FormMode.Create);

await this.optionsPart?.load();
// Don't call load in create mode. There we rely on the defaults set here, and useAutoLoad will load the part on mount.
if (this.formState.mode === FormMode.Edit) {
await this.optionsPart?.load();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is off here

Previously we waited for part being loaded and set initialState. And only then we went to setting driverId and all attached config to it. And it was pretty straightforward and imperative for both form modes: create and edit

Right now it smells like race condition. What if in create mode we resolve setDriverId() before the optionsPart is actually loaded and set initialState?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the original issue here? Maybe we can resolve it differently?


if (config.driverId) {
await this.optionsPart?.setDriverId(config.driverId);
Expand Down Expand Up @@ -136,7 +139,7 @@
return this.formState ? getConnectionFormOptionsPart(this.formState) : null;
}

private readonly closeRemoved: IExecutorHandler<ResourceKey<IConnectionInfoParams>> = (data, contexts) => {

Check warning on line 142 in webapp/packages/plugin-connections/src/PublicConnectionForm/PublicConnectionFormService.ts

View workflow job for this annotation

GitHub Actions / Frontend / Lint

'contexts' is defined but never used

Check warning on line 142 in webapp/packages/plugin-connections/src/PublicConnectionForm/PublicConnectionFormService.ts

View workflow job for this annotation

GitHub Actions / Frontend / Lint

'data' is defined but never used
if (!this.formState || !this.optionsPart?.connectionKey) {
return;
}
Expand All @@ -146,7 +149,7 @@
}
};

private readonly closeDeleted: IExecutorHandler<ResourceKeySimple<IConnectionInfoParams>> = (data, contexts) => {

Check warning on line 152 in webapp/packages/plugin-connections/src/PublicConnectionForm/PublicConnectionFormService.ts

View workflow job for this annotation

GitHub Actions / Frontend / Lint

'contexts' is defined but never used
if (!this.formState || !this.optionsPart?.connectionKey) {
return;
}
Expand Down
Loading