diff --git a/packages/ui/src/components/Visualization/ContextToolbar/NewEntity/NewEntity.tsx b/packages/ui/src/components/Visualization/ContextToolbar/NewEntity/NewEntity.tsx index 850a326e1..7eee68299 100644 --- a/packages/ui/src/components/Visualization/ContextToolbar/NewEntity/NewEntity.tsx +++ b/packages/ui/src/components/Visualization/ContextToolbar/NewEntity/NewEntity.tsx @@ -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)!; @@ -13,6 +15,28 @@ export const NewEntity: FunctionComponent = () => { const menuRef = useRef(null); const toggleRef = useRef(null); const groupedEntities = useRef(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) => { @@ -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);