Skip to content

Commit 2f0b6c9

Browse files
lordripchristophd
authored andcommitted
chore: cleanup branch incorporating Sonar findings
1 parent 76b3085 commit 2f0b6c9

5 files changed

Lines changed: 44 additions & 40 deletions

File tree

packages/ui/src/components/Visualization/Canvas/Form/fields/EndpointField/EndpointField.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,15 @@ export const EndpointField: FunctionComponent<FieldProps> = ({ propName, require
6666
}, []);
6767

6868
const handleCreate = useCallback(
69-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
70-
(type: string, model: any) => {
69+
(type: string, model: Record<string, unknown>) => {
7170
if (!type || model === undefined || typeof model !== 'object' || model?.name === undefined) {
7271
return;
7372
}
7473

75-
endpointsHandler.addNewEndpoint(type, model as unknown as Record<string, unknown>);
74+
endpointsHandler.addNewEndpoint(type, model);
7675

7776
setIsOpen(false);
78-
onChange(model.name);
77+
onChange(model.name as string);
7978
setLastUpdated(Date.now());
8079
},
8180
[endpointsHandler, onChange],

packages/ui/src/components/Visualization/Canvas/Form/fields/EndpointField/EndpointModal.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ describe('EndpointModal', () => {
3232
}
3333
}
3434

35+
const sortedEndpoints = [...endpoints];
36+
sortedEndpoints.sort((a, b) => a.name?.localeCompare(b.name));
37+
3538
endpointsSchema = {
36-
oneOf: endpoints.sort((a, b) => a.name?.localeCompare(b.name)),
39+
oneOf: sortedEndpoints,
3740
};
3841
});
3942

packages/ui/src/components/Visualization/Canvas/Form/fields/EndpointField/EndpointModal.tsx

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isDefined } from '@kaoto/forms';
22
import { CanvasFormTabsContext, CanvasFormTabsContextResult, ModelContextProvider, SchemaProvider } from '@kaoto/forms';
33
import { Button, Modal, ModalBody, ModalFooter, ModalHeader, ModalVariant } from '@patternfly/react-core';
4-
import { FunctionComponent, useCallback, useContext, useEffect, useMemo, useState } from 'react';
4+
import { FunctionComponent, useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react';
55

66
import { CatalogModalContext } from '../../../../../../dynamic-catalog/catalog-modal.provider';
77
import { CatalogKind, KaotoSchemaDefinition } from '../../../../../../models';
@@ -14,7 +14,7 @@ export type EndpointModalProps = {
1414
endpoint?: unknown;
1515
type?: string;
1616
endpointsSchema?: KaotoSchemaDefinition['schema'];
17-
onConfirm: (type: string, model: unknown) => void;
17+
onConfirm: (type: string, model: Record<string, unknown>) => void;
1818
onCancel: () => void;
1919
};
2020

@@ -33,6 +33,7 @@ export const EndpointModal: FunctionComponent<EndpointModalProps> = ({
3333
const [endpointModel = endpoint, setEndpointModel] = useState<unknown>();
3434
const [endpointType, setEndpointType] = useState<string | undefined>(type);
3535
const catalogModalContext = useContext(CatalogModalContext);
36+
const hasSelectedEndpoint = useRef(false);
3637

3738
const selectEndpoint = useCallback(async () => {
3839
/** Open Catalog modal, filtering the compatible nodes */
@@ -44,7 +45,7 @@ export const EndpointModal: FunctionComponent<EndpointModalProps> = ({
4445
}, [catalogModalContext]);
4546

4647
const onEndpointModelChange = (_propName: string, model: unknown) => {
47-
const jsonRecord = model as Record<string, unknown>;
48+
const jsonRecord = { ...(model as Record<string, unknown>) };
4849
// sanitize generated endpoint model
4950
for (const key in jsonRecord) {
5051
if (typeof jsonRecord[key] === 'object') {
@@ -55,30 +56,28 @@ export const EndpointModal: FunctionComponent<EndpointModalProps> = ({
5556
setEndpointModel(jsonRecord);
5657
};
5758

58-
const handleConfirm = useCallback(async () => {
59+
const handleConfirm = useCallback(() => {
5960
if (!endpointType || endpointModel === undefined || typeof endpointModel !== 'object') {
6061
return;
6162
}
6263

63-
onConfirm(endpointType, endpointModel);
64+
onConfirm(endpointType, endpointModel as Record<string, unknown>);
6465
}, [endpointType, endpointModel, onConfirm]);
6566

6667
useEffect(() => {
67-
if (isDefined(endpointModel) || !isDefined(endpointsSchema)) {
68-
return;
68+
if (!isDefined(endpointModel) && !hasSelectedEndpoint.current) {
69+
hasSelectedEndpoint.current = true;
70+
selectEndpoint()
71+
.then((selectedType) => {
72+
if (selectedType) {
73+
setEndpointType(selectedType);
74+
setEndpointModel({});
75+
}
76+
})
77+
.catch(() => {
78+
/* catalog modal was dismissed */
79+
});
6980
}
70-
71-
let isActive = true;
72-
void selectEndpoint().then((selectedType) => {
73-
if (isActive && selectedType) {
74-
setEndpointType(selectedType);
75-
setEndpointModel({});
76-
}
77-
});
78-
79-
return () => {
80-
isActive = false;
81-
};
8281
}, [endpointModel, endpointsSchema, selectEndpoint]);
8382

8483
return (

packages/ui/src/components/Visualization/Canvas/Form/fields/EndpointField/EndpointsField.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ export const EndpointsField: FunctionComponent<FieldProps> = ({ propName, requir
2626
const confirmModalContext = useContext(ActionConfirmationModalContext);
2727

2828
const handleCreateOrEdit = useCallback(
29-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30-
async (type: string, model: any) => {
29+
async (type: string, model: Record<string, unknown>) => {
3130
if (!type || model === undefined || typeof model !== 'object' || model?.name === undefined) {
3231
return;
3332
}
3433

3534
if (operation === 'Create') {
36-
endpointsHandler.addNewEndpoint(type, model as unknown as Record<string, unknown>);
35+
endpointsHandler.addNewEndpoint(type, model);
3736
setItems(endpointsHandler.getDefinedEndpointsNameAndType(value));
3837
} else if (operation === 'Update') {
3938
const prevName = editModel?.name as string;
@@ -47,7 +46,7 @@ export const EndpointsField: FunctionComponent<FieldProps> = ({ propName, requir
4746
if (!modalAnswer || modalAnswer === ACTION_ID_CANCEL) return;
4847
}
4948

50-
endpointsHandler.updateEndpoint(type, model as unknown as Record<string, unknown>, prevName);
49+
endpointsHandler.updateEndpoint(type, model, prevName);
5150
if (prevName !== model.name) {
5251
setItems(endpointsHandler.getDefinedEndpointsNameAndType(value));
5352
}
@@ -85,9 +84,10 @@ export const EndpointsField: FunctionComponent<FieldProps> = ({ propName, requir
8584

8685
if (!modalAnswer || modalAnswer === ACTION_ID_CANCEL) return;
8786

88-
value.splice(index, 1);
89-
setItems(endpointsHandler.getDefinedEndpointsNameAndType(value));
90-
onChange(value);
87+
const updated = [...value];
88+
updated.splice(index, 1);
89+
setItems(endpointsHandler.getDefinedEndpointsNameAndType(updated));
90+
onChange(updated);
9191
},
9292
[endpointsHandler, onChange, confirmModalContext, value],
9393
);

packages/ui/src/models/visualization/metadata/citrus/endpoints-entity-handler.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { KaotoSchemaDefinition } from '../../../kaoto-schema';
88
import { CamelCatalogService } from '../../flows/camel-catalog.service';
99

1010
export class EndpointsEntityHandler {
11-
constructor(private testResource?: CitrusTestResource | undefined) {
11+
constructor(readonly testResource?: CitrusTestResource | undefined) {
1212
if (!this.testResource) return;
1313
}
1414

@@ -25,22 +25,24 @@ export class EndpointsEntityHandler {
2525
}
2626
}
2727

28+
const sortedEndpoints = [...endpoints];
29+
sortedEndpoints.sort((a, b) => a.name?.localeCompare(b.name));
30+
2831
return {
29-
oneOf: endpoints.sort((a, b) => a.name?.localeCompare(b.name)),
32+
oneOf: sortedEndpoints,
3033
};
3134
}
3235

3336
getTestEntity(): Test | undefined {
3437
return this.testResource?.toJSON() as Test | undefined;
3538
}
3639

37-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
38-
getDefinedEndpointsNameAndType(endpoints: Record<string, any>[]): { name: string; type: string }[] {
40+
getDefinedEndpointsNameAndType(endpoints: Record<string, unknown>[]): { name: string; type: string }[] {
3941
const definedEndpoints: { name: string; type: string }[] = [];
4042

4143
for (const endpoint of endpoints) {
4244
if (endpoint.type !== undefined) {
43-
definedEndpoints.push({ name: endpoint.name ?? ('' as string), type: endpoint.type ?? ('unknown' as string) });
45+
definedEndpoints.push({ name: (endpoint.name as string) ?? '', type: (endpoint.type as string) ?? 'unknown' });
4446
} else {
4547
const type = this.getEndpointType(endpoint);
4648
if (type) {
@@ -66,7 +68,7 @@ export class EndpointsEntityHandler {
6668
);
6769

6870
for (const action of testEntity.actions ?? []) {
69-
if (action.createEndpoint != undefined) {
71+
if (action.createEndpoint !== undefined) {
7072
const jsonRecord = action.createEndpoint as Record<string, unknown>;
7173
const type = this.getEndpointType(jsonRecord);
7274
if (type) {
@@ -133,13 +135,14 @@ export class EndpointsEntityHandler {
133135
const endpointName = prevName || model['name'];
134136
if (endpointName) {
135137
const index = test.endpoints.findIndex((endpoint) => {
136-
const type = this.getEndpointType(endpoint);
137-
if (type) {
138-
const name = this.findEndpointName(type, endpoint);
138+
const foundType = this.getEndpointType(endpoint);
139+
if (foundType) {
140+
const name = this.findEndpointName(foundType, endpoint);
139141
if (name && name === endpointName) {
140142
return true;
141143
}
142144
}
145+
return false;
143146
});
144147

145148
if (index > -1) {

0 commit comments

Comments
 (0)