-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aviv Turgeman <[email protected]>
- Loading branch information
Showing
8 changed files
with
336 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.pf-v5-l-grid { | ||
.tree-view-grid-item { | ||
overflow-y: auto; | ||
border-right: var(--pf-v5-global--BorderWidth--sm) solid var(--pf-v5-global--BorderColor--100); | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
src/views/virtualmachines/tree/VirtualMachineTreeView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import React, { FC, MouseEvent, useMemo } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router-dom-v5-compat'; | ||
import { VirtualMachineModel } from 'src/views/dashboard-extensions/utils'; | ||
|
||
import { V1VirtualMachine } from '@kubevirt-ui/kubevirt-api/kubevirt'; | ||
import { useQueryParamsMethods } from '@kubevirt-utils/components/ListPageFilter/hooks/useQueryParamsMethods'; | ||
import { useIsAdmin } from '@kubevirt-utils/hooks/useIsAdmin'; | ||
import useKubevirtWatchResource from '@kubevirt-utils/hooks/useKubevirtWatchResource'; | ||
import useProjects from '@kubevirt-utils/hooks/useProjects'; | ||
import { VirtualMachineModelGroupVersionKind } from '@kubevirt-utils/models'; | ||
import { convertResourceArrayToMap, getResourceUrl } from '@kubevirt-utils/resources/shared'; | ||
import { FilterValue, useActiveNamespace } from '@openshift-console/dynamic-plugin-sdk'; | ||
import { Grid, GridItem, TreeView, TreeViewDataItem } from '@patternfly/react-core'; | ||
import { OBJECTS_FETCHING_LIMIT } from '@virtualmachines/utils'; | ||
|
||
import { useSyncClicksEffects } from './hooks/useSyncClicksEffects'; | ||
import { | ||
FOLDER_SELECTOR_PREFIX, | ||
PROJECT_SELECTOR_PREFIX, | ||
VM_FOLDER_LABEL, | ||
} from './utils/constants'; | ||
import { | ||
createTreeViewData, | ||
selectedTreeItem, | ||
setSelectedTreeItem, | ||
treeDataMap, | ||
} from './utils/utils'; | ||
|
||
import './VirtualMachineTreeView.scss'; | ||
|
||
type VirtualMachineTreeViewProps = { | ||
onFilterChange?: (type: string, value: FilterValue) => void; | ||
}; | ||
|
||
const VirtualMachineTreeView: FC<VirtualMachineTreeViewProps> = ({ children, onFilterChange }) => { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const { setOrRemoveQueryArgument } = useQueryParamsMethods(); | ||
const pathname = location.pathname; | ||
const isAdmin = useIsAdmin(); | ||
const [activeNamespace, setActiveNamespace] = useActiveNamespace(); | ||
|
||
const [vms, vmsLoaded, vmsError] = useKubevirtWatchResource<V1VirtualMachine[]>({ | ||
groupVersionKind: VirtualMachineModelGroupVersionKind, | ||
isList: true, | ||
limit: OBJECTS_FETCHING_LIMIT, | ||
}); | ||
const [projectNames, projectNamesLoaded, projectNamesError] = useProjects(); | ||
|
||
const treeData = useMemo(() => { | ||
const [data, dataMap] = createTreeViewData( | ||
projectNames, | ||
vms, | ||
activeNamespace, | ||
isAdmin, | ||
pathname, | ||
); | ||
treeDataMap.value = dataMap; | ||
return data; | ||
}, [projectNames, vms, activeNamespace, isAdmin]); | ||
|
||
useSyncClicksEffects(activeNamespace, vmsLoaded && projectNamesLoaded, location); | ||
|
||
if (vmsError || projectNamesError) return <>{children}</>; | ||
|
||
const onSelect = (_event: MouseEvent, treeViewItem: TreeViewDataItem) => { | ||
setSelectedTreeItem(treeViewItem); | ||
onFilterChange?.('labels', null); | ||
|
||
const treeItemName = treeViewItem.name as string; | ||
if (treeViewItem.id.startsWith(FOLDER_SELECTOR_PREFIX)) { | ||
const [_, folderNamespace] = treeViewItem.id.split('/'); | ||
navigate( | ||
getResourceUrl({ | ||
activeNamespace: folderNamespace, | ||
model: VirtualMachineModel, | ||
}), | ||
); | ||
setOrRemoveQueryArgument('labels', `${VM_FOLDER_LABEL}=${treeItemName}`); | ||
return onFilterChange?.('labels', { all: [`${VM_FOLDER_LABEL}=${treeItemName}`] }); | ||
} | ||
|
||
if (treeViewItem.id.startsWith(PROJECT_SELECTOR_PREFIX)) { | ||
setActiveNamespace(treeItemName); | ||
return navigate( | ||
getResourceUrl({ | ||
activeNamespace: treeItemName, | ||
model: VirtualMachineModel, | ||
}), | ||
); | ||
} | ||
|
||
const vmsMapper = convertResourceArrayToMap(vms, true); | ||
const [vmNamespace, vmName] = treeViewItem.id.split('/'); | ||
return navigate( | ||
getResourceUrl({ | ||
activeNamespace: vmNamespace, | ||
model: VirtualMachineModel, | ||
resource: vmsMapper?.[vmNamespace]?.[vmName], | ||
}), | ||
); | ||
}; | ||
|
||
return ( | ||
<Grid style={{ height: document.getElementById('content-scrollable')?.offsetHeight || 0 }}> | ||
<GridItem className="tree-view-grid-item" span={3}> | ||
<TreeView | ||
activeItems={selectedTreeItem.value} | ||
data={treeData} | ||
hasBadges={vmsLoaded && projectNamesLoaded} | ||
hasSelectableNodes | ||
onSelect={onSelect} | ||
/> | ||
</GridItem> | ||
<GridItem span={9}>{children}</GridItem> | ||
</Grid> | ||
); | ||
}; | ||
|
||
export default VirtualMachineTreeView; |
52 changes: 52 additions & 0 deletions
52
src/views/virtualmachines/tree/hooks/useSyncClicksEffects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useEffect } from 'react'; | ||
import { Location } from 'react-router-dom-v5-compat'; | ||
|
||
import { ALL_NAMESPACES, ALL_NAMESPACES_SESSION_KEY } from '@kubevirt-utils/hooks/constants'; | ||
import { VirtualMachineModelRef } from '@kubevirt-utils/models'; | ||
|
||
import { FOLDER_SELECTOR_PREFIX, PROJECT_SELECTOR_PREFIX } from '../utils/constants'; | ||
import { setSelectedTreeItem, treeDataMap } from '../utils/utils'; | ||
|
||
export const useSyncClicksEffects = ( | ||
activeNamespace: string, | ||
loaded: boolean, | ||
location: Location<any>, | ||
) => { | ||
useEffect(() => { | ||
const queryParams = new URLSearchParams(location.search); | ||
|
||
const folderFilterName = queryParams.get('labels')?.split('=')?.[1]; | ||
const pathname = location.pathname; | ||
if (loaded) { | ||
const dataMap = treeDataMap.value; | ||
if (pathname.startsWith(`/k8s/${ALL_NAMESPACES}`)) { | ||
setSelectedTreeItem(dataMap[ALL_NAMESPACES_SESSION_KEY]); | ||
return; | ||
} | ||
|
||
if ( | ||
pathname.startsWith('/k8s/ns/') && | ||
(pathname.endsWith(VirtualMachineModelRef) || | ||
pathname.endsWith(`${VirtualMachineModelRef}/`)) | ||
) { | ||
if (folderFilterName) { | ||
setSelectedTreeItem( | ||
dataMap[`${FOLDER_SELECTOR_PREFIX}/${activeNamespace}/${folderFilterName}`], | ||
); | ||
return; | ||
} | ||
|
||
setSelectedTreeItem(dataMap[`${PROJECT_SELECTOR_PREFIX}/${activeNamespace}`]); | ||
return; | ||
} | ||
|
||
const vmName = pathname.split('/')[5]; | ||
if (vmName && pathname.includes(vmName)) { | ||
setSelectedTreeItem(dataMap[`${activeNamespace}/${vmName}`]); | ||
return; | ||
} | ||
|
||
return; | ||
} | ||
}, [activeNamespace, loaded, location.search, location.pathname]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const VM_FOLDER_LABEL = 'vm.kubevirt.io/folder'; | ||
export const PROJECT_SELECTOR_PREFIX = 'projectSelector'; | ||
export const FOLDER_SELECTOR_PREFIX = 'folderSelector'; |
Oops, something went wrong.