krmmkr commented on code in PR #44216: URL: https://github.com/apache/superset/pull/44216#discussion_r4022078863
########## superset-frontend/src/dashboard/components/gridComponents/FilterHolder/FilterHolder.tsx: ########## @@ -0,0 +1,664 @@ +/** + * 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 { useCallback, useMemo, useState, ReactNode } from 'react'; +import cx from 'classnames'; +import { useDispatch, useSelector } from 'react-redux'; +import { ResizeCallback, ResizeStartCallback } from 're-resizable'; +import { css, useTheme } from '@apache-superset/core/theme'; +import { t } from '@apache-superset/core/translation'; +import { DataMask, Filter } from '@superset-ui/core'; +import { Button, Select } from '@superset-ui/core/components'; +import { Icons } from '@superset-ui/core/components/Icons'; +import PopoverDropdown from '@superset-ui/core/components/PopoverDropdown'; +import { + FilterBarOrientation, + LayoutItem, + RootState, +} from 'src/dashboard/types'; +import { updateDataMask } from 'src/dataMask/actions'; +import { Draggable } from 'src/dashboard/components/dnd/DragDroppable'; +import DragHandle from 'src/dashboard/components/dnd/DragHandle'; +import HoverMenu from 'src/dashboard/components/menu/HoverMenu'; +import IconButton from 'src/dashboard/components/IconButton'; +import WithPopoverMenu from 'src/dashboard/components/menu/WithPopoverMenu'; +import DeleteComponentButton from 'src/dashboard/components/DeleteComponentButton'; +import ResizableContainer from 'src/dashboard/components/resizable/ResizableContainer'; +import FilterControl from 'src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterControl'; +import { COLUMN_TYPE, ROW_TYPE } from 'src/dashboard/util/componentTypes'; +import { + GRID_BASE_UNIT, + GRID_MIN_COLUMN_COUNT, + GRID_MIN_ROW_UNITS, + GRID_COLUMN_COUNT, +} from 'src/dashboard/util/constants'; + +export interface FilterHolderProps { + id: string; + parentId: string; + component: LayoutItem; + parentComponent: LayoutItem; + index: number; + depth: number; + editMode: boolean; + + // grid related + availableColumnCount: number; + columnWidth: number; + onResizeStart: ResizeStartCallback; + onResize: ResizeCallback; + onResizeStop: ResizeCallback; + + // dnd + deleteComponent: (id: string, parentId: string) => void; + updateComponents: (updates: Record<string, LayoutItem>) => void; + handleComponentDrop: (...args: unknown[]) => unknown; +} + +const FilterHolder = ({ + id, + parentId, + component, + parentComponent, + index, + depth, + availableColumnCount, + columnWidth, + onResizeStart, + onResize, + onResizeStop, + editMode, + deleteComponent, + updateComponents, + handleComponentDrop, +}: FilterHolderProps) => { + const theme = useTheme(); + const dispatch = useDispatch(); + + const [isFocused, setIsFocused] = useState(false); + const [stagedDataMask, setStagedDataMask] = useState<DataMask | null>(null); + + const nativeFilters = useSelector( + (state: RootState) => state.nativeFilters?.filters || {}, + ); + const dataMask = useSelector((state: RootState) => state.dataMask || {}); + + const filterId = component.meta?.filterId as string | undefined; + const filter = filterId + ? (nativeFilters[filterId] as Filter | undefined) + : undefined; + + const titlePosition = + (component.meta?.titlePosition as 'top' | 'left') || 'top'; + const applyMode = + (component.meta?.applyMode as 'instant' | 'manual') || 'instant'; + const buttonPlacement = + (component.meta?.buttonPlacement as 'bottom' | 'right' | 'stacked_right') || + 'bottom'; + + const filterWithDataMask = useMemo(() => { + if (!filter) return null; + return { + ...filter, + dataMask: stagedDataMask || dataMask[filter.id], + inCanvas: true, + } as Filter & { inCanvas: boolean }; + }, [filter, dataMask, stagedDataMask]); + + const updateMeta = useCallback( + (metaUpdates: Record<string, unknown>) => { + updateComponents({ + [component.id]: { + ...component, + meta: { + ...component.meta, + ...metaUpdates, + }, + }, + }); + }, + [component, updateComponents], + ); + + const handleChangeTitlePosition = useCallback( + (nextPosition: string) => { + updateMeta({ titlePosition: nextPosition }); + }, + [updateMeta], + ); + + const handleChangeApplyMode = useCallback( + (nextApplyMode: string) => { + setStagedDataMask(null); + updateMeta({ applyMode: nextApplyMode }); + }, + [updateMeta], + ); + + const handleChangeButtonPlacement = useCallback( + (nextPlacement: string) => { + updateMeta({ buttonPlacement: nextPlacement }); + }, + [updateMeta], + ); + + const handleSelectFilter = useCallback( + (nextFilterId: string) => { + setStagedDataMask(null); + updateMeta({ filterId: nextFilterId }); + }, + [updateMeta], + ); + + const sanitizeDataMask = useCallback((mask: DataMask): DataMask => { + const { filterState, ...restDataMask } = mask; + return filterState + ? { + ...restDataMask, + filterState: { + ...filterState, + validateStatus: undefined, + }, + } + : mask; + }, []); + + const handleFilterSelectionChange = useCallback( + (targetFilter: Filter, nextDataMask: DataMask) => { + const sanitized = sanitizeDataMask(nextDataMask); + if (applyMode === 'manual') { + setStagedDataMask(sanitized); + } else { + dispatch(updateDataMask(targetFilter.id, sanitized)); + } + }, + [applyMode, dispatch, sanitizeDataMask], + ); + + const handleApplyStagedFilter = useCallback(() => { + if (filter && stagedDataMask) { + dispatch(updateDataMask(filter.id, sanitizeDataMask(stagedDataMask))); + setStagedDataMask(null); + } + }, [dispatch, filter, sanitizeDataMask, stagedDataMask]); + + const handleClearStagedFilter = useCallback(() => { + if (filter) { + const clearedValue = + filter.filterType === 'filter_range' ? [null, null] : undefined; + const clearedMask: DataMask = { + filterState: { value: clearedValue }, + extraFormData: {}, + }; + if (applyMode === 'manual') { + const hasAppliedValue = + dataMask[filter.id]?.filterState?.value !== undefined; + if (hasAppliedValue) { + setStagedDataMask(clearedMask); + } else { + setStagedDataMask(null); + } + } else { + dispatch(updateDataMask(filter.id, clearedMask)); + setStagedDataMask(null); + } + } + }, [applyMode, dataMask, dispatch, filter]); + + const handleDelete = useCallback(() => { + deleteComponent(id, parentId); + }, [deleteComponent, id, parentId]); + + const availableFilters = useMemo( + () => + Object.values(nativeFilters).map((f: Filter) => ({ Review Comment: Updated! We now narrow both availableFilters and the bound filter with isNativeFilter so dividers and chart customizations are excluded from the picker and cannot be passed down to FilterControl. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
