From 8763835db21cbfeace322f96edcd4ce8a287c064 Mon Sep 17 00:00:00 2001 From: bartoval Date: Fri, 14 Jun 2024 20:34:42 +0200 Subject: [PATCH] fix(Topology): :lady_beetle: Wrong group selection with show only neighbours --- mocks/data/PROCESSES.json | 2 +- .../__tests__/TopologyComponents.spec.tsx | 6 +-- .../__tests__/TopologyProcesses.spec.tsx | 4 +- .../Topology/__tests__/TopologySite.spec.tsx | 8 ++-- .../__tests__/useTopologyProcessData.spec.ts | 6 +-- .../__tests__/useTopologySiteData.spec.ts | 8 ++-- .../__tests__/useTopologySiteState.spec.tsx | 18 ++++---- .../Topology/components/DisplayResources.tsx | 41 +++++++++--------- .../Topology/components/TopologyComponent.tsx | 16 +++---- .../Topology/components/TopologyProcesses.tsx | 25 ++++++----- .../Topology/components/TopologySite.tsx | 16 +++---- .../components/useTopologyProcessData.ts | 43 ++++++++++++++----- .../components/useTopologySiteData.ts | 14 +++--- .../Topology/components/useTopologyState.ts | 10 ++--- .../services/topologyComponentController.ts | 6 +-- .../services/topologyProcessController.ts | 26 ++++++++--- .../services/topologySiteController.ts | 8 ++-- src/pages/Topology/views/Topology.tsx | 6 +-- 18 files changed, 152 insertions(+), 111 deletions(-) diff --git a/mocks/data/PROCESSES.json b/mocks/data/PROCESSES.json index 5ee39005..b1e9ae09 100644 --- a/mocks/data/PROCESSES.json +++ b/mocks/data/PROCESSES.json @@ -61,7 +61,7 @@ "parentName": "site 1", "imageName": "process op:v0.3.0", "groupName": "op", - "groupIdentity": "group-1", + "groupIdentity": "group-op", "hostName": "10.242.0.8", "sourceHost": "172.17.63.130", "processBinding": "bound", diff --git a/src/pages/Topology/__tests__/TopologyComponents.spec.tsx b/src/pages/Topology/__tests__/TopologyComponents.spec.tsx index 96ba6578..7ed2d13c 100644 --- a/src/pages/Topology/__tests__/TopologyComponents.spec.tsx +++ b/src/pages/Topology/__tests__/TopologyComponents.spec.tsx @@ -53,7 +53,7 @@ describe('Topology Components', () => { }> @@ -86,7 +86,7 @@ describe('Topology Components', () => { const handleSelected = jest.fn(); jest.spyOn(useTopologySiteState, 'default').mockImplementation(() => ({ - idSelected: TopologyController.transformStringIdsToIds(componentResult[2].identity), + idsSelected: TopologyController.transformStringIdsToIds(componentResult[2].identity), showOnlyNeighbours: false, moveToNodeSelected: false, displayOptionsSelected: [], @@ -121,7 +121,7 @@ describe('Topology Components', () => { const handleShowOnlyNeighbours = jest.fn(); jest.spyOn(useTopologySiteState, 'default').mockImplementation(() => ({ - idSelected: TopologyController.transformStringIdsToIds(componentResult[2].identity), + idsSelected: TopologyController.transformStringIdsToIds(componentResult[2].identity), showOnlyNeighbours: false, moveToNodeSelected: false, displayOptionsSelected: [], diff --git a/src/pages/Topology/__tests__/TopologyProcesses.spec.tsx b/src/pages/Topology/__tests__/TopologyProcesses.spec.tsx index cf39b1da..08ab4b84 100644 --- a/src/pages/Topology/__tests__/TopologyProcesses.spec.tsx +++ b/src/pages/Topology/__tests__/TopologyProcesses.spec.tsx @@ -37,7 +37,7 @@ const mockHandleMoveToNodeSelectedChecked = jest.fn(); const MockGetDisplaySelectedFromLocalStorage = jest.fn(); const mockUseTopologyStateResults = { - idSelected: TopologyController.transformStringIdsToIds(processesResults[0].identity), + idsSelected: TopologyController.transformStringIdsToIds(processesResults[0].identity), showOnlyNeighbours: false, moveToNodeSelected: false, displayOptionsSelected: [], @@ -111,7 +111,7 @@ describe('Topology Process', () => { }> identity)} diff --git a/src/pages/Topology/__tests__/TopologySite.spec.tsx b/src/pages/Topology/__tests__/TopologySite.spec.tsx index 65d1fc38..ad427962 100644 --- a/src/pages/Topology/__tests__/TopologySite.spec.tsx +++ b/src/pages/Topology/__tests__/TopologySite.spec.tsx @@ -52,7 +52,7 @@ describe('TopologySite', () => { }> @@ -103,7 +103,7 @@ describe('TopologySite', () => { const handleSelected = jest.fn(); jest.spyOn(useTopologySiteState, 'default').mockImplementation(() => ({ - idSelected: TopologyController.transformStringIdsToIds(sitesResults[2].identity), + idsSelected: TopologyController.transformStringIdsToIds(sitesResults[2].identity), showOnlyNeighbours: false, moveToNodeSelected: false, displayOptionsSelected: [], @@ -128,7 +128,7 @@ describe('TopologySite', () => { const handleShowOnlyNeighbours = jest.fn(); jest.spyOn(useTopologySiteState, 'default').mockImplementation(() => ({ - idSelected: TopologyController.transformStringIdsToIds(sitesResults[2].identity), + idsSelected: TopologyController.transformStringIdsToIds(sitesResults[2].identity), showOnlyNeighbours: false, moveToNodeSelected: false, displayOptionsSelected: [], @@ -155,7 +155,7 @@ describe('TopologySite', () => { const handleMoveToNodeSelectedChecked = jest.fn(); jest.spyOn(useTopologySiteState, 'default').mockImplementation(() => ({ - idSelected: TopologyController.transformStringIdsToIds(sitesResults[2].identity), + idsSelected: TopologyController.transformStringIdsToIds(sitesResults[2].identity), showOnlyNeighbours: false, moveToNodeSelected: false, displayOptionsSelected: [], diff --git a/src/pages/Topology/__tests__/useTopologyProcessData.spec.ts b/src/pages/Topology/__tests__/useTopologyProcessData.spec.ts index 86cd0627..39e2f11a 100644 --- a/src/pages/Topology/__tests__/useTopologyProcessData.spec.ts +++ b/src/pages/Topology/__tests__/useTopologyProcessData.spec.ts @@ -28,7 +28,7 @@ describe('useTopologySiteData', () => { const { result } = renderHook(() => useTopologyProcessData({ - idSelected: ['process1'], + idsSelected: ['process1'], showBytes: true, showByteRate: true, showLatency: true @@ -55,7 +55,7 @@ describe('useTopologySiteData', () => { const { result } = renderHook(() => useTopologyProcessData({ - idSelected: [], + idsSelected: [], showBytes: true, showByteRate: true, showLatency: true @@ -82,7 +82,7 @@ describe('useTopologySiteData', () => { const { result } = renderHook(() => useTopologyProcessData({ - idSelected: ['process3'], + idsSelected: ['process3'], showBytes: true, showByteRate: true, showLatency: true diff --git a/src/pages/Topology/__tests__/useTopologySiteData.spec.ts b/src/pages/Topology/__tests__/useTopologySiteData.spec.ts index b68cf07f..ee47a4c4 100644 --- a/src/pages/Topology/__tests__/useTopologySiteData.spec.ts +++ b/src/pages/Topology/__tests__/useTopologySiteData.spec.ts @@ -26,7 +26,7 @@ describe('useTopologySiteData', () => { const { result } = renderHook(() => useTopologySiteData({ - idSelected: ['site1'], + idsSelected: ['site1'], showDataLink: true, showBytes: true, showByteRate: true, @@ -59,7 +59,7 @@ describe('useTopologySiteData', () => { const { result } = renderHook(() => useTopologySiteData({ - idSelected: ['site1'], + idsSelected: ['site1'], showDataLink: true, showBytes: true, showByteRate: true, @@ -152,7 +152,7 @@ describe('useTopologySiteData', () => { const { result } = renderHook(() => useTopologySiteData({ - idSelected: [], + idsSelected: [], showDataLink: true, showBytes: true, showByteRate: true, @@ -182,7 +182,7 @@ describe('useTopologySiteData', () => { const { result } = renderHook(() => useTopologySiteData({ - idSelected: ['site3'], + idsSelected: ['site3'], showDataLink: true, showBytes: true, showByteRate: true, diff --git a/src/pages/Topology/__tests__/useTopologySiteState.spec.tsx b/src/pages/Topology/__tests__/useTopologySiteState.spec.tsx index ea130a14..e4130d4f 100644 --- a/src/pages/Topology/__tests__/useTopologySiteState.spec.tsx +++ b/src/pages/Topology/__tests__/useTopologySiteState.spec.tsx @@ -10,21 +10,21 @@ describe('useTopologySiteState', () => { }); it('should initialize with empty display options', () => { - const { result } = renderHook(() => useTopologyState({ id: undefined })); + const { result } = renderHook(() => useTopologyState({ ids: undefined })); expect(result.current.displayOptionsSelected).toEqual([]); }); it('should initialize with default display options', () => { const { result } = renderHook(() => - useTopologyState({ id: undefined, initDisplayOptionsEnabled: [SHOW_ROUTER_LINKS, SHOW_SITE_KEY] }) + useTopologyState({ ids: undefined, initDisplayOptionsEnabled: [SHOW_ROUTER_LINKS, SHOW_SITE_KEY] }) ); expect(result.current.displayOptionsSelected).toEqual([SHOW_ROUTER_LINKS, SHOW_SITE_KEY]); }); it('should handle display options selection', () => { - const { result } = renderHook(() => useTopologyState({ id: undefined, displayOptionsEnabledKey: 'test-key' })); + const { result } = renderHook(() => useTopologyState({ ids: undefined, displayOptionsEnabledKey: 'test-key' })); const newDisplayOptions = ['new_option']; act(() => { @@ -37,23 +37,23 @@ describe('useTopologySiteState', () => { it('sshould initialize the component with the correct selected id', () => { const { result } = renderHook(() => - useTopologyState({ id: TopologyController.transformStringIdsToIds('site_id') }) + useTopologyState({ ids: TopologyController.transformStringIdsToIds('site_id') }) ); - expect(result.current.idSelected).toEqual(['site_id']); + expect(result.current.idsSelected).toEqual(['site_id']); }); it('should update the selected ID when a site is selected', () => { - const { result } = renderHook(() => useTopologyState({ id: undefined })); + const { result } = renderHook(() => useTopologyState({ ids: undefined })); act(() => { result.current.handleSelected(TopologyController.transformStringIdsToIds('site_id')); }); - expect(result.current.idSelected).toEqual(['site_id']); + expect(result.current.idsSelected).toEqual(['site_id']); }); it('should handle show only neighbours', () => { - const { result } = renderHook(() => useTopologyState({ id: undefined })); + const { result } = renderHook(() => useTopologyState({ ids: undefined })); act(() => { result.current.handleShowOnlyNeighbours(true); @@ -63,7 +63,7 @@ describe('useTopologySiteState', () => { }); it('should handle move to node selection', () => { - const { result } = renderHook(() => useTopologyState({ id: undefined })); + const { result } = renderHook(() => useTopologyState({ ids: undefined })); act(() => { result.current.handleMoveToNodeSelectedChecked(true); diff --git a/src/pages/Topology/components/DisplayResources.tsx b/src/pages/Topology/components/DisplayResources.tsx index 0db1c756..85377794 100644 --- a/src/pages/Topology/components/DisplayResources.tsx +++ b/src/pages/Topology/components/DisplayResources.tsx @@ -1,4 +1,4 @@ -import { FC, FormEvent, Ref, useCallback, useRef, useState } from 'react'; +import { FC, FormEvent, Ref, useCallback, useEffect, useRef, useState } from 'react'; import { Button, @@ -35,39 +35,29 @@ const DisplayResources: FC = function ({ onSelect, options }) { - const [inputValue, setInputValue] = useState(findOptionSelected(options, id)?.name || ''); + const [inputValue, setInputValue] = useState(''); const [isOpen, setIsOpen] = useState(false); const filterValueRef = useRef(''); const textInputRef = useRef(); function handleToggleMenu() { - setIsOpen(!isOpen); + setIsOpen((prevIsOpen) => !prevIsOpen); } - function handleClear() { + const handleClear = useCallback(() => { setInputValue(''); filterValueRef.current = ''; textInputRef.current?.focus(); - - if (onSelect) { - onSelect(undefined); - } - } + onSelect?.(undefined); + }, [onSelect]); const handleSelect = useCallback( (idSelected: string) => { - const nameSelected = findOptionSelected(options, idSelected)?.name || ''; - setIsOpen(false); - setInputValue(nameSelected); - filterValueRef.current = ''; - - if (onSelect) { - onSelect(idSelected); - } + onSelect?.(idSelected); }, - [onSelect, options] + [onSelect] ); const handleTextInputChange = (_: FormEvent, selection: string) => { @@ -80,6 +70,17 @@ const DisplayResources: FC = function ({ !filterValueRef.current || name.toString().toLowerCase().includes(filterValueRef.current.toLowerCase()) ); + useEffect(() => { + if (id) { + setInputValue(findOptionNameSelected(options, id)); + filterValueRef.current = ''; + } + }, [id, options]); + + useEffect(() => { + setIsOpen(!!inputValue && !!filterValueRef.current); + }, [inputValue]); + return (