pierrejeambrun commented on code in PR #63273: URL: https://github.com/apache/airflow/pull/63273#discussion_r2945689866
########## airflow-core/src/airflow/ui/src/pages/DagsList/useTagFilter.test.tsx: ########## @@ -0,0 +1,311 @@ +/*! + * 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 { act, renderHook } from "@testing-library/react"; +import type { PropsWithChildren } from "react"; +import { MemoryRouter } from "react-router-dom"; +import { afterEach, describe, expect, it } from "vitest"; + +import { BaseWrapper } from "src/utils/Wrapper"; + +import { useTagFilter } from "./useTagFilter"; + +const createWrapper = + (initialEntries: Array<string> = ["/"]) => + ({ children }: PropsWithChildren) => ( + <BaseWrapper> + <MemoryRouter initialEntries={initialEntries}>{children}</MemoryRouter> + </BaseWrapper> + ); + +afterEach(() => { + localStorage.clear(); +}); + +describe("useTagFilter — initial state", () => { + it("returns empty tags and 'any' mode by default", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + expect(result.current.selectedTags).toEqual([]); + expect(result.current.tagFilterMode).toBe("any"); + }); + + it("reads tags from URL params", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(["/?tags=production&tags=ml"]), + }); + + expect(result.current.selectedTags).toEqual(["production", "ml"]); + }); + + it("reads match mode from URL params", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(["/?tags=production&tags=ml&tags_match_mode=all"]), + }); + + expect(result.current.tagFilterMode).toBe("all"); + }); + + it("falls back to localStorage when URL has no tags", () => { + localStorage.setItem("tags", JSON.stringify(["saved-tag-1", "saved-tag-2"])); + + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + expect(result.current.selectedTags).toEqual(["saved-tag-1", "saved-tag-2"]); + }); + + it("restores match mode from localStorage when using saved tags with 2+ tags", () => { + localStorage.setItem("tags", JSON.stringify(["tag-a", "tag-b"])); + localStorage.setItem("tags_match_mode", JSON.stringify("all")); + + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + expect(result.current.tagFilterMode).toBe("all"); + }); + + it("defaults match mode to 'any' when saved tags has fewer than 2 tags", () => { + localStorage.setItem("tags", JSON.stringify(["only-one"])); + localStorage.setItem("tags_match_mode", JSON.stringify("all")); + + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + expect(result.current.tagFilterMode).toBe("any"); + }); + + it("URL tags take precedence over localStorage", () => { + localStorage.setItem("tags", JSON.stringify(["saved-tag"])); + + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(["/?tags=url-tag"]), + }); + + expect(result.current.selectedTags).toEqual(["url-tag"]); + }); +}); + +describe("useTagFilter — setSelectedTags", () => { + it("sets tags", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + act(() => { + result.current.setSelectedTags(["new-tag-1", "new-tag-2"]); + }); + + expect(result.current.selectedTags).toEqual(["new-tag-1", "new-tag-2"]); + }); + + it("saves tags to localStorage", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + act(() => { + result.current.setSelectedTags(["persisted-tag"]); + }); + + expect(JSON.parse(localStorage.getItem("tags") ?? "[]")).toEqual(["persisted-tag"]); + }); + + it("clears tags when given empty array", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(["/?tags=old-tag"]), + }); + + act(() => { + result.current.setSelectedTags([]); + }); + + expect(result.current.selectedTags).toEqual([]); + }); + + it("resets match mode to 'any' when fewer than 2 tags", () => { Review Comment: I'm not sure about that. If someone edits the tags and want to replace one, removing and then adding another one, this can change the match mode while this is not expected. (I want "all", then I realize tags aren't correct, remove the incorrect tag, add the correct one, match mode is gone back to 'any' which isn't what I want) ########## airflow-core/src/airflow/ui/src/pages/DagsList/useTagFilter.ts: ########## @@ -0,0 +1,66 @@ +/*! + * 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 { useSearchParams } from "react-router-dom"; +import { useLocalStorage } from "usehooks-ts"; + +import { SearchParamsKeys, type SearchParamsKeysType } from "src/constants/searchParams"; + +const { OFFSET, TAGS, TAGS_MATCH_MODE }: SearchParamsKeysType = SearchParamsKeys; + +type TagMatchMode = "all" | "any"; + +export const useTagFilter = () => { + const [searchParams, setSearchParams] = useSearchParams(); + const [savedTags, setSavedTags] = useLocalStorage<Array<string>>(TAGS, []); + const [savedTagMatchMode, setSavedTagMatchMode] = useLocalStorage<TagMatchMode>(TAGS_MATCH_MODE, "any"); + + const urlTags = searchParams.getAll(TAGS); + const urlMatchMode = searchParams.get(TAGS_MATCH_MODE); + + // URL params take precedence; fall back to localStorage when URL has no tags. + const selectedTags = urlTags.length > 0 ? urlTags : savedTags; + const tagFilterMode: TagMatchMode = + urlMatchMode === null + ? urlTags.length === 0 && selectedTags.length >= 2 + ? savedTagMatchMode + : "any" + : (urlMatchMode as TagMatchMode); + + const setSelectedTags = (tags: Array<string>) => { + searchParams.delete(TAGS); + tags.forEach((tag) => { + searchParams.append(TAGS, tag); + }); + if (tags.length < 2) { + searchParams.delete(TAGS_MATCH_MODE); + setSavedTagMatchMode("any"); + } + searchParams.delete(OFFSET); + setSearchParams(searchParams); + setSavedTags(tags); + }; + + const setTagFilterMode = (mode: TagMatchMode) => { + searchParams.set(TAGS_MATCH_MODE, mode); + setSearchParams(searchParams); + setSavedTagMatchMode(mode); Review Comment: We should probably also reset OFFSET when changing the match mode. Going from one to another could leave us on a blank page. For instance from going to 'any' to 'all'. ########## airflow-core/src/airflow/ui/src/pages/DagsList/useTagFilter.test.tsx: ########## @@ -0,0 +1,311 @@ +/*! + * 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 { act, renderHook } from "@testing-library/react"; +import type { PropsWithChildren } from "react"; +import { MemoryRouter } from "react-router-dom"; +import { afterEach, describe, expect, it } from "vitest"; + +import { BaseWrapper } from "src/utils/Wrapper"; + +import { useTagFilter } from "./useTagFilter"; + +const createWrapper = + (initialEntries: Array<string> = ["/"]) => + ({ children }: PropsWithChildren) => ( + <BaseWrapper> + <MemoryRouter initialEntries={initialEntries}>{children}</MemoryRouter> + </BaseWrapper> + ); + +afterEach(() => { + localStorage.clear(); +}); + +describe("useTagFilter — initial state", () => { + it("returns empty tags and 'any' mode by default", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + expect(result.current.selectedTags).toEqual([]); + expect(result.current.tagFilterMode).toBe("any"); + }); + + it("reads tags from URL params", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(["/?tags=production&tags=ml"]), + }); + + expect(result.current.selectedTags).toEqual(["production", "ml"]); + }); + + it("reads match mode from URL params", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(["/?tags=production&tags=ml&tags_match_mode=all"]), + }); + + expect(result.current.tagFilterMode).toBe("all"); + }); + + it("falls back to localStorage when URL has no tags", () => { + localStorage.setItem("tags", JSON.stringify(["saved-tag-1", "saved-tag-2"])); + + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + expect(result.current.selectedTags).toEqual(["saved-tag-1", "saved-tag-2"]); + }); + + it("restores match mode from localStorage when using saved tags with 2+ tags", () => { + localStorage.setItem("tags", JSON.stringify(["tag-a", "tag-b"])); + localStorage.setItem("tags_match_mode", JSON.stringify("all")); + + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + expect(result.current.tagFilterMode).toBe("all"); + }); + + it("defaults match mode to 'any' when saved tags has fewer than 2 tags", () => { + localStorage.setItem("tags", JSON.stringify(["only-one"])); + localStorage.setItem("tags_match_mode", JSON.stringify("all")); + + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + expect(result.current.tagFilterMode).toBe("any"); + }); + + it("URL tags take precedence over localStorage", () => { + localStorage.setItem("tags", JSON.stringify(["saved-tag"])); + + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(["/?tags=url-tag"]), + }); + + expect(result.current.selectedTags).toEqual(["url-tag"]); + }); +}); + +describe("useTagFilter — setSelectedTags", () => { + it("sets tags", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + act(() => { + result.current.setSelectedTags(["new-tag-1", "new-tag-2"]); + }); + + expect(result.current.selectedTags).toEqual(["new-tag-1", "new-tag-2"]); + }); + + it("saves tags to localStorage", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(), + }); + + act(() => { + result.current.setSelectedTags(["persisted-tag"]); + }); + + expect(JSON.parse(localStorage.getItem("tags") ?? "[]")).toEqual(["persisted-tag"]); + }); + + it("clears tags when given empty array", () => { + const { result } = renderHook(() => useTagFilter(), { + wrapper: createWrapper(["/?tags=old-tag"]), + }); + + act(() => { + result.current.setSelectedTags([]); + }); + + expect(result.current.selectedTags).toEqual([]); + }); + + it("resets match mode to 'any' when fewer than 2 tags", () => { Review Comment: I would simplify this and not have any side effect from one filter to another. Let the user handle them separately. -- 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]
