michael-s-molina commented on code in PR #22064: URL: https://github.com/apache/superset/pull/22064#discussion_r1018310865
########## superset-frontend/src/dashboard/components/nativeFilters/FilterBar/Vertical.tsx: ########## @@ -0,0 +1,298 @@ +/** + * 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. + */ + +/* eslint-disable no-param-reassign */ +import throttle from 'lodash/throttle'; +import React, { + useEffect, + useState, + useCallback, + useMemo, + useRef, + createContext, +} from 'react'; +import cx from 'classnames'; +import { HandlerFunction, styled, t, isNativeFilter } from '@superset-ui/core'; +import Icons from 'src/components/Icons'; +import { AntdTabs } from 'src/components'; +import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags'; +import Loading from 'src/components/Loading'; +import { EmptyStateSmall } from 'src/components/EmptyState'; +import { TabIds, getFilterBarTestId, VerticalBarProps } from './utils'; +import FilterSets from './FilterSets'; +import { useFilterSets } from './state'; +import EditSection from './FilterSets/EditSection'; +import Header from './Header'; +import FilterControls from './FilterControls/FilterControls'; + +const BarWrapper = styled.div<{ width: number }>` + width: ${({ theme }) => theme.gridUnit * 8}px; + + & .ant-tabs-top > .ant-tabs-nav { + margin: 0; + } + &.open { + width: ${({ width }) => width}px; // arbitrary... + } +`; + +const Bar = styled.div<{ width: number }>` + & .ant-typography-edit-content { + left: 0; + margin-top: 0; + width: 100%; + } + position: absolute; + top: 0; + left: 0; + flex-direction: column; + flex-grow: 1; + width: ${({ width }) => width}px; + background: ${({ theme }) => theme.colors.grayscale.light5}; + border-right: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + border-bottom: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + min-height: 100%; + display: none; + &.open { + display: flex; + } +`; + +const CollapsedBar = styled.div<{ offset: number }>` + position: absolute; + top: ${({ offset }) => offset}px; + left: 0; + height: 100%; + width: ${({ theme }) => theme.gridUnit * 8}px; Review Comment: Same here ########## superset-frontend/src/dashboard/components/nativeFilters/FilterBar/Vertical.tsx: ########## @@ -0,0 +1,298 @@ +/** + * 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. + */ + +/* eslint-disable no-param-reassign */ +import throttle from 'lodash/throttle'; +import React, { + useEffect, + useState, + useCallback, + useMemo, + useRef, + createContext, +} from 'react'; +import cx from 'classnames'; +import { HandlerFunction, styled, t, isNativeFilter } from '@superset-ui/core'; +import Icons from 'src/components/Icons'; +import { AntdTabs } from 'src/components'; +import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags'; +import Loading from 'src/components/Loading'; +import { EmptyStateSmall } from 'src/components/EmptyState'; +import { TabIds, getFilterBarTestId, VerticalBarProps } from './utils'; +import FilterSets from './FilterSets'; +import { useFilterSets } from './state'; +import EditSection from './FilterSets/EditSection'; +import Header from './Header'; +import FilterControls from './FilterControls/FilterControls'; + +const BarWrapper = styled.div<{ width: number }>` + width: ${({ theme }) => theme.gridUnit * 8}px; + + & .ant-tabs-top > .ant-tabs-nav { + margin: 0; + } + &.open { + width: ${({ width }) => width}px; // arbitrary... + } +`; + +const Bar = styled.div<{ width: number }>` + & .ant-typography-edit-content { + left: 0; + margin-top: 0; + width: 100%; + } + position: absolute; + top: 0; + left: 0; + flex-direction: column; + flex-grow: 1; + width: ${({ width }) => width}px; + background: ${({ theme }) => theme.colors.grayscale.light5}; Review Comment: Maybe promote `theme` to the top level to avoid repeating code? ########## superset-frontend/src/dashboard/components/nativeFilters/FilterBar/index.tsx: ########## @@ -352,166 +233,56 @@ const FilterBar: React.FC<FiltersBarProps> = ({ }); }, [dataMaskSelected, dispatch, setDataMaskSelected]); - const openFiltersBar = useCallback( - () => toggleFiltersBar(true), - [toggleFiltersBar], - ); - - const onScroll = useCallback( - throttle(() => { - clearTimeout(timeout.current); - setIsScrolling(true); - timeout.current = setTimeout(() => { - setIsScrolling(false); - }, 300); - }, 200), - [], - ); - - useEffect(() => { - document.onscroll = onScroll; - return () => { - document.onscroll = null; - }; - }, [onScroll]); - useFilterUpdates(dataMaskSelected, setDataMaskSelected); const isApplyDisabled = checkIsApplyDisabled( dataMaskSelected, dataMaskApplied, nativeFilterValues, ); const isInitialized = useInitialization(); - const tabPaneStyle = useMemo( - () => ({ overflow: 'auto', height, overscrollBehavior: 'contain' }), - [height], - ); - const numberOfFilters = nativeFilterValues.length; - - return ( - <FilterBarScrollContext.Provider value={isScrolling}> - <BarWrapper - {...getFilterBarTestId()} - className={cx({ open: filtersOpen })} - width={width} - > - <CollapsedBar - {...getFilterBarTestId('collapsable')} - className={cx({ open: !filtersOpen })} - onClick={openFiltersBar} - offset={offset} - > - <StyledCollapseIcon - {...getFilterBarTestId('expand-button')} - iconSize="l" - /> - <StyledFilterIcon - {...getFilterBarTestId('filter-icon')} - iconSize="l" - /> - </CollapsedBar> - <Bar className={cx({ open: filtersOpen })} width={width}> - <Header toggleFiltersBar={toggleFiltersBar} /> - {!isInitialized ? ( - <div css={{ height }}> - <Loading /> - </div> - ) : isFeatureEnabled(FeatureFlag.DASHBOARD_NATIVE_FILTERS_SET) ? ( - <StyledTabs - centered - onChange={setTab as HandlerFunction} - defaultActiveKey={TabIds.AllFilters} - activeKey={editFilterSetId ? TabIds.AllFilters : undefined} - > - <AntdTabs.TabPane - tab={t('All filters (%(filterCount)d)', { - filterCount: numberOfFilters, - })} - key={TabIds.AllFilters} - css={tabPaneStyle} - > - {editFilterSetId && ( - <EditSection - dataMaskSelected={dataMaskSelected} - disabled={!isApplyDisabled} - onCancel={() => setEditFilterSetId(null)} - filterSetId={editFilterSetId} - /> - )} - {filterValues.length === 0 ? ( - <FilterBarEmptyStateContainer> - <EmptyStateSmall - title={t('No filters are currently added')} - image="filter.svg" - description={ - canEdit && - t( - 'Click the button above to add a filter to the dashboard', - ) - } - /> - </FilterBarEmptyStateContainer> - ) : ( - <FilterControls - dataMaskSelected={dataMaskSelected} - directPathToChild={directPathToChild} - onFilterSelectionChange={handleFilterSelectionChange} - /> - )} - </AntdTabs.TabPane> - <AntdTabs.TabPane - disabled={!!editFilterSetId} - tab={t('Filter sets (%(filterSetCount)d)', { - filterSetCount: filterSetFilterValues.length, - })} - key={TabIds.FilterSets} - css={tabPaneStyle} - > - <FilterSets - onEditFilterSet={setEditFilterSetId} - disabled={!isApplyDisabled} - dataMaskSelected={dataMaskSelected} - tab={tab} - onFilterSelectionChange={handleFilterSelectionChange} - /> - </AntdTabs.TabPane> - </StyledTabs> - ) : ( - <div css={tabPaneStyle} onScroll={onScroll}> - {filterValues.length === 0 ? ( - <FilterBarEmptyStateContainer> - <EmptyStateSmall - title={t('No filters are currently added')} - image="filter.svg" - description={ - canEdit && - t( - 'Click the button above to add a filter to the dashboard', - ) - } - /> - </FilterBarEmptyStateContainer> - ) : ( - <FilterControls - dataMaskSelected={dataMaskSelected} - directPathToChild={directPathToChild} - onFilterSelectionChange={handleFilterSelectionChange} - /> - )} - </div> - )} - <ActionButtons - width={width} - onApply={handleApply} - onClearAll={handleClearAll} - dataMaskSelected={dataMaskSelected} - dataMaskApplied={dataMaskApplied} - isApplyDisabled={isApplyDisabled} - /> - </Bar> - </BarWrapper> - </FilterBarScrollContext.Provider> + const actions = useMemo( + () => ( + <ActionButtons + orientation={orientation} + width={verticalConfig?.width} + onApply={handleApply} + onClearAll={handleClearAll} + dataMaskSelected={dataMaskSelected} + dataMaskApplied={dataMaskApplied} + isApplyDisabled={isApplyDisabled} + /> + ), + [verticalConfig?.width, dataMaskSelected, dataMaskApplied, isApplyDisabled], Review Comment: We have many missing dependencies here. I suggest not using `useMemo` if we can't handle the dependencies because it will be using stale values. ########## superset-frontend/src/dashboard/components/nativeFilters/FilterBar/Vertical.tsx: ########## @@ -0,0 +1,298 @@ +/** + * 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. + */ + +/* eslint-disable no-param-reassign */ +import throttle from 'lodash/throttle'; +import React, { + useEffect, + useState, + useCallback, + useMemo, + useRef, + createContext, +} from 'react'; +import cx from 'classnames'; +import { HandlerFunction, styled, t, isNativeFilter } from '@superset-ui/core'; +import Icons from 'src/components/Icons'; +import { AntdTabs } from 'src/components'; +import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags'; +import Loading from 'src/components/Loading'; +import { EmptyStateSmall } from 'src/components/EmptyState'; +import { TabIds, getFilterBarTestId, VerticalBarProps } from './utils'; +import FilterSets from './FilterSets'; +import { useFilterSets } from './state'; +import EditSection from './FilterSets/EditSection'; +import Header from './Header'; +import FilterControls from './FilterControls/FilterControls'; + +const BarWrapper = styled.div<{ width: number }>` + width: ${({ theme }) => theme.gridUnit * 8}px; + + & .ant-tabs-top > .ant-tabs-nav { + margin: 0; + } + &.open { + width: ${({ width }) => width}px; // arbitrary... + } +`; + +const Bar = styled.div<{ width: number }>` + & .ant-typography-edit-content { + left: 0; + margin-top: 0; + width: 100%; + } + position: absolute; + top: 0; + left: 0; + flex-direction: column; + flex-grow: 1; + width: ${({ width }) => width}px; + background: ${({ theme }) => theme.colors.grayscale.light5}; + border-right: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + border-bottom: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + min-height: 100%; + display: none; + &.open { + display: flex; + } +`; + +const CollapsedBar = styled.div<{ offset: number }>` + position: absolute; + top: ${({ offset }) => offset}px; + left: 0; + height: 100%; + width: ${({ theme }) => theme.gridUnit * 8}px; + padding-top: ${({ theme }) => theme.gridUnit * 2}px; + display: none; + text-align: center; + &.open { + display: flex; + flex-direction: column; + align-items: center; + padding: ${({ theme }) => theme.gridUnit * 2}px; + } + svg { + cursor: pointer; + } +`; + +const StyledCollapseIcon = styled(Icons.Collapse)` + color: ${({ theme }) => theme.colors.primary.base}; + margin-bottom: ${({ theme }) => theme.gridUnit * 3}px; +`; + +const StyledFilterIcon = styled(Icons.Filter)` + color: ${({ theme }) => theme.colors.grayscale.base}; +`; + +const StyledTabs = styled(AntdTabs)` + & .ant-tabs-nav-list { + width: 100%; + } + & .ant-tabs-tab { + display: flex; + justify-content: center; + margin: 0; + flex: 1; + } + + & > .ant-tabs-nav .ant-tabs-nav-operations { + display: none; + } +`; + +const FilterBarEmptyStateContainer = styled.div` + margin-top: ${({ theme }) => theme.gridUnit * 8}px; +`; + +export const FilterBarScrollContext = createContext(false); +const VerticalFilterBar: React.FC<VerticalBarProps> = ({ + actions, + canEdit, + dataMaskSelected, + directPathToChild, + filtersOpen, + filterValues, + height, + isDisabled, + isInitialized, + offset, + onSelectionChange, + toggleFiltersBar, + width, +}) => { + const [editFilterSetId, setEditFilterSetId] = useState<number | null>(null); + const filterSets = useFilterSets(); + const filterSetFilterValues = Object.values(filterSets); + const [tab, setTab] = useState(TabIds.AllFilters); + const nativeFilterValues = filterValues.filter(isNativeFilter); + const [isScrolling, setIsScrolling] = useState(false); + const timeout = useRef<any>(); + + const openFiltersBar = useCallback( + () => toggleFiltersBar(true), + [toggleFiltersBar], + ); + + const onScroll = useCallback( + throttle(() => { + clearTimeout(timeout.current); + setIsScrolling(true); + timeout.current = setTimeout(() => { + setIsScrolling(false); + }, 300); + }, 200), + [], + ); Review Comment: This is functionally equivalent without the warning. ```suggestion const onScroll = useMemo( () => throttle(() => { clearTimeout(timeout.current); setIsScrolling(true); timeout.current = setTimeout(() => { setIsScrolling(false); }, 300); }, 200), [], ); ``` ########## superset-frontend/src/dashboard/components/nativeFilters/FilterBar/utils.ts: ########## @@ -18,7 +18,52 @@ */ import { areObjectsEqual } from 'src/reduxUtils'; -import { DataMaskStateWithId, Filter, FilterState } from '@superset-ui/core'; +import { + DataMask, + DataMaskStateWithId, + Divider, + Filter, + FilterState, +} from '@superset-ui/core'; +import { testWithId } from 'src/utils/testUtils'; +import { FilterBarLocation } from 'src/dashboard/types'; + +interface CommonFiltersBarProps { Review Comment: Can we leave only utility functions in this file and move type definitions to a types file? ########## superset-frontend/src/dashboard/components/nativeFilters/FilterBar/Vertical.tsx: ########## @@ -0,0 +1,298 @@ +/** + * 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. + */ + +/* eslint-disable no-param-reassign */ +import throttle from 'lodash/throttle'; +import React, { + useEffect, + useState, + useCallback, + useMemo, + useRef, + createContext, +} from 'react'; +import cx from 'classnames'; +import { HandlerFunction, styled, t, isNativeFilter } from '@superset-ui/core'; +import Icons from 'src/components/Icons'; +import { AntdTabs } from 'src/components'; +import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags'; +import Loading from 'src/components/Loading'; +import { EmptyStateSmall } from 'src/components/EmptyState'; +import { TabIds, getFilterBarTestId, VerticalBarProps } from './utils'; +import FilterSets from './FilterSets'; +import { useFilterSets } from './state'; +import EditSection from './FilterSets/EditSection'; +import Header from './Header'; +import FilterControls from './FilterControls/FilterControls'; + +const BarWrapper = styled.div<{ width: number }>` + width: ${({ theme }) => theme.gridUnit * 8}px; + + & .ant-tabs-top > .ant-tabs-nav { + margin: 0; + } + &.open { + width: ${({ width }) => width}px; // arbitrary... + } +`; + +const Bar = styled.div<{ width: number }>` + & .ant-typography-edit-content { + left: 0; + margin-top: 0; + width: 100%; + } + position: absolute; + top: 0; + left: 0; + flex-direction: column; + flex-grow: 1; + width: ${({ width }) => width}px; + background: ${({ theme }) => theme.colors.grayscale.light5}; + border-right: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + border-bottom: 1px solid ${({ theme }) => theme.colors.grayscale.light2}; + min-height: 100%; + display: none; + &.open { + display: flex; + } +`; + +const CollapsedBar = styled.div<{ offset: number }>` + position: absolute; + top: ${({ offset }) => offset}px; + left: 0; + height: 100%; + width: ${({ theme }) => theme.gridUnit * 8}px; + padding-top: ${({ theme }) => theme.gridUnit * 2}px; + display: none; + text-align: center; + &.open { + display: flex; + flex-direction: column; + align-items: center; + padding: ${({ theme }) => theme.gridUnit * 2}px; + } + svg { + cursor: pointer; + } +`; + +const StyledCollapseIcon = styled(Icons.Collapse)` + color: ${({ theme }) => theme.colors.primary.base}; Review Comment: Same here -- 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: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org