pierrejeambrun commented on code in PR #29413:
URL: https://github.com/apache/airflow/pull/29413#discussion_r1103667180


##########
airflow/www/static/js/dag/details/graph/index.tsx:
##########
@@ -0,0 +1,260 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import React, {
+  useRef, useMemo, useState, useEffect,
+} from 'react';
+import Color from 'color';
+import {
+  Box,
+  useTheme,
+  Select,
+  Text,
+} from '@chakra-ui/react';
+import ReactFlow, {
+  ReactFlowProvider,
+  Controls,
+  Background,
+  MiniMap,
+  Node as ReactFlowNode,
+  useReactFlow,
+  ControlButton,
+  Panel,
+} from 'reactflow';
+import { MdCenterFocusStrong } from 'react-icons/md';
+
+import { useGraphData, useGridData } from 'src/api';
+import useOffsetHeight from 'src/utils/useOffsetHeight';
+import useSelection from 'src/dag/useSelection';
+import { getTask } from 'src/utils';
+import type { TaskInstance } from 'src/types';
+import { useGraphLayout } from 'src/utils/graph';
+import type { NodeType } from 'src/datasets/Graph/Node';
+
+import Edge from './Edge';
+import Node, { CustomNodeProps } from './Node';
+
+interface Props {
+  openGroupIds: string[];
+  onToggleGroups: (groupIds: string[]) => void;
+}
+
+const Graph = ({ openGroupIds, onToggleGroups }: Props) => {
+  const graphRef = useRef(null);
+  const { data } = useGraphData();
+  const [arrange, setArrange] = useState(data?.arrange || 'LR');
+
+  useEffect(() => {
+    setArrange(data?.arrange || 'LR');
+  }, [data?.arrange]);
+
+  const { data: graphData } = useGraphLayout({
+    edges: data?.edges, nodes: data?.nodes, openGroupIds, arrange,
+  });
+  const { selected: { runId, taskId } } = useSelection();
+  const { data: { dagRuns, groups } } = useGridData();
+  const { colors } = useTheme();
+  const { setCenter } = useReactFlow();
+  const latestDagRunId = dagRuns[dagRuns.length - 1]?.runId;
+
+  const offsetHeight = useOffsetHeight(graphRef, undefined, 750);
+
+  const nodes: ReactFlowNode<CustomNodeProps>[] = [];
+  const edges = (graphData?.edges || []).map((edge) => ({
+    id: edge.id,
+    source: edge.sources[0],
+    target: edge.targets[0],
+    data: { rest: edge },
+    type: 'custom',
+  }));
+
+  const flattenNodes = (children: NodeType[], parent?: string) => {
+    const parentNode = parent ? { parentNode: parent } : undefined;
+    children.forEach((node) => {
+      let instance: TaskInstance | undefined;
+      const group = getTask({ taskId: node.id, task: groups });
+      if (!node.id.includes('join_id') && runId) {
+        instance = group?.instances.find((ti) => ti.runId === runId);
+      }
+      const isSelected = node.id === taskId && !!instance;
+
+      nodes.push({
+        id: node.id,
+        data: {
+          width: node.width,
+          height: node.height,
+          task: group,
+          instance,
+          isSelected,
+          latestDagRunId,
+          onToggleCollapse: () => {
+            let newGroupIds = [];
+            if (!node.value.isOpen) {
+              newGroupIds = [...openGroupIds, node.value.label];
+            } else {
+              newGroupIds = openGroupIds.filter((g) => g !== node.value.label);
+            }
+            onToggleGroups(newGroupIds);
+          },
+          ...node.value,
+        },
+        type: 'custom',
+        position: {
+          x: node.x || 0,
+          y: node.y || 0,
+        },
+        ...parentNode,
+      });
+      if (node.children) {
+        flattenNodes(node.children, node.id);
+      }
+    });
+  };
+
+  if (graphData?.children) {
+    flattenNodes(graphData.children);
+  }
+
+  const focusNode = () => {
+    const selectedNode = nodes.find((n) => n.id === taskId);
+    if (selectedNode) {
+      setCenter(selectedNode.position.x, selectedNode.position.y, { duration: 
1000 });
+    }
+  };
+
+  const nodeTypes = useMemo(() => ({ custom: Node }), []);
+  const edgeTypes = useMemo(() => ({ custom: Edge }), []);
+
+  const newEdges = edges.map((e) => {

Review Comment:
   I think we can extract this outside the component, with a function, maybe 
like `buildGraphEdges(gridDataEdges, taskId,)`. This will make the component a 
bit smaller and easier to maintain. Same for nodeColor and nodeStroke, also 
avoid redefining these functions on each render.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to