From 0a2cc2fccacf7c022fea41fbcc2b26e9c5d26f86 Mon Sep 17 00:00:00 2001 From: Aviv Turgeman Date: Tue, 31 Dec 2024 14:37:34 +0200 Subject: [PATCH] CNV-53452: VirtualMachines page crashes for nonpriv user when user hasn't created a project Signed-off-by: Aviv Turgeman --- .../virtualmachines/tree/VirtualMachineTreeView.tsx | 4 +++- .../tree/components/TreeViewContent.tsx | 5 +++++ .../tree/components/TreeViewToolbar.tsx | 10 ++++++++-- .../virtualmachines/tree/hooks/useTreeViewData.ts | 8 +++++--- src/views/virtualmachines/tree/utils/utils.tsx | 4 +++- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/views/virtualmachines/tree/VirtualMachineTreeView.tsx b/src/views/virtualmachines/tree/VirtualMachineTreeView.tsx index 5ebb4d99f..dccef7537 100644 --- a/src/views/virtualmachines/tree/VirtualMachineTreeView.tsx +++ b/src/views/virtualmachines/tree/VirtualMachineTreeView.tsx @@ -41,7 +41,8 @@ const VirtualMachineTreeView: FC = ({ children, onF const { featureEnabled: treeViewEnabled, loading } = useFeatures(TREE_VIEW); - const { loaded, loadError, selectedTreeItem, treeData, vms } = useTreeViewData(activeNamespace); + const { isSwitchDisabled, loaded, loadError, selectedTreeItem, treeData, vms } = + useTreeViewData(activeNamespace); const onSelect = useTreeViewSelect(onFilterChange, vms); @@ -83,6 +84,7 @@ const VirtualMachineTreeView: FC = ({ children, onF > void; selectedTreeItem: TreeViewDataItem; @@ -28,6 +29,7 @@ type TreeViewContentProps = { const TreeViewContent: FC = ({ isOpen, + isSwitchDisabled, loaded, onSelect, selectedTreeItem, @@ -37,6 +39,7 @@ const TreeViewContent: FC = ({ const { t } = useKubevirtTranslation(); const [showAll, setShowAll] = useState(); const { filteredTreeData, onSearch } = UseFilteredTreeView(treeData, setShowAll); + if (!isOpen) { return ( = ({ /> ); } + return ( <> = ({ toggleDrawer={toggleDrawer} /> } + isSwitchDisabled={isSwitchDisabled} onSearch={onSearch} /> diff --git a/src/views/virtualmachines/tree/components/TreeViewToolbar.tsx b/src/views/virtualmachines/tree/components/TreeViewToolbar.tsx index 09b32d226..b4b1cb972 100644 --- a/src/views/virtualmachines/tree/components/TreeViewToolbar.tsx +++ b/src/views/virtualmachines/tree/components/TreeViewToolbar.tsx @@ -19,10 +19,15 @@ import { HIDE, SHOW, SHOW_EMPTY_PROJECTS_KEY, TREE_VIEW_SEARCH_ID } from '../uti type TreeViewToolbarProps = { closeComponent: ReactNode; + isSwitchDisabled: boolean; onSearch: (event: ChangeEvent) => void; }; -const TreeViewToolbar: FC = ({ closeComponent, onSearch }) => { +const TreeViewToolbar: FC = ({ + closeComponent, + isSwitchDisabled, + onSearch, +}) => { const { t } = useKubevirtTranslation(); const [showEmptyProjects, setShowEmptyProjects] = useLocalStorage(SHOW_EMPTY_PROJECTS_KEY, HIDE); @@ -51,8 +56,9 @@ const TreeViewToolbar: FC = ({ closeComponent, onSearch }) setShowEmptyProjects(checked ? HIDE : SHOW)} /> diff --git a/src/views/virtualmachines/tree/hooks/useTreeViewData.ts b/src/views/virtualmachines/tree/hooks/useTreeViewData.ts index 86076fe91..429444256 100644 --- a/src/views/virtualmachines/tree/hooks/useTreeViewData.ts +++ b/src/views/virtualmachines/tree/hooks/useTreeViewData.ts @@ -10,10 +10,10 @@ import { useK8sWatchResource, useK8sWatchResources } from '@openshift-console/dy import { TreeViewDataItem } from '@patternfly/react-core'; import { OBJECTS_FETCHING_LIMIT } from '@virtualmachines/utils'; -import { createTreeViewData } from '../utils/utils'; +import { createTreeViewData, isSystemNamespace } from '../utils/utils'; type UseTreeViewData = { - isAdmin: boolean; + isSwitchDisabled: boolean; loaded: boolean; loadError: any; selectedTreeItem: TreeViewDataItem; @@ -66,8 +66,10 @@ export const useTreeViewData = (activeNamespace: string): UseTreeViewData => { [projectNames, memoizedVMs, activeNamespace, isAdmin, treeViewFoldersEnabled], ); + const isSwitchDisabled = useMemo(() => projectNames.every(isSystemNamespace), [projectNames]); + return { - isAdmin, + isSwitchDisabled, loaded: projectNamesLoaded && (isAdmin diff --git a/src/views/virtualmachines/tree/utils/utils.tsx b/src/views/virtualmachines/tree/utils/utils.tsx index 4d9992384..eef58abc2 100644 --- a/src/views/virtualmachines/tree/utils/utils.tsx +++ b/src/views/virtualmachines/tree/utils/utils.tsx @@ -189,7 +189,9 @@ export const createTreeViewData = ( treeDataMap.value = treeViewDataMap; - return [[allNamespacesTreeItem] ?? treeViewData, getSelectedTreeItem()]; + const tree = allNamespacesTreeItem ? [allNamespacesTreeItem] : treeViewData; + + return [tree, getSelectedTreeItem()]; }; export const filterItems = (item: TreeViewDataItem, input: string) => {