Skip to content

Commit

Permalink
fix(1094): Keep current visibility when adding a new entity to the ca…
Browse files Browse the repository at this point in the history
…nvas
  • Loading branch information
tplevko committed Nov 11, 2024
1 parent 39c0ffd commit aded76a
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { BaseVisualCamelEntityDefinition } from '../../../../models/camel/camel-
import { EntityType } from '../../../../models/camel/entities';
import { EntitiesContext } from '../../../../providers/entities.provider';
import { VisibleFlowsContext } from '../../../../providers/visible-flows.provider';
import { SELECTION_EVENT, useVisualizationController } from '@patternfly/react-topology';
import { IVisualizationNode } from '../../../../models';

export const NewEntity: FunctionComponent = () => {
const { camelResource, updateEntitiesFromCamelResource } = useContext(EntitiesContext)!;
Expand All @@ -13,6 +15,28 @@ export const NewEntity: FunctionComponent = () => {
const menuRef = useRef<HTMLDivElement>(null);
const toggleRef = useRef<HTMLButtonElement>(null);
const groupedEntities = useRef<BaseVisualCamelEntityDefinition>(camelResource.getCanvasEntityList());
const controller = useVisualizationController();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function findRootVisualizationNode(node: any, newId?: string): IVisualizationNode | null {
if (node?.data?.vizNode?.data.entity.id === newId) {
if (node?.data?.vizNode?.data?.isGroup === true) {
return node.data.vizNode;
}
}
return null;
}

function findRootVisualization(node: any, newId?: string): IVisualizationNode | null {
if (node?.children) {
for (const child of node.children) {
const result = findRootVisualizationNode(child, newId);
if (result) return result;
}
}
return null;
}


const onSelect = useCallback(
(_event: unknown, entityType: string | number | undefined) => {
Expand All @@ -26,7 +50,11 @@ export const NewEntity: FunctionComponent = () => {
* supported
*/
const newId = camelResource.addNewEntity(entityType as EntityType);
visibleFlowsContext.visualFlowsApi.hideAllFlows();
setTimeout(() => {
const result = findRootVisualization(controller.getGraph(), newId)
controller.fireEvent(SELECTION_EVENT, [result?.id]);
}, 300);

visibleFlowsContext.visualFlowsApi.toggleFlowVisible(newId);
updateEntitiesFromCamelResource();
setIsOpen(false);
Expand Down

0 comments on commit aded76a

Please sign in to comment.