Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when moving between VMs from different namespaces #2323

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { Checkbox } from '@patternfly/react-core';
import VirtualMachineActions from '@virtualmachines/actions/components/VirtualMachineActions/VirtualMachineActions';
import useVirtualMachineActionsProvider from '@virtualmachines/actions/hooks/useVirtualMachineActionsProvider';
import { deselectVM, isVMSelected, selectVM } from '@virtualmachines/list/selectedVMs';
import { setSelectedTreeItem, treeDataMap } from '@virtualmachines/tree/utils/utils';

import VirtualMachineStatus from '../VirtualMachineStatus/VirtualMachineStatus';
import { VMStatusConditionLabelList } from '../VMStatusConditionLabel';
Expand All @@ -39,7 +38,6 @@ const VirtualMachineRowLayout: FC<

const vmName = useMemo(() => getName(obj), [obj]);
const vmNamespace = useMemo(() => getNamespace(obj), [obj]);

const [actions] = useVirtualMachineActionsProvider(obj, vmim, isSingleNodeCluster);
return (
<>
Expand All @@ -52,9 +50,6 @@ const VirtualMachineRowLayout: FC<
</TableData>
<TableData activeColumnIDs={activeColumnIDs} className="pf-m-width-20 vm-column" id="name">
<ResourceLink
onClick={() => {
setSelectedTreeItem(treeDataMap.value[`${vmNamespace}/${vmName}`]);
}}
groupVersionKind={VirtualMachineModelGroupVersionKind}
name={vmName}
namespace={vmNamespace}
Expand Down
5 changes: 2 additions & 3 deletions src/views/virtualmachines/tree/VirtualMachineTreeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {

import TreeViewContent from './components/TreeViewContent';
import { useHideNamespaceBar } from './hooks/useHideNamespaceBar';
import { useSyncClicksEffects } from './hooks/useSyncClicksEffects';
import { useTreeViewData } from './hooks/useTreeViewData';
import useTreeViewSelect from './hooks/useTreeViewSelect';
import {
Expand Down Expand Up @@ -42,13 +41,12 @@ const VirtualMachineTreeView: FC<VirtualMachineTreeViewProps> = ({ children, onF

const { featureEnabled: treeViewEnabled, loading } = useFeatures(TREE_VIEW);

const { loaded, loadError, treeData, vms } = useTreeViewData(activeNamespace);
const { loaded, loadError, selectedTreeItem, treeData, vms } = useTreeViewData(activeNamespace);

const onSelect = useTreeViewSelect(onFilterChange, vms);

const isOpen = useMemo(() => drawerOpen === SHOW, [drawerOpen]);

useSyncClicksEffects(activeNamespace, loaded);
useHideNamespaceBar();
useEffect(() => {
const drawerPanel = document.getElementById(TREE_VIEW_PANEL_ID);
Expand Down Expand Up @@ -87,6 +85,7 @@ const VirtualMachineTreeView: FC<VirtualMachineTreeViewProps> = ({ children, onF
isOpen={isOpen}
loaded={loaded}
onSelect={onSelect}
selectedTreeItem={selectedTreeItem}
toggleDrawer={toggleDrawer}
treeData={treeData}
/>
Expand Down
7 changes: 5 additions & 2 deletions src/views/virtualmachines/tree/components/CreateProject.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import React, { FC } from 'react';

import CreateProjectModal from '@kubevirt-utils/components/CreateProjectModal/CreateProjectModal';
import { useQueryParamsMethods } from '@kubevirt-utils/components/ListPageFilter/hooks/useQueryParamsMethods';
import { useModal } from '@kubevirt-utils/components/ModalProvider/ModalProvider';
import { useKubevirtTranslation } from '@kubevirt-utils/hooks/useKubevirtTranslation';
import { getName } from '@kubevirt-utils/resources/shared';
import { useActiveNamespace } from '@openshift-console/dynamic-plugin-sdk';
import { Button, ButtonVariant } from '@patternfly/react-core';
import { PlusCircleIcon } from '@patternfly/react-icons';
import { TEXT_FILTER_LABELS_ID } from '@virtualmachines/list/hooks/constants';

const CreateProject: FC = () => {
const { t } = useKubevirtTranslation();
const [_, setActiveNamespace] = useActiveNamespace();
const { removeQueryArguments } = useQueryParamsMethods();
const { createModal } = useModal();
return (
<Button
Expand All @@ -19,8 +22,8 @@ const CreateProject: FC = () => {
<CreateProjectModal
{...props}
createdProject={(namespace) => {
const nsName = getName(namespace);
setActiveNamespace(nsName);
removeQueryArguments(TEXT_FILTER_LABELS_ID);
setActiveNamespace(getName(namespace));
}}
/>
))
Expand Down
6 changes: 3 additions & 3 deletions src/views/virtualmachines/tree/components/TreeViewContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from '@patternfly/react-core';

import UseFilteredTreeView from '../hooks/UseFilteredTreeView';
import { selectedTreeItem } from '../utils/utils';

import CreateProject from './CreateProject';
import PanelToggleButton from './PanelToggleButton';
Expand All @@ -22,6 +21,7 @@ type TreeViewContentProps = {
isOpen: boolean;
loaded: boolean;
onSelect: (_event: React.MouseEvent, treeViewItem: TreeViewDataItem) => void;
selectedTreeItem: TreeViewDataItem;
toggleDrawer: () => void;
treeData: TreeViewDataItem[];
};
Expand All @@ -30,12 +30,12 @@ const TreeViewContent: FC<TreeViewContentProps> = ({
isOpen,
loaded,
onSelect,
selectedTreeItem,
toggleDrawer,
treeData,
}) => {
const { t } = useKubevirtTranslation();
const [showAll, setShowAll] = useState<boolean>();

const { filteredTreeData, onSearch } = UseFilteredTreeView(treeData, setShowAll);
if (!isOpen) {
return (
Expand Down Expand Up @@ -69,7 +69,7 @@ const TreeViewContent: FC<TreeViewContentProps> = ({
</DrawerHead>
<DrawerPanelBody className="vms-tree-view-body">
<TreeView
activeItems={selectedTreeItem.value}
activeItems={[selectedTreeItem]}
allExpanded={showAll}
data={filteredTreeData}
hasBadges={loaded}
Expand Down
39 changes: 0 additions & 39 deletions src/views/virtualmachines/tree/hooks/useSyncClicksEffects.ts

This file was deleted.

5 changes: 3 additions & 2 deletions src/views/virtualmachines/tree/hooks/useTreeViewData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ type UseTreeViewData = {
isAdmin: boolean;
loaded: boolean;
loadError: any;
selectedTreeItem: TreeViewDataItem;
treeData: TreeViewDataItem[];
vms: V1VirtualMachine[];
};

export const useTreeViewData = (activeNamespace: string): UseTreeViewData => {
const isAdmin = useIsAdmin();
const { featureEnabled: treeViewFoldersEnabled } = useFeatures(TREE_VIEW_FOLDERS);

const [projectNames, projectNamesLoaded, projectNamesError] = useProjects();

const [allVMs, allVMsLoaded] = useK8sWatchResource<V1VirtualMachine[]>({
Expand Down Expand Up @@ -53,7 +53,7 @@ export const useTreeViewData = (activeNamespace: string): UseTreeViewData => {
[allVMs, allowedResources, isAdmin],
);

const treeData = useMemo(
const [treeData, selectedTreeItem] = useMemo(
() =>
createTreeViewData(
projectNames,
Expand All @@ -74,6 +74,7 @@ export const useTreeViewData = (activeNamespace: string): UseTreeViewData => {
? allVMsLoaded
: Object.values(allowedResources).some((resource) => resource.loaded)),
loadError: projectNamesError,
selectedTreeItem,
treeData,
vms: memoizedVMs,
};
Expand Down
5 changes: 2 additions & 3 deletions src/views/virtualmachines/tree/hooks/useTreeViewSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ import {
PROJECT_SELECTOR_PREFIX,
VM_FOLDER_LABEL,
} from '../utils/constants';
import { setSelectedTreeItem } from '../utils/utils';

const useTreeViewSelect = (
onFilterChange: (type: string, value: FilterValue) => void,
vms: V1VirtualMachine[],
) => {
const navigate = useNavigate();
const { setOrRemoveQueryArgument } = useQueryParamsMethods();

const vmsMapper = useMemo(() => convertResourceArrayToMap(vms, true), [vms]);

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('/');
const [__, folderNamespace] = treeViewItem.id.split('/');
navigate(
getResourceUrl({
activeNamespace: folderNamespace,
Expand Down
28 changes: 0 additions & 28 deletions src/views/virtualmachines/tree/icons/ClosePanelIcon.tsx

This file was deleted.

28 changes: 0 additions & 28 deletions src/views/virtualmachines/tree/icons/OpenPanelIcon.tsx

This file was deleted.

1 change: 1 addition & 0 deletions src/views/virtualmachines/tree/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export const SYSTEM_NAMESPACES = ['default', 'openshift'];
export const SHOW_EMPTY_PROJECTS_KEY = 'showEmptyProjects';
export const TREE_VIEW_LAST_WIDTH = 'treeViewLastWidth';
export const SHOW_TREE_VIEW = 'showTreeView';
export const SELECTED_ITEM = 'selectedItem';
export const SHOW = 'show';
export const HIDE = 'hide';
35 changes: 22 additions & 13 deletions src/views/virtualmachines/tree/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import {
} from './constants';

export const treeDataMap = signal<Record<string, TreeViewDataItem>>(null);
export const selectedTreeItem = signal<TreeViewDataItem[]>(null);
export const setSelectedTreeItem = (selected: TreeViewDataItem) =>
(selectedTreeItem.value = [selected]);

const buildProjectMap = (
vms: V1VirtualMachine[],
Expand Down Expand Up @@ -161,7 +158,7 @@ export const createTreeViewData = (
isAdmin: boolean,
pathname: string,
foldersEnabled: boolean,
): TreeViewDataItem[] => {
): [TreeViewDataItem[], TreeViewDataItem] => {
const currentPageVMName = pathname.split('/')[5];
const treeViewDataMap: Record<string, TreeViewDataItem> = {};
const projectMap = buildProjectMap(vms, currentPageVMName, treeViewDataMap, foldersEnabled);
Expand All @@ -170,17 +167,29 @@ export const createTreeViewData = (
createProjectTreeItem(project, projectMap, activeNamespace, currentPageVMName, treeViewDataMap),
);

if (isAdmin) {
const allNamespacesTreeItem = createAllNamespacesTreeItem(
treeViewData,
treeViewDataMap,
projectMap,
);
return [allNamespacesTreeItem];
}
const allNamespacesTreeItem = isAdmin
? createAllNamespacesTreeItem(treeViewData, treeViewDataMap, projectMap)
: null;

const getSelectedTreeItem = (): TreeViewDataItem => {
if (activeNamespace === ALL_NAMESPACES_SESSION_KEY) return allNamespacesTreeItem;

if (currentPageVMName) return treeViewDataMap[`${activeNamespace}/${currentPageVMName}`];

const params = new URLSearchParams(window.location.search);

if (!params?.has('labels'))
return treeViewDataMap[`${PROJECT_SELECTOR_PREFIX}/${activeNamespace}`];

const folderLabel: string = params.values().next().value;
const folder = folderLabel.split('=')?.[1];

return treeViewDataMap[`${FOLDER_SELECTOR_PREFIX}/${activeNamespace}/${folder}`];
};

treeDataMap.value = treeViewDataMap;
return treeViewData;

return [[allNamespacesTreeItem] ?? treeViewData, getSelectedTreeItem()];
};

export const filterItems = (item: TreeViewDataItem, input: string) => {
Expand Down
Loading