pierrejeambrun commented on code in PR #68484: URL: https://github.com/apache/airflow/pull/68484#discussion_r3529721014
########## airflow-core/src/airflow/ui/src/components/SavedViewsMenu.tsx: ########## @@ -0,0 +1,260 @@ +/*! + * 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, Button, HStack, Input, Text, VStack } from "@chakra-ui/react"; +import type { SortingState } from "@tanstack/react-table"; +import { useEffect, useState } from "react"; +import { useTranslation } from "react-i18next"; +import { FiTrash2 } from "react-icons/fi"; +import { LuBookmark, LuInfo } from "react-icons/lu"; +import { MdOutlinePushPin, MdPushPin } from "react-icons/md"; +import { useLocation, useSearchParams } from "react-router-dom"; +import { useLocalStorage } from "usehooks-ts"; + +import DeleteDialog from "src/components/DeleteDialog"; +import { IconButton, Popover, Tooltip } from "src/components/ui"; +import { savedViewsDefaultKey, savedViewsKey, tableSortKey } from "src/constants/localStorage"; +import { SearchParamsKeys } from "src/constants/searchParams"; + +type SavedView = { + readonly name: string; + readonly search: string; +}; + +const normalizeSearch = (value: string) => { + const params = new URLSearchParams(value); + + params.sort(); + + return params.toString(); +}; + +export const SavedViewsMenu = () => { + const { t: translate } = useTranslation("common"); + const { pathname } = useLocation(); + const [searchParams, setSearchParams] = useSearchParams(); + const [sorting, setSorting] = useLocalStorage<SortingState>(tableSortKey(pathname), []); + const [savedViews, setSavedViews] = useLocalStorage<Array<SavedView>>(savedViewsKey(pathname), []); + const [defaultViewName, setDefaultViewName] = useLocalStorage<string | null>( + savedViewsDefaultKey(pathname), + null, + ); + const [name, setName] = useState(""); + const [open, setOpen] = useState(false); + const [viewToDelete, setViewToDelete] = useState<string | undefined>(undefined); + + const viewParams = new URLSearchParams(searchParams); + + viewParams.delete(SearchParamsKeys.OFFSET); + viewParams.delete(SearchParamsKeys.CURSOR); + // Only URL filters count, so the bare page blocks save even when a sort sits in localStorage. + const hasViewToSave = [...viewParams].length > 0; + + // The sort is often only in localStorage, not the URL — bake it into the snapshot so it is restored. + if (viewParams.getAll(SearchParamsKeys.SORT).length === 0) { + sorting.forEach(({ desc, id }) => viewParams.append(SearchParamsKeys.SORT, `${desc ? "-" : ""}${id}`)); + } + const search = viewParams.toString(); + + const duplicateView = savedViews.find((view) => normalizeSearch(view.search) === normalizeSearch(search)); + + const handleSave = () => { + const trimmedName = name.trim(); + + if (trimmedName === "" || !hasViewToSave || duplicateView !== undefined) { + return; + } + + setSavedViews((prev) => + prev.some((view) => view.name === trimmedName) + ? prev.map((view) => (view.name === trimmedName ? { name: trimmedName, search } : view)) + : [...prev, { name: trimmedName, search }], + ); + setName(""); + }; + + const applyView = (view: SavedView) => { + const params = new URLSearchParams(view.search); + + setSorting( + params + .getAll(SearchParamsKeys.SORT) Review Comment: That's just to check if landing view has filter or not. If it has not then restore the 'default view'. Added comment and clarified the variables names -- 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]
