bbovenzi commented on code in PR #70470:
URL: https://github.com/apache/airflow/pull/70470#discussion_r4136901837


##########
airflow-core/src/airflow/ui/src/components/FilterBar/filters/runStateParams.ts:
##########
@@ -0,0 +1,71 @@
+/*!
+ * 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 { SearchParamsKeys } from "src/constants/searchParams";
+
+import type { FilterValue } from "../types";
+
+// "latest" matches on the latest run only; numeric values are hours; "any" 
has no time bound.
+export type RunStateLookback = "168" | "24" | "720" | "any" | "latest";
+
+export const TIME_LOOKBACKS: ReadonlyArray<RunStateLookback> = ["24", "168", 
"720"];
+export const RUN_STATE_LOOKBACKS: ReadonlyArray<RunStateLookback> = ["latest", 
...TIME_LOOKBACKS, "any"];
+
+export type RunStateValue = {
+  lookback: RunStateLookback;
+  state: string;
+};
+
+export const isRunStateValue = (value: FilterValue): value is RunStateValue =>
+  typeof value === "object" && value !== null && !Array.isArray(value) && 
"state" in value;
+
+// The unified Run state pill is backed by the existing independent URL params:
+//   "latest" lookback → last_dag_run_state (match the latest run only)
+//   a time lookback   → dag_run_state (match any run) + a within-hours bound
+//   "any"             → dag_run_state with no time bound
+export const runStateToSearchParams = (value: FilterValue): Record<string, 
string | undefined> => {
+  const selection = isRunStateValue(value) ? value : undefined;
+  const isLatest = selection?.lookback === "latest";
+  const isTimeBounded = selection !== undefined && 
TIME_LOOKBACKS.includes(selection.lookback);
+
+  return {
+    [SearchParamsKeys.DAG_RUN_STATE]: selection !== undefined && !isLatest ? 
selection.state : undefined,
+    [SearchParamsKeys.DAG_RUN_STATE_WITHIN_HOURS]: isTimeBounded ? 
selection.lookback : undefined,
+    [SearchParamsKeys.LAST_DAG_RUN_STATE]: isLatest ? selection.state : 
undefined,
+  };
+};
+
+export const runStateFromSearchParams = (params: URLSearchParams): 
RunStateValue | undefined => {
+  const lastRunState = params.get(SearchParamsKeys.LAST_DAG_RUN_STATE);
+  const anyRunState = params.get(SearchParamsKeys.DAG_RUN_STATE);
+  const withinHours = params.get(SearchParamsKeys.DAG_RUN_STATE_WITHIN_HOURS);
+
+  // The latest-run param wins when both are present, matching how the list 
endpoint treats them.
+  if (lastRunState !== null && lastRunState !== "") {
+    return { lookback: "latest", state: lastRunState };
+  }
+  if (anyRunState === null || anyRunState === "") {
+    return undefined;
+  }
+
+  const lookback = (TIME_LOOKBACKS as 
ReadonlyArray<string>).includes(withinHours ?? "")

Review Comment:
   Same here
   ```
   const isLookback = (value: string): value is RunStateLookback =>
     TIME_LOOKBACKS.some((lookback) => lookback === value);
   
   const lookback = withinHours !== null && isLookback(withinHours) ? 
withinHours : "any";
   ```



##########
airflow-core/src/airflow/ui/src/components/FilterBar/filters/RunStateFilter.tsx:
##########
@@ -0,0 +1,234 @@
+/*!
+ * 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 { useRef, useState } from "react";
+
+import { Box, HStack, createListCollection } from "@chakra-ui/react";
+import { useTranslation } from "react-i18next";
+import { FiClock, FiZap } from "react-icons/fi";
+
+import { Select } from "src/system-components";
+
+import { FilterPill } from "../FilterPill";
+import type { FilterConfig, FilterPluginProps } from "../types";
+import { RUN_STATE_LOOKBACKS, isRunStateValue, type RunStateLookback } from 
"./runStateParams";
+
+const LOOKBACK_LABEL_KEYS: Record<RunStateLookback, string> = {
+  "24": "last24Hours",
+  "168": "last7Days",
+  "720": "last30Days",
+  any: "anyTime",
+  latest: "latestRun",
+};
+
+type SelectOption = {
+  label: string;
+  value: string;
+};
+
+type RunStateFilterConfig = {
+  options: Array<SelectOption>;
+};
+
+const collapsePill = () => {
+  setTimeout(() => {
+    const activeElement = document.activeElement as HTMLElement;
+
+    activeElement.blur();
+  }, 0);
+};
+
+/**
+ * One pill filtering Dags on a run state matched within a lookback: the 
latest run only, a
+ * recent time window, or the whole run history. Committed as a composite 
value that the
+ * config's projections spread over the underlying URL params.
+ */
+export const RunStateFilter = ({ filter, onChange, onRemove }: 
FilterPluginProps) => {
+  const { t: translate } = useTranslation(["common"]);
+  const config = filter.config as FilterConfig & RunStateFilterConfig;
+  const value = isRunStateValue(filter.value) ? filter.value : undefined;
+
+  // Holds a lookback picked before any state is chosen, when there is nothing 
to commit yet.
+  const [pendingLookback, setPendingLookback] = 
useState<RunStateLookback>(value?.lookback ?? "latest");
+
+  // Selecting an option closes the menu in the same tick as the value 
commits, before React has
+  // re-rendered. Recording the selection keeps the close from being mistaken 
for an abandoned
+  // filter, which a timing-based check gets wrong.
+  const hasJustSelected = useRef(false);
+
+  const handleStateChange = ({ value: selected }: { value: Array<string> }) => 
{
+    const [newState] = selected;
+
+    if (newState === undefined) {
+      return;
+    }
+    hasJustSelected.current = true;
+    onChange({ lookback: value?.lookback ?? pendingLookback, state: newState 
});
+    collapsePill();
+  };
+
+  const handleLookbackChange = ({ value: selected }: { value: Array<string> }) 
=> {
+    const [newLookback] = selected as Array<RunStateLookback>;

Review Comment:
   Let's try to avoid this typecasting with a "value in" check like:
   ```
   export const isLookback = (value: string): value is RunStateLookback =>
     RUN_STATE_LOOKBACKS.some((lookback) => lookback === value);
   ```



-- 
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