Skip to content

Commit ce602d6

Browse files
moving yalm viewer outside of Reactflow to fix bug
1 parent 487c72a commit ce602d6

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

src/components/Graphs/CustomNode.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import React from 'react';
22
import { Handle, Position } from 'reactflow';
33
import { ThemingParameters } from '@ui5/webcomponents-react-base';
4-
import { YamlViewButton } from '../Yaml/YamlViewButton';
4+
import { Button, Icon } from '@ui5/webcomponents-react';
55
import StatusIcon from './StatusIcon';
6-
import { CustomNodeProps } from './types';
6+
import { CustomNodeProps, ManagedResourceItem } from './types';
77
import styles from './CustomNode.module.css';
88

9-
const CustomNode: React.FC<CustomNodeProps> = ({ data }) => (
9+
const CustomNode: React.FC<CustomNodeProps & { onYamlClick: (item: ManagedResourceItem) => void }> = ({
10+
data,
11+
onYamlClick,
12+
}) => (
1013
<div className={styles.nodeContainer} style={{ fontFamily: ThemingParameters.sapFontFamily }}>
1114
<Handle type="target" position={Position.Top} style={{ visibility: 'hidden' }} />
1215
<Handle type="source" position={Position.Bottom} style={{ visibility: 'hidden' }} />
@@ -22,7 +25,15 @@ const CustomNode: React.FC<CustomNodeProps> = ({ data }) => (
2225
</div>
2326
</div>
2427
<div className={styles.yamlButtonWrapper}>
25-
<YamlViewButton resourceObject={data.item} smallerIcon={true} />
28+
<Button
29+
className={styles.smallerIconButton}
30+
design="Transparent"
31+
aria-label="YAML"
32+
title="YAML"
33+
onClick={() => onYamlClick(data.item)}
34+
>
35+
<Icon name="document" style={{ color: 'rgb(0, 100, 217)' }} />
36+
</Button>
2637
</div>
2738
</div>
2839
);

src/components/Graphs/Graph.tsx

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
useEdgesState,
1010
MarkerType,
1111
Position,
12+
NodeProps,
1213
} from 'reactflow';
1314
import styles from './Graph.module.css';
1415
import dagre from 'dagre';
@@ -21,14 +22,14 @@ import { NodeData, ManagedResourceGroup, ManagedResourceItem } from './types';
2122
import CustomNode from './CustomNode';
2223
import { Legend } from './Legend';
2324
import { extractRefs, generateColorMap, getStatusFromConditions, resolveProviderType } from './graphUtils';
25+
import { YamlViewDialog } from '../Yaml/YamlViewDialog';
26+
import YamlViewer from '../Yaml/YamlViewer';
27+
import { stringify } from 'yaml';
28+
import { removeManagedFieldsProperty } from '../../utils/removeManagedFieldsProperty';
2429

2530
const nodeWidth = 250;
2631
const nodeHeight = 60;
2732

28-
const nodeTypes = {
29-
custom: CustomNode,
30-
};
31-
3233
function buildGraph(
3334
treeData: NodeData[],
3435
colorBy: 'provider' | 'source',
@@ -106,6 +107,28 @@ const Graph: React.FC = () => {
106107
const [edges, setEdges] = useEdgesState<Edge[]>([]);
107108
const [colorBy, setColorBy] = useState<'provider' | 'source'>('provider');
108109

110+
const [yamlDialogOpen, setYamlDialogOpen] = useState(false);
111+
const [yamlResource, setYamlResource] = useState<ManagedResourceItem | null>(null);
112+
113+
const handleYamlClick = (item: ManagedResourceItem) => {
114+
setYamlResource(item);
115+
setYamlDialogOpen(true);
116+
};
117+
118+
const nodeTypes = {
119+
custom: (props: NodeProps<NodeData>) => <CustomNode {...props} onYamlClick={handleYamlClick} />,
120+
};
121+
122+
const yamlString = useMemo(
123+
() => (yamlResource ? stringify(removeManagedFieldsProperty(yamlResource)) : ''),
124+
[yamlResource],
125+
);
126+
const yamlFilename = useMemo(() => {
127+
if (!yamlResource) return '';
128+
const { kind, metadata } = yamlResource;
129+
return `${kind ?? ''}${metadata?.name ? '_' : ''}${metadata?.name ?? ''}`;
130+
}, [yamlResource]);
131+
109132
const treeData = useMemo(() => {
110133
const allNodesMap = new Map<string, NodeData>();
111134
if (managedResources) {
@@ -249,13 +272,20 @@ const Graph: React.FC = () => {
249272
nodesDraggable={false}
250273
nodesConnectable={false}
251274
elementsSelectable={false}
252-
zoomOnScroll={false}
275+
zoomOnScroll={true}
253276
panOnDrag={true}
254277
>
255278
<Controls />
256279
<Background />
257280
</ReactFlow>
258281
</div>
282+
{yamlDialogOpen && yamlResource && (
283+
<YamlViewDialog
284+
isOpen={yamlDialogOpen}
285+
setIsOpen={setYamlDialogOpen}
286+
dialogContent={<YamlViewer yamlString={yamlString} filename={yamlFilename} />}
287+
/>
288+
)}
259289
<Legend nodes={nodes.map((n) => n.data as NodeData)} colorBy={colorBy} generateColorMap={generateColorMap} />
260290
</div>
261291
);

src/components/Yaml/YamlViewButton.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Button, Icon } from '@ui5/webcomponents-react';
1+
import { Button } from '@ui5/webcomponents-react';
22
import { FC, useMemo, useState } from 'react';
33
import styles from './YamlViewer.module.css';
44
import { useTranslation } from 'react-i18next';
@@ -10,10 +10,9 @@ import { YamlViewDialog } from './YamlViewDialog.tsx';
1010

1111
export type YamlViewButtonProps = {
1212
resourceObject: unknown;
13-
smallerIcon?: boolean;
1413
};
1514

16-
export const YamlViewButton: FC<YamlViewButtonProps> = ({ resourceObject, smallerIcon }) => {
15+
export const YamlViewButton: FC<YamlViewButtonProps> = ({ resourceObject }) => {
1716
const [isOpen, setIsOpen] = useState(false);
1817
const { t } = useTranslation();
1918
const resource = resourceObject as Resource;
@@ -34,15 +33,15 @@ export const YamlViewButton: FC<YamlViewButtonProps> = ({ resourceObject, smalle
3433
/>
3534

3635
<Button
37-
className={smallerIcon ? styles.smallerIconButton : styles.button}
36+
className={styles.button}
3837
design={'Transparent'}
3938
aria-label={t('buttons.viewResource')}
4039
title={t('buttons.viewResource')}
4140
onClick={() => {
4241
setIsOpen(true);
4342
}}
4443
>
45-
{smallerIcon ? <Icon name="document" className={styles.smallIcon} /> : <YamlIcon />}
44+
<YamlIcon />
4645
</Button>
4746
</span>
4847
);

0 commit comments

Comments
 (0)