Skip to content

Commit 2315123

Browse files
memo + removing as string
1 parent 2198689 commit 2315123

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/components/Graphs/Graph.tsx

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import React, { useMemo, useState } from 'react';
2-
import { ReactFlow, Background, Controls, MarkerType } from '@xyflow/react';
1+
import React, { useState, useCallback, useMemo } from 'react';
2+
import { ReactFlow, Background, Controls, MarkerType, Node } from '@xyflow/react';
33
import type { NodeProps } from '@xyflow/react';
44
import { RadioButton, FlexBox, FlexBoxAlignItems } from '@ui5/webcomponents-react';
55
import styles from './Graph.module.css';
66
import '@xyflow/react/dist/style.css';
7-
import { ManagedResourceItem } from './types';
7+
import { ManagedResourceItem, NodeData } from './types';
88
import CustomNode from './CustomNode';
99
import { Legend, LegendItem } from './Legend';
1010
import { YamlViewDialog } from '../Yaml/YamlViewDialog';
@@ -14,34 +14,35 @@ import { removeManagedFieldsProperty } from '../../utils/removeManagedFieldsProp
1414
import { useTranslation } from 'react-i18next';
1515
import { useGraph } from './useGraph';
1616

17+
const nodeTypes = {
18+
custom: (props: NodeProps<Node<NodeData, 'custom'>>) => (
19+
<CustomNode
20+
label={props.data.label}
21+
type={props.data.type}
22+
status={props.data.status}
23+
onYamlClick={() => props.data.onYamlClick(props.data.item)}
24+
/>
25+
),
26+
};
27+
1728
const Graph: React.FC = () => {
1829
const { t } = useTranslation();
1930
const [colorBy, setColorBy] = useState<'provider' | 'source'>('provider');
20-
const { nodes, edges, colorMap, treeData, loading, error } = useGraph(colorBy);
21-
2231
const [yamlDialogOpen, setYamlDialogOpen] = useState(false);
2332
const [yamlResource, setYamlResource] = useState<ManagedResourceItem | null>(null);
2433

25-
const handleYamlClick = (item: ManagedResourceItem) => {
34+
const handleYamlClick = useCallback((item: ManagedResourceItem) => {
2635
setYamlResource(item);
2736
setYamlDialogOpen(true);
28-
};
37+
}, []);
2938

30-
const nodeTypes = {
31-
custom: (props: NodeProps) => (
32-
<CustomNode
33-
label={props.data.label as string}
34-
type={props.data.type as string}
35-
status={props.data.status as string}
36-
onYamlClick={() => handleYamlClick(props.data.item as ManagedResourceItem)}
37-
/>
38-
),
39-
};
39+
const { nodes, edges, colorMap, treeData, loading, error } = useGraph(colorBy, handleYamlClick);
4040

4141
const yamlString = useMemo(
4242
() => (yamlResource ? stringify(removeManagedFieldsProperty(yamlResource)) : ''),
4343
[yamlResource],
4444
);
45+
4546
const yamlFilename = useMemo(() => {
4647
if (!yamlResource) return '';
4748
const { kind, metadata } = yamlResource;

src/components/Graphs/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@ export interface NodeData {
6161
parentId?: string;
6262
extraRefs: string[];
6363
item: ManagedResourceItem;
64+
onYamlClick: (item: ManagedResourceItem) => void;
6465
}

src/components/Graphs/useGraph.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ function buildGraph(
7676
return { nodes: Object.values(nodeMap), edges: edgeList };
7777
}
7878

79-
export function useGraph(colorBy: 'provider' | 'source') {
79+
export function useGraph(colorBy: 'provider' | 'source', onYamlClick: (item: ManagedResourceItem) => void) {
8080
const {
8181
data: managedResources,
8282
isLoading: managedResourcesLoading,
@@ -154,12 +154,13 @@ export function useGraph(colorBy: 'provider' | 'source') {
154154
parentId,
155155
extraRefs,
156156
item,
157+
onYamlClick,
157158
});
158159
}
159160
});
160161
});
161162
return Array.from(allNodesMap.values());
162-
}, [managedResources, providerConfigsList]);
163+
}, [managedResources, providerConfigsList, onYamlClick]);
163164

164165
const colorMap = useMemo(() => generateColorMap(treeData, colorBy), [treeData, colorBy]);
165166

0 commit comments

Comments
 (0)