Skip to content

Commit 0e001d9

Browse files
committed
pr suggestions
1 parent 62b30c0 commit 0e001d9

3 files changed

Lines changed: 151 additions & 34 deletions

File tree

‎packages/app/src/components/DBSearchPageFilters.tsx‎

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
memo,
3-
useCallback,
4-
useEffect,
5-
useEffectEvent,
6-
useMemo,
7-
useState,
8-
} from 'react';
1+
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
92
import cx from 'classnames';
103
import {
114
TableMetadata,
@@ -420,6 +413,7 @@ export type FilterGroupProps = {
420413
loadMoreLoading: boolean;
421414
hasLoadedMore: boolean;
422415
isDefaultExpanded?: boolean;
416+
expanded?: boolean;
423417
onExpandedChange?: (expanded: boolean) => void;
424418
showFilterCounts?: boolean;
425419
'data-testid'?: string;
@@ -928,6 +922,7 @@ export const FilterGroup = ({
928922
loadMoreLoading,
929923
hasLoadedMore,
930924
isDefaultExpanded,
925+
expanded,
931926
onExpandedChange,
932927
showFilterCounts,
933928
'data-testid': dataTestId,
@@ -936,7 +931,14 @@ export const FilterGroup = ({
936931
distributionKey,
937932
onRangeChange,
938933
}: FilterGroupProps) => {
939-
const [isExpanded, setExpanded] = useState(isDefaultExpanded ?? false);
934+
const [uncontrolledExpanded, setUncontrolledExpanded] = useState(
935+
isDefaultExpanded ?? false,
936+
);
937+
const isExpanded = expanded ?? uncontrolledExpanded;
938+
const setExpanded = (value: boolean) => {
939+
setUncontrolledExpanded(value);
940+
onExpandedChange?.(value);
941+
};
940942
const [showDistributions, setShowDistributions] = useState(false);
941943
const [isFetchingDistribution, setIsFetchingDistribution] = useState(false);
942944

@@ -947,32 +949,21 @@ export const FilterGroup = ({
947949

948950
const hasRange = selectedValues.range != null;
949951

950-
const toggleShowDistributions = useCallback(() => {
951-
setShowDistributions(prev => {
952-
if (!prev) {
953-
setExpanded(true);
954-
}
955-
return !prev;
956-
});
957-
}, []);
952+
const toggleShowDistributions = () => {
953+
if (!showDistributions) setExpanded(true);
954+
setShowDistributions(!showDistributions);
955+
};
958956

959957
const onDistributionError = useCallback(() => {
960958
setShowDistributions(false);
961959
}, []);
962960

963961
useEffect(() => {
964962
if (isDefaultExpanded) {
965-
setExpanded(true);
963+
setUncontrolledExpanded(true);
966964
}
967965
}, [isDefaultExpanded]);
968966

969-
// A virtualized parent passes the reported state back as isDefaultExpanded,
970-
// so it survives the row unmounting when scrolled out of view.
971-
const reportExpanded = useEffectEvent(() => onExpandedChange?.(isExpanded));
972-
useEffect(() => {
973-
if (isExpanded !== isDefaultExpanded) reportExpanded();
974-
}, [isExpanded, isDefaultExpanded]);
975-
976967
const totalAppliedFiltersSize =
977968
selectedValues.included.size +
978969
selectedValues.excluded.size +

‎packages/app/src/components/DBSearchPageFilters/NestedFilterGroup.tsx‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,34 @@ export const NestedFilterGroup = ({
107107
}, [childFilters, search]);
108108

109109
const scrollContainerRef = useRef<HTMLDivElement>(null);
110-
// Rows unmount when scrolled out of view, so their expanded state lives here
110+
const keyHasSelections = (key: string) => {
111+
const entry = selectedValues[key];
112+
return entry != null && entry.included.size + entry.excluded.size > 0;
113+
};
114+
// Rows unmount when scrolled out of view, so their expanded state lives here.
115+
// Like regular filter groups, a key expands when it gains a filter (including
116+
// one added outside the sidebar) and keeps its state when it loses one.
111117
const [expandedChildren, setExpandedChildren] = useState<
112-
Record<string, boolean>
118+
Record<string, { expanded: boolean; hadSelections: boolean }>
113119
>({});
120+
const changedKeys = Object.keys({
121+
...selectedValues,
122+
...expandedChildren,
123+
}).filter(
124+
key =>
125+
(expandedChildren[key]?.hadSelections ?? false) !== keyHasSelections(key),
126+
);
127+
if (changedKeys.length > 0) {
128+
setExpandedChildren(prev => {
129+
const next = { ...prev };
130+
for (const key of changedKeys) {
131+
next[key] = keyHasSelections(key)
132+
? { expanded: true, hadSelections: true }
133+
: { ...prev[key], hadSelections: false };
134+
}
135+
return next;
136+
});
137+
}
114138

115139
const rowVirtualizer = useVirtualizer({
116140
count: isExpanded ? matchingChildFilters.length : 0,
@@ -207,10 +231,7 @@ export const NestedFilterGroup = ({
207231
included: new Set(),
208232
excluded: new Set(),
209233
};
210-
const childHasSelections =
211-
childSelectedValues.included.size +
212-
childSelectedValues.excluded.size >
213-
0;
234+
const childHasSelections = keyHasSelections(child.key);
214235

215236
return (
216237
<div
@@ -279,13 +300,17 @@ export const NestedFilterGroup = ({
279300
}
280301
hasLoadedMore={hasLoadedMore[child.key] || false}
281302
showFilterCounts={showFilterCounts}
282-
isDefaultExpanded={
283-
expandedChildren[child.key] ?? childHasSelections
303+
expanded={
304+
expandedChildren[child.key]?.expanded ??
305+
childHasSelections
284306
}
285307
onExpandedChange={expanded =>
286308
setExpandedChildren(prev => ({
287309
...prev,
288-
[child.key]: expanded,
310+
[child.key]: {
311+
expanded,
312+
hadSelections: childHasSelections,
313+
},
289314
}))
290315
}
291316
chartConfig={chartConfig}

‎packages/app/src/components/__tests__/DBSearchPageFilters.test.tsx‎

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,107 @@ describe('NestedFilterGroup', () => {
933933
);
934934
});
935935

936+
it('should keep a child with selections collapsed after its row unmounts and remounts', async () => {
937+
const { key } = manyChildFilters[0];
938+
renderWithMantine(
939+
<NestedFilterGroup
940+
{...defaultNestedProps}
941+
childFilters={manyChildFilters}
942+
selectedValues={{
943+
[key]: { included: new Set(['val']), excluded: new Set() },
944+
}}
945+
isDefaultExpanded={true}
946+
/>,
947+
);
948+
const childTestId = `nested-filter-group-${key}`;
949+
const child = () => within(screen.getByTestId(childTestId));
950+
expect(child().getByTestId('filter-group-panel')).toHaveAttribute(
951+
'aria-hidden',
952+
'false',
953+
);
954+
955+
await userEvent.click(child().getByTestId('filter-group-control'));
956+
expect(child().getByTestId('filter-group-panel')).toHaveAttribute(
957+
'aria-hidden',
958+
'true',
959+
);
960+
961+
const search = screen.getByPlaceholderText('Search keys...');
962+
await userEvent.type(search, 'k8s');
963+
expect(screen.queryByTestId(childTestId)).toBeNull();
964+
await userEvent.clear(search);
965+
966+
expect(child().getByTestId('filter-group-panel')).toHaveAttribute(
967+
'aria-hidden',
968+
'true',
969+
);
970+
});
971+
972+
it('should expand a collapsed child when a filter for it is added elsewhere', async () => {
973+
const props = {
974+
...defaultNestedProps,
975+
childFilters: manyChildFilters,
976+
isDefaultExpanded: true,
977+
};
978+
const { rerender } = renderWithMantine(<NestedFilterGroup {...props} />);
979+
const { key } = manyChildFilters[0];
980+
const child = () =>
981+
within(screen.getByTestId(`nested-filter-group-${key}`));
982+
983+
await userEvent.click(child().getByTestId('filter-group-control'));
984+
await userEvent.click(child().getByTestId('filter-group-control'));
985+
expect(child().getByTestId('filter-group-panel')).toHaveAttribute(
986+
'aria-hidden',
987+
'true',
988+
);
989+
990+
rerender(
991+
<TestProviders>
992+
<NestedFilterGroup
993+
{...props}
994+
selectedValues={{
995+
[key]: { included: new Set(['val']), excluded: new Set() },
996+
}}
997+
/>
998+
</TestProviders>,
999+
);
1000+
1001+
expect(child().getByTestId('filter-group-panel')).toHaveAttribute(
1002+
'aria-hidden',
1003+
'false',
1004+
);
1005+
});
1006+
1007+
it('should keep a child expanded when its filter is removed', () => {
1008+
const props = {
1009+
...defaultNestedProps,
1010+
childFilters: manyChildFilters,
1011+
isDefaultExpanded: true,
1012+
};
1013+
const { key } = manyChildFilters[0];
1014+
const { rerender } = renderWithMantine(
1015+
<NestedFilterGroup
1016+
{...props}
1017+
selectedValues={{
1018+
[key]: { included: new Set(['val']), excluded: new Set() },
1019+
}}
1020+
/>,
1021+
);
1022+
const panel = () =>
1023+
within(screen.getByTestId(`nested-filter-group-${key}`)).getByTestId(
1024+
'filter-group-panel',
1025+
);
1026+
expect(panel()).toHaveAttribute('aria-hidden', 'false');
1027+
1028+
rerender(
1029+
<TestProviders>
1030+
<NestedFilterGroup {...props} />
1031+
</TestProviders>,
1032+
);
1033+
1034+
expect(panel()).toHaveAttribute('aria-hidden', 'false');
1035+
});
1036+
9361037
it('should show an empty state when no keys match', async () => {
9371038
renderWithMantine(
9381039
<NestedFilterGroup

0 commit comments

Comments
 (0)