bito-code-review[bot] commented on code in PR #44216: URL: https://github.com/apache/superset/pull/44216#discussion_r4078926892
########## superset-frontend/src/dashboard/components/gridComponents/FilterHolder/FilterHolder.tsx: ########## @@ -0,0 +1,712 @@ +/** + * 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, Divider, Filter, isNativeFilter } 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 isValueEmpty = (value: unknown): boolean => + value == null || + (Array.isArray(value) && value.length === 0) || + (typeof value === 'string' && value.trim() === ''); + +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 rawFilter = filterId ? nativeFilters[filterId] : undefined; + const filter = + rawFilter && isNativeFilter(rawFilter as Filter | Divider) + ? (rawFilter as Filter) + : 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 isApplyDisabled = useMemo(() => { + if (!stagedDataMask || !filter) { + return true; + } + if (stagedDataMask.filterState?.validateStatus === 'error') { + return true; + } + const isRequired = !!filter.controlValues?.enableEmptyFilter; + const value = stagedDataMask.filterState?.value; + if (isRequired && isValueEmpty(value)) { + return true; + } + return false; + }, [filter, stagedDataMask]); + + const handleFilterSelectionChange = useCallback( + (targetFilter: Filter, nextDataMask: DataMask) => { + if (applyMode === 'manual') { + const isRequired = !!targetFilter.controlValues?.enableEmptyFilter; + const value = nextDataMask.filterState?.value; + const isEmpty = isValueEmpty(value); + const validateStatus = + (isRequired && isEmpty) || + nextDataMask.filterState?.validateStatus === 'error' + ? 'error' + : undefined; + + setStagedDataMask({ + ...nextDataMask, + filterState: { + ...nextDataMask.filterState, + validateStatus, + }, + }); + } else { + const sanitized = sanitizeDataMask(nextDataMask); + dispatch(updateDataMask(targetFilter.id, sanitized)); + } + }, + [applyMode, dispatch, sanitizeDataMask], + ); + + const handleApplyStagedFilter = useCallback(() => { + if (filter && stagedDataMask && !isApplyDisabled) { + dispatch(updateDataMask(filter.id, sanitizeDataMask(stagedDataMask))); + setStagedDataMask(null); + } + }, [dispatch, filter, isApplyDisabled, sanitizeDataMask, stagedDataMask]); + + const handleClearStagedFilter = useCallback(() => { + if (filter) { + const clearedValue = + filter.filterType === 'filter_range' ? [null, null] : null; + const isRequired = !!filter.controlValues?.enableEmptyFilter; + const clearedMask: DataMask = { + filterState: { + value: clearedValue, + validateStatus: isRequired ? 'error' : undefined, + }, + extraFormData: {}, + }; + if (applyMode === 'manual') { + const hasAppliedValue = !isValueEmpty( + dataMask[filter.id]?.filterState?.value, + ); + 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) + .filter((f: any): f is Filter => isNativeFilter(f)) Review Comment: <div> <div id="suggestion"> <div id="issue"><b>any type used</b></div> <div id="fix"> `f: any` violates the repo's explicit no-`any` rule (AGENTS.md:38, dev-standard.mdc:16). `Object.values(nativeFilters)` yields `Filters[string]` = `Filter | Divider | ChartCustomization | ChartCustomizationDivider`; type the callback param with that union and let `isNativeFilter` narrow it. </div> </div> <small><i>Code Review Run #722f8b</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them ########## superset-frontend/src/dashboard/components/nativeFilters/FilterBar/useFilterControlFactory.tsx: ########## @@ -37,9 +43,32 @@ export const useFilterControlFactory = ( onClearAllComplete?: (filterId: string) => void, ) => { const filters = useFilters(); + const dashboardLayout = useSelector<RootState, DashboardLayout>( + state => state.dashboardLayout?.present || {}, + ); + + const canvasFilterIds = useMemo(() => { + const ids = new Set<string>(); + Object.values(dashboardLayout).forEach(item => { + const filterId = + item?.type === FILTER_TYPE ? String(item?.meta?.filterId || '') : ''; + if (filterId && filterId in filters) { + ids.add(filterId); + } + }); + return ids; + }, [dashboardLayout, filters]); + const filterValues = useMemo( - () => Object.values(filters) as (Filter | Divider)[], - [filters], + () => + (Object.values(filters) as (Filter | Divider)[]).filter( + filter => + isFilterDivider(filter) || + !canvasFilterIds.has(filter.id) || + (Boolean(filter.requiredFirst) && + dataMaskSelected[filter.id]?.filterState?.value === undefined), Review Comment: <div> <div id="suggestion"> <div id="issue"><b>Null value not treated as missing</b></div> <div id="fix"> This predicate treats only `undefined` as a missing value, but repo-wide cleared/default filter state is `filterState: { value: null }` (e.g. `FilterBar/index.tsx`), and the canonical `checkIsMissingRequiredValue` in `../utils` treats both null and undefined as missing. A required canvas filter defaulted to or cleared to null is hidden from the bar with no way to set it. Please also test for null or reuse the helper. </div> </div> <small><i>Code Review Run #722f8b</i></small> </div> --- Should Bito avoid suggestions like this for future reviews? (<a href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>) - [ ] Yes, avoid them -- 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]
