From 371de492fac567fd4f2232956230b631949d628a Mon Sep 17 00:00:00 2001 From: tplevko Date: Mon, 11 Nov 2024 15:32:22 +0100 Subject: [PATCH] fix(1094): Keep current visibility when adding a new entity to the canvas --- .../multiflow/multiFlowDesigner.cy.ts | 6 ++--- .../cypress/support/next-commands/default.ts | 1 - .../ContextToolbar/NewEntity/NewEntity.tsx | 27 ++++++++++++++++++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/ui-tests/cypress/e2e/designer/multiflow/multiFlowDesigner.cy.ts b/packages/ui-tests/cypress/e2e/designer/multiflow/multiFlowDesigner.cy.ts index a4ab36036..91c65f643 100644 --- a/packages/ui-tests/cypress/e2e/designer/multiflow/multiFlowDesigner.cy.ts +++ b/packages/ui-tests/cypress/e2e/designer/multiflow/multiFlowDesigner.cy.ts @@ -17,15 +17,13 @@ describe('Test for Multi route actions from the canvas', () => { cy.addNewRoute(); cy.addNewRoute(); - cy.get('[data-testid="flows-list-route-count"]').should('have.text', '1/3'); + cy.get('[data-testid="flows-list-route-count"]').should('have.text', '3/3'); cy.toggleRouteVisibility(0); cy.toggleRouteVisibility(1); - cy.get('[data-testid="flows-list-route-count"]').should('have.text', '3/3'); + cy.get('[data-testid="flows-list-route-count"]').should('have.text', '1/3'); - cy.toggleRouteVisibility(0); - cy.toggleRouteVisibility(1); cy.toggleRouteVisibility(2); cy.get('[data-testid="flows-list-route-count"]').should('have.text', '0/3'); diff --git a/packages/ui-tests/cypress/support/next-commands/default.ts b/packages/ui-tests/cypress/support/next-commands/default.ts index 30cd1c8e1..e7e5af242 100644 --- a/packages/ui-tests/cypress/support/next-commands/default.ts +++ b/packages/ui-tests/cypress/support/next-commands/default.ts @@ -153,5 +153,4 @@ Cypress.Commands.add('deleteRoute', (index: number) => { cy.get('.pf-m-danger').click(); } }); - cy.closeFlowsListIfVisible(); }); diff --git a/packages/ui/src/components/Visualization/ContextToolbar/NewEntity/NewEntity.tsx b/packages/ui/src/components/Visualization/ContextToolbar/NewEntity/NewEntity.tsx index 850a326e1..a74e673aa 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,25 @@ 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 findRootVisualization(node: any, newId?: string): IVisualizationNode | null { + if (node?.children) { + for (const child of node.children) { + const result = (() => { + if (child?.data?.vizNode?.data.entity.id === newId) { + if (child?.data?.vizNode?.data?.isGroup === true) { + return child.data.vizNode; + } + } + return null; + })(); + if (result) return result; + } + } + return null; + } const onSelect = useCallback( (_event: unknown, entityType: string | number | undefined) => { @@ -26,7 +47,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);