diff --git a/.changeset/nested-filter-keep-expanded.md b/.changeset/nested-filter-keep-expanded.md new file mode 100644 index 0000000000..1c8357408d --- /dev/null +++ b/.changeset/nested-filter-keep-expanded.md @@ -0,0 +1,7 @@ +--- +'@hyperdx/app': patch +--- + +fix: keys inside Map and JSON filter groups (e.g. `LogAttributes`) in the search +page filter sidebar stay expanded or collapsed after scrolling them out of view +and back. diff --git a/packages/app/src/components/DBSearchPageFilters.tsx b/packages/app/src/components/DBSearchPageFilters.tsx index 5f848ff44d..4ac0ea29e7 100644 --- a/packages/app/src/components/DBSearchPageFilters.tsx +++ b/packages/app/src/components/DBSearchPageFilters.tsx @@ -1,4 +1,11 @@ -import { memo, useCallback, useEffect, useMemo, useState } from 'react'; +import { + memo, + useCallback, + useEffect, + useEffectEvent, + useMemo, + useState, +} from 'react'; import cx from 'classnames'; import { TableMetadata, @@ -413,6 +420,10 @@ export type FilterGroupProps = { loadMoreLoading: boolean; hasLoadedMore: boolean; isDefaultExpanded?: boolean; + expanded?: boolean; + onExpandedChange?: (expanded: boolean) => void; + showDistributions?: boolean; + onShowDistributionsChange?: (show: boolean) => void; showFilterCounts?: boolean; 'data-testid'?: string; chartConfig: BuilderChartConfigWithDateRange; @@ -517,6 +528,8 @@ const FilterGroupBody = ({ onFetchingDistributionChange(isFetchingDistribution); }, [isFetchingDistribution, onFetchingDistributionChange]); + // onDistributionError isn't stable; re-running on it would notify again + const handleDistributionError = useEffectEvent(onDistributionError); useEffect(() => { if (distributionError) { notifications.show({ @@ -525,9 +538,9 @@ const FilterGroupBody = ({ message: distributionError?.message, autoClose: 5000, }); - onDistributionError(); + handleDistributionError(); } - }, [distributionError, onDistributionError]); + }, [distributionError]); const totalAppliedFiltersSize = selectedValues.included.size + @@ -920,6 +933,10 @@ export const FilterGroup = ({ loadMoreLoading, hasLoadedMore, isDefaultExpanded, + expanded, + onExpandedChange, + showDistributions: _showDistributions, + onShowDistributionsChange, showFilterCounts, 'data-testid': dataTestId, chartConfig, @@ -927,8 +944,21 @@ export const FilterGroup = ({ distributionKey, onRangeChange, }: FilterGroupProps) => { - const [isExpanded, setExpanded] = useState(isDefaultExpanded ?? false); - const [showDistributions, setShowDistributions] = useState(false); + const [uncontrolledExpanded, setUncontrolledExpanded] = useState( + isDefaultExpanded ?? false, + ); + const isExpanded = expanded ?? uncontrolledExpanded; + const setExpanded = (value: boolean) => { + setUncontrolledExpanded(value); + onExpandedChange?.(value); + }; + const [uncontrolledShowDistributions, setUncontrolledShowDistributions] = + useState(false); + const showDistributions = _showDistributions ?? uncontrolledShowDistributions; + const setShowDistributions = (value: boolean) => { + setUncontrolledShowDistributions(value); + onShowDistributionsChange?.(value); + }; const [isFetchingDistribution, setIsFetchingDistribution] = useState(false); const selectedValues: SelectedValues = useMemo( @@ -938,22 +968,16 @@ export const FilterGroup = ({ const hasRange = selectedValues.range != null; - const toggleShowDistributions = useCallback(() => { - setShowDistributions(prev => { - if (!prev) { - setExpanded(true); - } - return !prev; - }); - }, []); + const toggleShowDistributions = () => { + if (!showDistributions) setExpanded(true); + setShowDistributions(!showDistributions); + }; - const onDistributionError = useCallback(() => { - setShowDistributions(false); - }, []); + const onDistributionError = () => setShowDistributions(false); useEffect(() => { if (isDefaultExpanded) { - setExpanded(true); + setUncontrolledExpanded(true); } }, [isDefaultExpanded]); diff --git a/packages/app/src/components/DBSearchPageFilters/NestedFilterGroup.tsx b/packages/app/src/components/DBSearchPageFilters/NestedFilterGroup.tsx index 7da5b45a33..df1241b3be 100644 --- a/packages/app/src/components/DBSearchPageFilters/NestedFilterGroup.tsx +++ b/packages/app/src/components/DBSearchPageFilters/NestedFilterGroup.tsx @@ -48,6 +48,12 @@ type NestedFilterGroupProps = { isLive?: boolean; }; +type ChildState = { + expanded: boolean; + showDistributions?: boolean; + hadSelections: boolean; +}; + const VIRTUAL_ITEM_HEIGHT_ESTIMATE = 26; const MAX_VIRTUAL_LIST_HEIGHT = 350; @@ -107,6 +113,30 @@ export const NestedFilterGroup = ({ }, [childFilters, search]); const scrollContainerRef = useRef(null); + const keyHasSelections = (key: string) => { + const entry = selectedValues[key]; + return entry != null && entry.included.size + entry.excluded.size > 0; + }; + // Rows unmount when scrolled out of view, so their state lives here. Like + // regular filter groups, a key expands when it gains a filter (including one + // added outside the sidebar) and keeps its state when it loses one. + const [childState, setChildState] = useState>({}); + const updateChildState = (key: string, patch: Partial) => + setChildState(prev => ({ ...prev, [key]: { ...prev[key], ...patch } })); + const changedKeys = Object.keys({ ...selectedValues, ...childState }).filter( + key => (childState[key]?.hadSelections ?? false) !== keyHasSelections(key), + ); + if (changedKeys.length > 0) { + setChildState(prev => { + const next = { ...prev }; + for (const key of changedKeys) { + next[key] = keyHasSelections(key) + ? { ...prev[key], expanded: true, hadSelections: true } + : { ...prev[key], hadSelections: false }; + } + return next; + }); + } const rowVirtualizer = useVirtualizer({ count: isExpanded ? matchingChildFilters.length : 0, @@ -203,10 +233,7 @@ export const NestedFilterGroup = ({ included: new Set(), excluded: new Set(), }; - const childHasSelections = - childSelectedValues.included.size + - childSelectedValues.excluded.size > - 0; + const childHasSelections = keyHasSelections(child.key); return (
+ updateChildState(child.key, { + expanded, + hadSelections: childHasSelections, + }) + } + showDistributions={ + childState[child.key]?.showDistributions ?? false + } + onShowDistributionsChange={showDistributions => + updateChildState(child.key, { showDistributions }) + } chartConfig={chartConfig} isLive={isLive} /> diff --git a/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx b/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx index 7ec0223d0f..ee287bdafd 100644 --- a/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx +++ b/packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx @@ -70,11 +70,14 @@ jest.mock('@/hooks/useMetadata', () => ({ .mockReturnValue({ data: undefined, isFetching: false, error: undefined }), })); -const mockDistribution = (data: Map) => +const mockDistribution = ( + data: Map | undefined, + error: Error | null = null, +) => jest.mocked(useGetValuesDistribution).mockReturnValue({ data, isFetching: false, - error: null, + error, } as UseQueryResult>); describe('cleanedFacetName', () => { @@ -904,6 +907,186 @@ describe('NestedFilterGroup', () => { expect(screen.queryByText('host.name')).toBeNull(); }); + it('should keep a child expanded after its row unmounts and remounts', async () => { + renderWithMantine( + , + ); + const childTestId = `nested-filter-group-${manyChildFilters[0].key}`; + const child = () => within(screen.getByTestId(childTestId)); + + await userEvent.click(child().getByTestId('filter-group-control')); + expect(child().getByTestId('filter-group-panel')).toHaveAttribute( + 'aria-hidden', + 'false', + ); + + // Filtering the row out unmounts it, like scrolling it out of view + const search = screen.getByPlaceholderText('Search keys...'); + await userEvent.type(search, 'k8s'); + expect(screen.queryByTestId(childTestId)).toBeNull(); + await userEvent.clear(search); + + expect(child().getByTestId('filter-group-panel')).toHaveAttribute( + 'aria-hidden', + 'false', + ); + }); + + it('should keep a child with selections collapsed after its row unmounts and remounts', async () => { + const { key } = manyChildFilters[0]; + renderWithMantine( + , + ); + const childTestId = `nested-filter-group-${key}`; + const child = () => within(screen.getByTestId(childTestId)); + expect(child().getByTestId('filter-group-panel')).toHaveAttribute( + 'aria-hidden', + 'false', + ); + + await userEvent.click(child().getByTestId('filter-group-control')); + expect(child().getByTestId('filter-group-panel')).toHaveAttribute( + 'aria-hidden', + 'true', + ); + + const search = screen.getByPlaceholderText('Search keys...'); + await userEvent.type(search, 'k8s'); + expect(screen.queryByTestId(childTestId)).toBeNull(); + await userEvent.clear(search); + + expect(child().getByTestId('filter-group-panel')).toHaveAttribute( + 'aria-hidden', + 'true', + ); + }); + + it('should keep distributions on after a row unmounts and remounts', async () => { + renderWithMantine( + , + ); + const toggleTestId = `toggle-distribution-button-${manyChildFilters[0].propertyPath}`; + + await userEvent.click(screen.getByTestId(toggleTestId)); + expect(screen.getByTestId(toggleTestId)).toHaveAttribute( + 'aria-checked', + 'true', + ); + + const search = screen.getByPlaceholderText('Search keys...'); + await userEvent.type(search, 'k8s'); + expect(screen.queryByTestId(toggleTestId)).toBeNull(); + await userEvent.clear(search); + + expect(screen.getByTestId(toggleTestId)).toHaveAttribute( + 'aria-checked', + 'true', + ); + }); + + it('should turn distributions off and notify once when they fail to load', async () => { + mockDistribution(undefined, new Error('boom')); + renderWithMantine( + , + ); + const toggleTestId = `toggle-distribution-button-${manyChildFilters[0].propertyPath}`; + + await userEvent.click(screen.getByTestId(toggleTestId)); + + expect(screen.getByTestId(toggleTestId)).toHaveAttribute( + 'aria-checked', + 'false', + ); + expect( + await screen.findAllByText('Error loading filter distribution'), + ).toHaveLength(1); + mockDistribution(undefined); + }); + + it('should expand a collapsed child when a filter for it is added elsewhere', async () => { + const props = { + ...defaultNestedProps, + childFilters: manyChildFilters, + isDefaultExpanded: true, + }; + const { rerender } = renderWithMantine(); + const { key } = manyChildFilters[0]; + const child = () => + within(screen.getByTestId(`nested-filter-group-${key}`)); + + await userEvent.click(child().getByTestId('filter-group-control')); + await userEvent.click(child().getByTestId('filter-group-control')); + expect(child().getByTestId('filter-group-panel')).toHaveAttribute( + 'aria-hidden', + 'true', + ); + + rerender( + + + , + ); + + expect(child().getByTestId('filter-group-panel')).toHaveAttribute( + 'aria-hidden', + 'false', + ); + }); + + it('should keep a child expanded when its filter is removed', () => { + const props = { + ...defaultNestedProps, + childFilters: manyChildFilters, + isDefaultExpanded: true, + }; + const { key } = manyChildFilters[0]; + const { rerender } = renderWithMantine( + , + ); + const panel = () => + within(screen.getByTestId(`nested-filter-group-${key}`)).getByTestId( + 'filter-group-panel', + ); + expect(panel()).toHaveAttribute('aria-hidden', 'false'); + + rerender( + + + , + ); + + expect(panel()).toHaveAttribute('aria-hidden', 'false'); + }); + it('should show an empty state when no keys match', async () => { renderWithMantine(