This is an automated email from the ASF dual-hosted git repository.
bbovenzi pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 5294276b48e Add ability to select a dag from dags list (#43324)
5294276b48e is described below
commit 5294276b48e49ab37ba9261f4e17c28be85e633b
Author: Brent Bovenzi <[email protected]>
AuthorDate: Fri Oct 25 14:41:36 2024 -0400
Add ability to select a dag from dags list (#43324)
Fix event propagation and onRowClicked type
Simplify error page
Remove div links and only show error stack trace in dev
useLocalStorage hook instead of manualy manipulation
Remove deserializer
---
airflow/ui/package.json | 3 +-
airflow/ui/pnpm-lock.yaml | 19 ++++
airflow/ui/src/App.test.tsx | 124 ---------------------
.../ui/src/context/timezone/TimezoneProvider.tsx | 26 ++---
airflow/ui/src/layouts/BaseLayout.tsx | 5 +-
airflow/ui/src/main.tsx | 21 ++--
.../ui/src/{App.tsx => pages/DagsList/Dag/Dag.tsx} | 19 +---
.../DagsList/Dag/index.tsx} | 13 +--
airflow/ui/src/pages/DagsList/DagCard.tsx | 16 ++-
airflow/ui/src/pages/DagsList/DagsList.tsx | 22 +++-
airflow/ui/src/pages/Error.tsx | 99 ++++++++++++++++
airflow/ui/src/{App.tsx => router.tsx} | 37 ++++--
airflow/ui/src/theme.ts | 5 -
13 files changed, 206 insertions(+), 203 deletions(-)
diff --git a/airflow/ui/package.json b/airflow/ui/package.json
index 3ca8d1a06f4..97f3a2ee63e 100644
--- a/airflow/ui/package.json
+++ b/airflow/ui/package.json
@@ -30,7 +30,8 @@
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-router-dom": "^6.26.2",
- "use-debounce": "^10.0.3"
+ "use-debounce": "^10.0.3",
+ "usehooks-ts": "^3.1.0"
},
"devDependencies": {
"@7nohe/openapi-react-query-codegen": "^1.6.0",
diff --git a/airflow/ui/pnpm-lock.yaml b/airflow/ui/pnpm-lock.yaml
index 3ceee513bb1..45858f664a6 100644
--- a/airflow/ui/pnpm-lock.yaml
+++ b/airflow/ui/pnpm-lock.yaml
@@ -53,6 +53,9 @@ importers:
use-debounce:
specifier: ^10.0.3
version: 10.0.3([email protected])
+ usehooks-ts:
+ specifier: ^3.1.0
+ version: 3.1.0([email protected])
devDependencies:
'@7nohe/openapi-react-query-codegen':
specifier: ^1.6.0
@@ -2464,6 +2467,9 @@ packages:
resolution: {integrity:
sha512-iPZK6eYjbxRu3uB4/WZ3EsEIMJFMqAoopl3R+zuq0UjcAm/MO6KCweDgPfP3elTztoKP3KtnVHxTn2NHBSDVUw==}
engines: {node: '>=10'}
+ [email protected]:
+ resolution: {integrity:
sha512-FT1yDzDYEoYWhnSGnpE/4Kj1fLZkDFyqRb7fNt6FdYOSxlUWAtp42Eh6Wb0rGIv/m9Bgo7x4GhQbm5Ys4SG5ow==}
+
[email protected]:
resolution: {integrity:
sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==}
@@ -3255,6 +3261,12 @@ packages:
'@types/react':
optional: true
+ [email protected]:
+ resolution: {integrity:
sha512-bBIa7yUyPhE1BCc0GmR96VU/15l/9gP1Ch5mYdLcFBaFGQsdmXkvjV0TtOqW1yUd6VjIwDunm+flSciCQXujiw==}
+ engines: {node: '>=16.15.0'}
+ peerDependencies:
+ react: ^16.8.0 || ^17 || ^18
+
[email protected]:
resolution: {integrity:
sha512-DpKm2Ui/xN7/HQKCtpZxoRWBhZ9Z0kqtygG8XCgNQ8ZlDnxuQmWhj566j8fN4Cu3/JmbhsDo7fcAJq4s9h27Ew==}
@@ -6113,6 +6125,8 @@ snapshots:
dependencies:
p-locate: 5.0.0
+ [email protected]: {}
+
[email protected]: {}
[email protected]: {}
@@ -6927,6 +6941,11 @@ snapshots:
optionalDependencies:
'@types/react': 18.3.5
+ [email protected]([email protected]):
+ dependencies:
+ lodash.debounce: 4.0.8
+ react: 18.3.1
+
[email protected]:
dependencies:
spdx-correct: 3.2.0
diff --git a/airflow/ui/src/App.test.tsx b/airflow/ui/src/App.test.tsx
deleted file mode 100644
index 38b90d1c498..00000000000
--- a/airflow/ui/src/App.test.tsx
+++ /dev/null
@@ -1,124 +0,0 @@
-/*!
- * 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 type { QueryObserverSuccessResult } from "@tanstack/react-query";
-import { render } from "@testing-library/react";
-import { afterEach, beforeEach, describe, it, vi } from "vitest";
-
-import * as openapiQueriesModule from "openapi/queries";
-import type { DAGCollectionResponse } from "openapi/requests/types.gen";
-
-import { App } from "./App";
-import { Wrapper } from "./utils/Wrapper";
-
-// The null fields actually have to be null instead of undefined
-/* eslint-disable unicorn/no-null */
-
-const mockListDags: DAGCollectionResponse = {
- dags: [
- {
- dag_display_name: "nested_groups",
- dag_id: "nested_groups",
- default_view: "grid",
- description: null,
- file_token:
-
"Ii9maWxlcy9kYWdzL25lc3RlZF90YXNrX2dyb3Vwcy5weSI.G3EkdxmDUDQsVb7AIZww1TSGlFE",
- fileloc: "/files/dags/nested_task_groups.py",
- has_import_errors: false,
- has_task_concurrency_limits: false,
- is_active: true,
- is_paused: false,
- last_expired: null,
- last_parsed_time: "2024-08-22T13:50:10.372238+00:00",
- last_pickled: null,
- max_active_runs: 16,
- max_active_tasks: 16,
- max_consecutive_failed_dag_runs: 0,
- next_dagrun: "2024-08-22T00:00:00+00:00",
- next_dagrun_create_after: "2024-08-23T00:00:00+00:00",
- next_dagrun_data_interval_end: "2024-08-23T00:00:00+00:00",
- next_dagrun_data_interval_start: "2024-08-22T00:00:00+00:00",
- owners: ["airflow"],
- pickle_id: null,
- scheduler_lock: null,
- tags: [],
- timetable_description: "",
- timetable_summary: "",
- },
- {
- dag_display_name: "simple_bash_operator",
- dag_id: "simple_bash_operator",
- default_view: "grid",
- description: null,
- file_token:
-
"Ii9maWxlcy9kYWdzL3NpbXBsZV9iYXNoX29wZXJhdG9yLnB5Ig.RteaxTC78ceHlgMkfU3lfznlcLI",
- fileloc: "/files/dags/simple_bash_operator.py",
- has_import_errors: false,
- has_task_concurrency_limits: false,
- is_active: true,
- is_paused: false,
- last_expired: null,
- last_parsed_time: "2024-08-22T13:50:10.368561+00:00",
- last_pickled: null,
- max_active_runs: 16,
- max_active_tasks: 16,
- max_consecutive_failed_dag_runs: 0,
- next_dagrun: "2024-08-22T00:00:00+00:00",
- next_dagrun_create_after: "2024-08-23T00:00:00+00:00",
- next_dagrun_data_interval_end: "2024-08-23T00:00:00+00:00",
- next_dagrun_data_interval_start: "2024-08-22T00:00:00+00:00",
- owners: ["airflow"],
- pickle_id: null,
- scheduler_lock: null,
- tags: [
- {
- dag_id: "dag",
- name: "example2",
- },
- {
- dag_id: "dag",
- name: "example",
- },
- ],
- timetable_description: "At 00:00",
- timetable_summary: "sum",
- },
- ],
- total_entries: 2,
-};
-
-beforeEach(() => {
- const returnValue = {
- data: mockListDags,
- isLoading: false,
- } as QueryObserverSuccessResult<DAGCollectionResponse, unknown>;
-
- vi.spyOn(openapiQueriesModule, "useDagServiceGetDags").mockImplementation(
- () => returnValue,
- );
-});
-
-afterEach(() => {
- vi.restoreAllMocks();
-});
-
-describe("App", () => {
- it("App component should render", () => {
- render(<App />, { wrapper: Wrapper });
- });
-});
diff --git a/airflow/ui/src/context/timezone/TimezoneProvider.tsx
b/airflow/ui/src/context/timezone/TimezoneProvider.tsx
index dfe40f69767..74c0c2462b3 100644
--- a/airflow/ui/src/context/timezone/TimezoneProvider.tsx
+++ b/airflow/ui/src/context/timezone/TimezoneProvider.tsx
@@ -16,12 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
-import {
- createContext,
- useState,
- useMemo,
- type PropsWithChildren,
-} from "react";
+import { createContext, useMemo, type PropsWithChildren } from "react";
+import { useLocalStorage } from "usehooks-ts";
export type TimezoneContextType = {
selectedTimezone: string;
@@ -35,20 +31,14 @@ export const TimezoneContext =
createContext<TimezoneContextType | undefined>(
const TIMEZONE_KEY = "timezone";
export const TimezoneProvider = ({ children }: PropsWithChildren) => {
- const [selectedTimezone, setSelectedTimezone] = useState(() => {
- const timezone = localStorage.getItem(TIMEZONE_KEY);
-
- return timezone ?? "UTC";
- });
-
- const selectTimezone = (tz: string) => {
- localStorage.setItem(TIMEZONE_KEY, tz);
- setSelectedTimezone(tz);
- };
+ const [selectedTimezone, setSelectedTimezone] = useLocalStorage(
+ TIMEZONE_KEY,
+ "UTC",
+ );
const value = useMemo<TimezoneContextType>(
- () => ({ selectedTimezone, setSelectedTimezone: selectTimezone }),
- [selectedTimezone],
+ () => ({ selectedTimezone, setSelectedTimezone }),
+ [selectedTimezone, setSelectedTimezone],
);
return (
diff --git a/airflow/ui/src/layouts/BaseLayout.tsx
b/airflow/ui/src/layouts/BaseLayout.tsx
index 4aa7a74de6f..c0d1b6c869b 100644
--- a/airflow/ui/src/layouts/BaseLayout.tsx
+++ b/airflow/ui/src/layouts/BaseLayout.tsx
@@ -17,15 +17,16 @@
* under the License.
*/
import { Box } from "@chakra-ui/react";
+import type { PropsWithChildren } from "react";
import { Outlet } from "react-router-dom";
import { Nav } from "./Nav";
-export const BaseLayout = () => (
+export const BaseLayout = ({ children }: PropsWithChildren) => (
<>
<Nav />
<Box ml={24} p={3}>
- <Outlet />
+ {children ?? <Outlet />}
</Box>
</>
);
diff --git a/airflow/ui/src/main.tsx b/airflow/ui/src/main.tsx
index dda70beccd3..ba9b5e84995 100644
--- a/airflow/ui/src/main.tsx
+++ b/airflow/ui/src/main.tsx
@@ -20,11 +20,10 @@ import { ChakraProvider } from "@chakra-ui/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import axios, { type AxiosError } from "axios";
import { createRoot } from "react-dom/client";
-import { BrowserRouter } from "react-router-dom";
-
-import { App } from "src/App";
+import { RouterProvider } from "react-router-dom";
import { TimezoneProvider } from "./context/timezone";
+import { router } from "./router";
import theme from "./theme";
const queryClient = new QueryClient({
@@ -62,13 +61,11 @@ axios.interceptors.response.use(
const root = createRoot(document.querySelector("#root") as HTMLDivElement);
root.render(
- <BrowserRouter basename="/webapp">
- <ChakraProvider theme={theme}>
- <QueryClientProvider client={queryClient}>
- <TimezoneProvider>
- <App />
- </TimezoneProvider>
- </QueryClientProvider>
- </ChakraProvider>
- </BrowserRouter>,
+ <ChakraProvider theme={theme}>
+ <QueryClientProvider client={queryClient}>
+ <TimezoneProvider>
+ <RouterProvider router={router} />
+ </TimezoneProvider>
+ </QueryClientProvider>
+ </ChakraProvider>,
);
diff --git a/airflow/ui/src/App.tsx b/airflow/ui/src/pages/DagsList/Dag/Dag.tsx
similarity index 66%
copy from airflow/ui/src/App.tsx
copy to airflow/ui/src/pages/DagsList/Dag/Dag.tsx
index b8c73c35c7f..79290803e70 100644
--- a/airflow/ui/src/App.tsx
+++ b/airflow/ui/src/pages/DagsList/Dag/Dag.tsx
@@ -16,18 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/
-import { Route, Routes } from "react-router-dom";
+import { Box } from "@chakra-ui/react";
+import { useParams } from "react-router-dom";
-import { DagsList } from "src/pages/DagsList";
-import { Dashboard } from "src/pages/Dashboard";
+export const Dag = () => {
+ const params = useParams();
-import { BaseLayout } from "./layouts/BaseLayout";
-
-export const App = () => (
- <Routes>
- <Route element={<BaseLayout />} path="/">
- <Route element={<Dashboard />} index />
- <Route element={<DagsList />} path="dags" />
- </Route>
- </Routes>
-);
+ return <Box>{params.dagId}</Box>;
+};
diff --git a/airflow/ui/src/layouts/BaseLayout.tsx
b/airflow/ui/src/pages/DagsList/Dag/index.tsx
similarity index 78%
copy from airflow/ui/src/layouts/BaseLayout.tsx
copy to airflow/ui/src/pages/DagsList/Dag/index.tsx
index 4aa7a74de6f..9a386b3fb38 100644
--- a/airflow/ui/src/layouts/BaseLayout.tsx
+++ b/airflow/ui/src/pages/DagsList/Dag/index.tsx
@@ -16,16 +16,5 @@
* specific language governing permissions and limitations
* under the License.
*/
-import { Box } from "@chakra-ui/react";
-import { Outlet } from "react-router-dom";
-import { Nav } from "./Nav";
-
-export const BaseLayout = () => (
- <>
- <Nav />
- <Box ml={24} p={3}>
- <Outlet />
- </Box>
- </>
-);
+export { Dag } from "./Dag";
diff --git a/airflow/ui/src/pages/DagsList/DagCard.tsx
b/airflow/ui/src/pages/DagsList/DagCard.tsx
index da5531fedb2..c222ac67897 100644
--- a/airflow/ui/src/pages/DagsList/DagCard.tsx
+++ b/airflow/ui/src/pages/DagsList/DagCard.tsx
@@ -26,8 +26,10 @@ import {
Text,
Tooltip,
VStack,
+ Link,
} from "@chakra-ui/react";
import { FiCalendar, FiTag } from "react-icons/fi";
+import { Link as RouterLink } from "react-router-dom";
import type { DAGResponse } from "openapi/requests/types.gen";
import Time from "src/components/Time";
@@ -55,9 +57,15 @@ export const DagCard = ({ dag }: Props) => (
>
<HStack>
<Tooltip hasArrow label={dag.description}>
- <Heading color="blue.contrast" fontSize="md">
+ <Link
+ as={RouterLink}
+ color="blue.contrast"
+ fontSize="md"
+ fontWeight="bold"
+ to={`/dags/${dag.dag_id}`}
+ >
{dag.dag_display_name}
- </Heading>
+ </Link>
</Tooltip>
{dag.tags.length ? (
<HStack spacing={1}>
@@ -82,9 +90,7 @@ export const DagCard = ({ dag }: Props) => (
</HStack>
) : undefined}
</HStack>
- <HStack>
- <TogglePause dagId={dag.dag_id} isPaused={dag.is_paused} />
- </HStack>
+ <TogglePause dagId={dag.dag_id} isPaused={dag.is_paused} />
</Flex>
<SimpleGrid columns={4} height={20} px={3} py={2} spacing={4}>
<div />
diff --git a/airflow/ui/src/pages/DagsList/DagsList.tsx
b/airflow/ui/src/pages/DagsList/DagsList.tsx
index 7ae6df64ae3..cbc09b87c1a 100644
--- a/airflow/ui/src/pages/DagsList/DagsList.tsx
+++ b/airflow/ui/src/pages/DagsList/DagsList.tsx
@@ -23,6 +23,7 @@ import {
Select,
Skeleton,
VStack,
+ Link,
} from "@chakra-ui/react";
import type { ColumnDef } from "@tanstack/react-table";
import {
@@ -31,7 +32,8 @@ import {
useCallback,
useState,
} from "react";
-import { useSearchParams } from "react-router-dom";
+import { Link as RouterLink, useSearchParams } from "react-router-dom";
+import { useLocalStorage } from "usehooks-ts";
import { useDagServiceGetDags } from "openapi/queries";
import type { DAGResponse, DagRunState } from "openapi/requests/types.gen";
@@ -69,7 +71,16 @@ const columns: Array<ColumnDef<DAGResponse>> = [
},
{
accessorKey: "dag_id",
- cell: ({ row }) => row.original.dag_display_name,
+ cell: ({ row }) => (
+ <Link
+ as={RouterLink}
+ color="blue.contrast"
+ fontWeight="bold"
+ to={`/dags/${row.original.dag_id}`}
+ >
+ {row.original.dag_display_name}
+ </Link>
+ ),
header: "Dag",
},
{
@@ -118,9 +129,14 @@ const cardDef: CardDef<DAGResponse> = {
},
};
+const DAGS_LIST_DISPLAY = "dags_list_display";
+
export const DagsList = () => {
const [searchParams, setSearchParams] = useSearchParams();
- const [display, setDisplay] = useState<"card" | "table">("card");
+ const [display, setDisplay] = useLocalStorage<"card" | "table">(
+ DAGS_LIST_DISPLAY,
+ "card",
+ );
const showPaused = searchParams.get(PAUSED_PARAM);
const lastDagRunState = searchParams.get(
diff --git a/airflow/ui/src/pages/Error.tsx b/airflow/ui/src/pages/Error.tsx
new file mode 100644
index 00000000000..bc9604fbc90
--- /dev/null
+++ b/airflow/ui/src/pages/Error.tsx
@@ -0,0 +1,99 @@
+/*!
+ * 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 {
+ Box,
+ VStack,
+ Heading,
+ Text,
+ Button,
+ Container,
+ HStack,
+ Code,
+ Icon,
+} from "@chakra-ui/react";
+import {
+ useNavigate,
+ useRouteError,
+ isRouteErrorResponse,
+} from "react-router-dom";
+
+import { AirflowPin } from "src/assets/AirflowPin";
+
+export const ErrorPage = () => {
+ const navigate = useNavigate();
+ const error = useRouteError();
+
+ let errorMessage = "An unexpected error occurred";
+ let statusCode = "";
+
+ if (isRouteErrorResponse(error)) {
+ statusCode = String(error.status);
+ errorMessage =
+ ((error as unknown as Error).message ||
+ (error as { statusText?: string }).statusText) ??
+ "Page Not Found";
+ } else if (error instanceof Error) {
+ errorMessage = error.message;
+ } else if (typeof error === "string") {
+ errorMessage = error;
+ }
+
+ // This is vite's dev mode. We should switch to Airflow's dev mode as part
of a UI config endpoint
+ const isDev = import.meta.env.DEV;
+
+ return (
+ <Box
+ alignItems="center"
+ display="flex"
+ justifyContent="center"
+ pt={36}
+ px={4}
+ >
+ <Container maxW="lg">
+ <VStack spacing={8} textAlign="center">
+ <Icon as={AirflowPin} height="50px" width="50px" />
+
+ <VStack spacing={4}>
+ <Heading>{statusCode || "Error"}</Heading>
+ <Text fontSize="lg">{errorMessage}</Text>
+ {error instanceof Error && isDev ? (
+ <Code borderRadius="md" fontSize="sm" p={3} width="full">
+ {error.stack}
+ </Code>
+ ) : undefined}
+ </VStack>
+
+ <HStack spacing={4}>
+ <Button colorScheme="blue" onClick={() => navigate(-1)} size="lg">
+ Back
+ </Button>
+ <Button
+ colorScheme="blue"
+ onClick={() => navigate("/")}
+ size="lg"
+ variant="outline"
+ >
+ Home
+ </Button>
+ </HStack>
+ </VStack>
+ </Container>
+ </Box>
+ );
+};
diff --git a/airflow/ui/src/App.tsx b/airflow/ui/src/router.tsx
similarity index 60%
rename from airflow/ui/src/App.tsx
rename to airflow/ui/src/router.tsx
index b8c73c35c7f..30f5ab9e93d 100644
--- a/airflow/ui/src/App.tsx
+++ b/airflow/ui/src/router.tsx
@@ -16,18 +16,39 @@
* specific language governing permissions and limitations
* under the License.
*/
-import { Route, Routes } from "react-router-dom";
+import { createBrowserRouter } from "react-router-dom";
import { DagsList } from "src/pages/DagsList";
import { Dashboard } from "src/pages/Dashboard";
import { BaseLayout } from "./layouts/BaseLayout";
+import { Dag } from "./pages/DagsList/Dag";
+import { ErrorPage } from "./pages/Error";
-export const App = () => (
- <Routes>
- <Route element={<BaseLayout />} path="/">
- <Route element={<Dashboard />} index />
- <Route element={<DagsList />} path="dags" />
- </Route>
- </Routes>
+export const router = createBrowserRouter(
+ [
+ {
+ children: [
+ {
+ element: <Dashboard />,
+ index: true,
+ },
+ {
+ element: <DagsList />,
+ path: "dags",
+ },
+ { element: <Dag />, path: "dags/:dagId" },
+ ],
+ element: <BaseLayout />,
+ errorElement: (
+ <BaseLayout>
+ <ErrorPage />
+ </BaseLayout>
+ ),
+ path: "/",
+ },
+ ],
+ {
+ basename: "/webapp",
+ },
);
diff --git a/airflow/ui/src/theme.ts b/airflow/ui/src/theme.ts
index 7809e400794..08e300a9bb4 100644
--- a/airflow/ui/src/theme.ts
+++ b/airflow/ui/src/theme.ts
@@ -60,11 +60,6 @@ const theme = extendTheme({
},
components: {
Table: tableTheme,
- Tooltip: {
- baseStyle: {
- fontSize: "md",
- },
- },
},
config: {
initialColorMode: "system",