-
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
10 changed files
with
386 additions
and
18 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); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
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,112 @@ | ||
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 { useQueryParamsMethods } from '@kubevirt-utils/components/ListPageFilter/hooks/useQueryParamsMethods'; | ||
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 { TEXT_FILTER_LABELS_ID } from '@virtualmachines/list/hooks/constants'; | ||
|
||
import { useSyncClicksEffects } from './hooks/useSyncClicksEffects'; | ||
import { useTreeViewData } from './hooks/useTreeViewData'; | ||
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 [activeNamespace, setActiveNamespace] = useActiveNamespace(); | ||
|
||
const { isAdmin, loaded, loadError, projectNames, vms } = useTreeViewData(); | ||
|
||
const treeData = useMemo(() => { | ||
const [data, dataMap] = createTreeViewData( | ||
projectNames, | ||
vms, | ||
activeNamespace, | ||
isAdmin, | ||
pathname, | ||
); | ||
treeDataMap.value = dataMap; | ||
return data; | ||
}, [projectNames, vms, activeNamespace, isAdmin]); | ||
|
||
useSyncClicksEffects(activeNamespace, loaded, location); | ||
|
||
if (loadError) return <>{children}</>; | ||
|
||
const onSelect = (_event: MouseEvent, treeViewItem: TreeViewDataItem) => { | ||
setSelectedTreeItem(treeViewItem); | ||
onFilterChange?.(TEXT_FILTER_LABELS_ID, 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(TEXT_FILTER_LABELS_ID, `${VM_FOLDER_LABEL}=${treeItemName}`); | ||
return onFilterChange?.(TEXT_FILTER_LABELS_ID, { | ||
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={loaded} | ||
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,58 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { VirtualMachineModelGroupVersionKind } from '@kubevirt-ui/kubevirt-api/console'; | ||
import { V1VirtualMachine } from '@kubevirt-ui/kubevirt-api/kubevirt'; | ||
import { useIsAdmin } from '@kubevirt-utils/hooks/useIsAdmin'; | ||
import useProjects from '@kubevirt-utils/hooks/useProjects'; | ||
import { isEmpty } from '@kubevirt-utils/utils/utils'; | ||
import { useK8sWatchResource, useK8sWatchResources } from '@openshift-console/dynamic-plugin-sdk'; | ||
import { OBJECTS_FETCHING_LIMIT } from '@virtualmachines/utils'; | ||
|
||
type UseTreeViewData = { | ||
isAdmin: boolean; | ||
loaded: boolean; | ||
loadError: any; | ||
projectNames: string[]; | ||
vms: V1VirtualMachine[]; | ||
}; | ||
|
||
export const useTreeViewData = (): UseTreeViewData => { | ||
const isAdmin = useIsAdmin(); | ||
|
||
const [projectNames, projectNamesLoaded, projectNamesError] = useProjects(); | ||
|
||
const [allVMs, allVMsLoaded] = useK8sWatchResource<V1VirtualMachine[]>({ | ||
groupVersionKind: VirtualMachineModelGroupVersionKind, | ||
isList: true, | ||
limit: OBJECTS_FETCHING_LIMIT, | ||
}); | ||
|
||
// user has limited access, so we can only get vms from allowed namespaces | ||
const allowedResources = useK8sWatchResources<{ [key: string]: V1VirtualMachine[] }>( | ||
Object.fromEntries( | ||
projectNamesLoaded && !isAdmin | ||
? (projectNames || []).map((namespace) => [ | ||
namespace, | ||
{ | ||
groupVersionKind: VirtualMachineModelGroupVersionKind, | ||
isList: true, | ||
namespace, | ||
}, | ||
]) | ||
: [], | ||
), | ||
); | ||
|
||
const memoizedVMs = useMemo( | ||
() => (isAdmin ? allVMs : Object.values(allowedResources).flatMap((resource) => resource.data)), | ||
[allVMs, allowedResources, isAdmin], | ||
); | ||
|
||
return { | ||
isAdmin, | ||
loaded: projectNamesLoaded && (isAdmin ? allVMsLoaded : !isEmpty(memoizedVMs)), | ||
loadError: projectNamesError, | ||
projectNames, | ||
vms: memoizedVMs, | ||
}; | ||
}; |
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.