-
Notifications
You must be signed in to change notification settings - Fork 33
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
17 changed files
with
735 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
25 changes: 25 additions & 0 deletions
25
src/views/virtualmachines/tree/VirtualMachineTreeView.scss
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,25 @@ | ||
.vms-tree-view { | ||
overflow-x: hidden; | ||
} | ||
|
||
.vms-tree-view-body, | ||
.vms-tree-view-toolbar, | ||
.vms-tree-view-toolbar-content { | ||
padding: 0; | ||
} | ||
|
||
.pf-v5-c-tree-view__node .pf-v5-c-tree-view__node-count, | ||
.vms-tree-view-toolbar-buttons { | ||
display: flex; | ||
flex-grow: 1; | ||
flex-direction: row-reverse; | ||
} | ||
|
||
.vms-tree-view-toolbar-buttons { | ||
align-self: center; | ||
margin-right: var(--pf-global--spacer--sm); | ||
|
||
&__padding { | ||
padding: var(--pf-global--spacer--sm); | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
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,137 @@ | ||
import React, { FC, MouseEvent, useMemo } from 'react'; | ||
import { useLocation, useNavigate } from 'react-router-dom-v5-compat'; | ||
|
||
import VirtualMachineModel from '@kubevirt-ui/kubevirt-api/console/models/VirtualMachineModel'; | ||
import { useQueryParamsMethods } from '@kubevirt-utils/components/ListPageFilter/hooks/useQueryParamsMethods'; | ||
import { convertResourceArrayToMap, getResourceUrl } from '@kubevirt-utils/resources/shared'; | ||
import { getContentScrollableElement } from '@kubevirt-utils/utils/utils'; | ||
import { FilterValue, useActiveNamespace } from '@openshift-console/dynamic-plugin-sdk'; | ||
import { | ||
Drawer, | ||
DrawerContent, | ||
DrawerContentBody, | ||
DrawerPanelBody, | ||
DrawerPanelContent, | ||
TreeView, | ||
TreeViewDataItem, | ||
} from '@patternfly/react-core'; | ||
import { TEXT_FILTER_LABELS_ID } from '@virtualmachines/list/hooks/constants'; | ||
|
||
import TreeViewToolbar from './components/TreeViewToolbar'; | ||
import { useHideNamespaceBar } from './hooks/useHideNamespaceBar'; | ||
import { useSyncClicksEffects } from './hooks/useSyncClicksEffects'; | ||
import { useTreeViewData } from './hooks/useTreeViewData'; | ||
import { useTreeViewSearch } from './hooks/useTreeViewSearch'; | ||
import { | ||
FOLDER_SELECTOR_PREFIX, | ||
PROJECT_SELECTOR_PREFIX, | ||
TREE_VIEW_PANEL_ID, | ||
VM_FOLDER_LABEL, | ||
} from './utils/constants'; | ||
import { | ||
createTreeViewData, | ||
selectedTreeItem, | ||
setSelectedTreeItem, | ||
treeViewOpen, | ||
} from './utils/utils'; | ||
|
||
import './VirtualMachineTreeView.scss'; | ||
|
||
type VirtualMachineTreeViewProps = { | ||
onFilterChange?: (type: string, value: FilterValue) => void; | ||
}; | ||
|
||
const VirtualMachineTreeView: FC<VirtualMachineTreeViewProps> = ({ children, onFilterChange }) => { | ||
const [activeNamespace] = useActiveNamespace(); | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
|
||
const { setOrRemoveQueryArgument } = useQueryParamsMethods(); | ||
const { isAdmin, loaded, loadError, projectNames, vms } = useTreeViewData(); | ||
const vmsMapper = useMemo(() => convertResourceArrayToMap(vms, true), [vms]); | ||
|
||
const treeData = useMemo( | ||
() => createTreeViewData(projectNames, vms, activeNamespace, isAdmin, location.pathname), | ||
[projectNames, vms, activeNamespace, isAdmin, location.pathname], | ||
); | ||
|
||
const { filteredItems, onSearch, setShowAll, showAll } = useTreeViewSearch(treeData); | ||
|
||
useSyncClicksEffects(activeNamespace, loaded, location); | ||
useHideNamespaceBar(); | ||
|
||
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)) { | ||
return navigate( | ||
getResourceUrl({ | ||
activeNamespace: treeItemName, | ||
model: VirtualMachineModel, | ||
}), | ||
); | ||
} | ||
|
||
const [vmNamespace, vmName] = treeViewItem.id.split('/'); | ||
return navigate( | ||
getResourceUrl({ | ||
activeNamespace: vmNamespace, | ||
model: VirtualMachineModel, | ||
resource: vmsMapper?.[vmNamespace]?.[vmName], | ||
}), | ||
); | ||
}; | ||
|
||
return ( | ||
<Drawer isExpanded isInline position="start"> | ||
<DrawerContent | ||
panelContent={ | ||
<DrawerPanelContent | ||
style={{ | ||
height: getContentScrollableElement().offsetHeight || 0, | ||
}} | ||
className="vms-tree-view" | ||
id={TREE_VIEW_PANEL_ID} | ||
isResizable={treeViewOpen.value} | ||
> | ||
<DrawerPanelBody className="vms-tree-view-body"> | ||
<TreeView | ||
toolbar={ | ||
<TreeViewToolbar onSearch={onSearch} setShowAll={setShowAll} showAll={showAll} /> | ||
} | ||
activeItems={selectedTreeItem.value} | ||
allExpanded={showAll} | ||
data={!treeViewOpen.value ? [] : filteredItems ?? treeData} | ||
hasBadges={loaded} | ||
hasSelectableNodes | ||
onSelect={onSelect} | ||
/> | ||
</DrawerPanelBody> | ||
</DrawerPanelContent> | ||
} | ||
> | ||
<DrawerContentBody>{children}</DrawerContentBody> | ||
</DrawerContent> | ||
</Drawer> | ||
); | ||
}; | ||
|
||
export default VirtualMachineTreeView; |
80 changes: 80 additions & 0 deletions
80
src/views/virtualmachines/tree/components/TreeViewToolbar.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,80 @@ | ||
import React, { ChangeEvent, Dispatch, FC, SetStateAction } from 'react'; | ||
|
||
import { useKubevirtTranslation } from '@kubevirt-utils/hooks/useKubevirtTranslation'; | ||
import { | ||
Button, | ||
ButtonVariant, | ||
Toolbar, | ||
ToolbarContent, | ||
ToolbarItem, | ||
Tooltip, | ||
TreeViewSearch, | ||
} from '@patternfly/react-core'; | ||
import { PanelCloseIcon, PanelOpenIcon } from '@patternfly/react-icons'; | ||
|
||
import { | ||
CLOSED_DRAWER_SIZE, | ||
OPEN_DRAWER_SIZE, | ||
PANEL_WIDTH_PROPERTY, | ||
TREE_VIEW_PANEL_ID, | ||
TREE_VIEW_SEARCH_ID, | ||
} from '../utils/constants'; | ||
import { treeViewOpen } from '../utils/utils'; | ||
|
||
type TreeViewToolbarProps = { | ||
onSearch: (event: ChangeEvent<HTMLInputElement>) => void; | ||
setShowAll: Dispatch<SetStateAction<boolean>>; | ||
showAll: boolean; | ||
}; | ||
|
||
const TreeViewToolbar: FC<TreeViewToolbarProps> = ({ onSearch, setShowAll, showAll }) => { | ||
const { t } = useKubevirtTranslation(); | ||
|
||
const toggleDrawer = () => { | ||
treeViewOpen.value = !treeViewOpen.value; | ||
const panel = document.getElementById(TREE_VIEW_PANEL_ID); | ||
panel.style.setProperty( | ||
PANEL_WIDTH_PROPERTY, | ||
treeViewOpen.value ? OPEN_DRAWER_SIZE : CLOSED_DRAWER_SIZE, | ||
); | ||
}; | ||
|
||
return ( | ||
<Toolbar className="vms-tree-view-toolbar"> | ||
<ToolbarContent className="vms-tree-view-toolbar-content"> | ||
<ToolbarItem> | ||
{treeViewOpen.value && ( | ||
<TreeViewSearch | ||
id={TREE_VIEW_SEARCH_ID} | ||
name={TREE_VIEW_SEARCH_ID} | ||
onSearch={onSearch} | ||
placeholder={t('Search')} | ||
/> | ||
)} | ||
</ToolbarItem> | ||
<ToolbarItem className="vms-tree-view-toolbar-buttons"> | ||
<Tooltip content={treeViewOpen.value ? t('Close') : t('Open')}> | ||
<Button | ||
className="vms-tree-view-toolbar-buttons__padding" | ||
onClick={toggleDrawer} | ||
variant={ButtonVariant.plain} | ||
> | ||
{treeViewOpen.value ? <PanelCloseIcon /> : <PanelOpenIcon />} | ||
</Button> | ||
</Tooltip> | ||
{treeViewOpen.value && ( | ||
<Button | ||
className="vms-tree-view-toolbar-buttons__padding" | ||
onClick={() => setShowAll((prev) => !prev)} | ||
variant={ButtonVariant.link} | ||
> | ||
{showAll ? t('Collapse all') : t('Expand all')} | ||
</Button> | ||
)} | ||
</ToolbarItem> | ||
</ToolbarContent> | ||
</Toolbar> | ||
); | ||
}; | ||
|
||
export default TreeViewToolbar; |
Oops, something went wrong.