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


##########
airflow-core/src/airflow/ui/src/hooks/navigation/useNavigation.ts:
##########
@@ -0,0 +1,201 @@
+/*!
+ * 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 { useCallback, useEffect, useMemo, useState } from "react";
+import { useNavigate, useParams } from "react-router-dom";
+
+import type { GridRunsResponse } from "openapi/requests";
+import type { GridTask } from "src/layouts/Details/Grid/utils";
+
+import type {
+  NavigationDirection,
+  NavigationIndices,
+  NavigationMode,
+  UseNavigationProps,
+  UseNavigationReturn,
+} from "./types";
+import { useKeyboardNavigation } from "./useKeyboardNavigation";
+
+const detectModeFromUrl = (pathname: string): NavigationMode => {
+  if (pathname.includes("/runs/") && pathname.includes("/tasks/")) {
+    return "TI";
+  }
+  if (pathname.includes("/runs/") && !pathname.includes("/tasks/")) {
+    return "run";
+  }
+  if (pathname.includes("/tasks/") && !pathname.includes("/runs/")) {
+    return "task";
+  }
+
+  return "TI";
+};
+
+const isValidDirection = (direction: NavigationDirection, mode: 
NavigationMode): boolean => {
+  switch (mode) {
+    case "run":
+      return direction === "left" || direction === "right";
+    case "task":
+      return direction === "down" || direction === "up";
+    case "TI":
+      return true;
+    default:
+      return false;
+  }
+};
+
+const getNextIndex = (
+  current: number,
+  direction: number,
+  options: { isJump: boolean; max: number },
+): number => {
+  if (options.isJump) {
+    return direction > 0 ? options.max - 1 : 0;
+  }
+
+  return Math.max(0, Math.min(options.max - 1, current + direction));
+};
+
+const buildPath = (params: {
+  dagId: string;
+  mode: NavigationMode;
+  run: GridRunsResponse;
+  task: GridTask;
+}): string => {
+  const { dagId, mode, run, task } = params;
+  const groupPath = task.isGroup ? "group/" : "";
+
+  switch (mode) {
+    case "run":
+      return `/dags/${dagId}/runs/${run.run_id}`;
+    case "task":
+      return `/dags/${dagId}/tasks/${groupPath}${task.id}`;
+    case "TI":
+      return `/dags/${dagId}/runs/${run.run_id}/tasks/${groupPath}${task.id}`;
+    default:
+      return `/dags/${dagId}`;
+  }

Review Comment:
   This is not handling mapped TI, and mapped Tasg groups. For TIs there is 
already a utility in the code `getTaskInstanceLinkFromObj` that will help you 
achieve that.



##########
airflow-core/src/airflow/ui/src/layouts/Details/Grid/Grid.tsx:
##########
@@ -84,12 +101,60 @@ export const Grid = ({ limit }: Props) => {
           .map((dr: GridRunsResponse) => dr.duration)
           .filter((duration: number | null): duration is number => duration 
!== null),
   );
+
   const { flatNodes } = useMemo(() => flattenNodes(dagStructure, 
openGroupIds), [dagStructure, openGroupIds]);
 
+  const setGridFocus = useCallback((focused: boolean) => {
+    setIsGridFocused(focused);
+    if (focused) {
+      gridRef.current?.focus();
+    } else {
+      gridRef.current?.blur();
+    }
+  }, []);
+
+  const { mode, setMode } = useNavigation({
+    enabled: isGridFocused,
+    onEscapePress: () => setGridFocus(false),
+    onToggleGroup: toggleGroupId,
+    runs: gridRuns ?? [],
+    tasks: flatNodes,
+  });
+
+  useEffect(() => {
+    if (gridRef.current && gridRuns && flatNodes.length > 0) {
+      setGridFocus(true);
+    }
+  }, [gridRuns, flatNodes.length, setGridFocus]);
+
   return (
-    <Flex justifyContent="flex-start" position="relative" pt={50} width="100%">
+    <Flex
+      _focus={{
+        borderRadius: "4px",
+        boxShadow: "0 0 0 2px rgba(59, 130, 246, 0.5)",
+      }}
+      cursor="pointer"
+      justifyContent="flex-start"
+      onBlur={() => setGridFocus(false)}
+      onFocus={() => setGridFocus(true)}
+      onMouseDown={() => setGridFocus(true)}
+      outline="none"
+      position="relative"
+      pt={50}
+      ref={gridRef}
+      tabIndex={0}
+      width="100%"
+    >
+      {Boolean(isGridFocused) && (
+        <Box borderRadius="md" color="gray.400" fontSize="xs" 
position="absolute" px={0} py={12} top={0}>
+          <Text>{translate("navigation.navigation", { arrow: 
getArrowsForMode(mode) })}</Text>
+          <Text>{translate("navigation.jump", { arrow: getArrowsForMode(mode) 
})}</Text>
+          <Text>{translate("navigation.toggleGroup")}</Text>
+        </Box>
+      )}
+

Review Comment:
   Always show this. The flickering between renders and focus/unfocus does not 
look good.



-- 
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: commits-unsubscr...@airflow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to