bbovenzi commented on code in PR #63273: URL: https://github.com/apache/airflow/pull/63273#discussion_r2918633517
########## 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. Review Comment: I'd rather we just rely on url params and not have a competing source of truth with localStorage ########## 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: I think it would be great to have some unit tests for these functions. Adding/removing tags, toggling any/all, especially when tags length changes from below to above 2 -- 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]
