Skip to content

Commit 8e451b2

Browse files
committed
persist distributions
1 parent 0e001d9 commit 8e451b2

3 files changed

Lines changed: 106 additions & 31 deletions

File tree

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
1+
import {
2+
memo,
3+
useCallback,
4+
useEffect,
5+
useEffectEvent,
6+
useMemo,
7+
useState,
8+
} from 'react';
29
import cx from 'classnames';
310
import {
411
TableMetadata,
@@ -415,6 +422,8 @@ export type FilterGroupProps = {
415422
isDefaultExpanded?: boolean;
416423
expanded?: boolean;
417424
onExpandedChange?: (expanded: boolean) => void;
425+
showDistributions?: boolean;
426+
onShowDistributionsChange?: (show: boolean) => void;
418427
showFilterCounts?: boolean;
419428
'data-testid'?: string;
420429
chartConfig: BuilderChartConfigWithDateRange;
@@ -519,6 +528,8 @@ const FilterGroupBody = ({
519528
onFetchingDistributionChange(isFetchingDistribution);
520529
}, [isFetchingDistribution, onFetchingDistributionChange]);
521530

531+
// onDistributionError isn't stable; re-running on it would notify again
532+
const handleDistributionError = useEffectEvent(onDistributionError);
522533
useEffect(() => {
523534
if (distributionError) {
524535
notifications.show({
@@ -527,9 +538,9 @@ const FilterGroupBody = ({
527538
message: distributionError?.message,
528539
autoClose: 5000,
529540
});
530-
onDistributionError();
541+
handleDistributionError();
531542
}
532-
}, [distributionError, onDistributionError]);
543+
}, [distributionError]);
533544

534545
const totalAppliedFiltersSize =
535546
selectedValues.included.size +
@@ -924,6 +935,8 @@ export const FilterGroup = ({
924935
isDefaultExpanded,
925936
expanded,
926937
onExpandedChange,
938+
showDistributions: _showDistributions,
939+
onShowDistributionsChange,
927940
showFilterCounts,
928941
'data-testid': dataTestId,
929942
chartConfig,
@@ -939,7 +952,13 @@ export const FilterGroup = ({
939952
setUncontrolledExpanded(value);
940953
onExpandedChange?.(value);
941954
};
942-
const [showDistributions, setShowDistributions] = useState(false);
955+
const [uncontrolledShowDistributions, setUncontrolledShowDistributions] =
956+
useState(false);
957+
const showDistributions = _showDistributions ?? uncontrolledShowDistributions;
958+
const setShowDistributions = (value: boolean) => {
959+
setUncontrolledShowDistributions(value);
960+
onShowDistributionsChange?.(value);
961+
};
943962
const [isFetchingDistribution, setIsFetchingDistribution] = useState(false);
944963

945964
const selectedValues: SelectedValues = useMemo(
@@ -954,9 +973,7 @@ export const FilterGroup = ({
954973
setShowDistributions(!showDistributions);
955974
};
956975

957-
const onDistributionError = useCallback(() => {
958-
setShowDistributions(false);
959-
}, []);
976+
const onDistributionError = () => setShowDistributions(false);
960977

961978
useEffect(() => {
962979
if (isDefaultExpanded) {

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

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ type NestedFilterGroupProps = {
4848
isLive?: boolean;
4949
};
5050

51+
type ChildState = {
52+
expanded: boolean;
53+
showDistributions?: boolean;
54+
hadSelections: boolean;
55+
};
56+
5157
const VIRTUAL_ITEM_HEIGHT_ESTIMATE = 26;
5258
const MAX_VIRTUAL_LIST_HEIGHT = 350;
5359

@@ -111,25 +117,21 @@ export const NestedFilterGroup = ({
111117
const entry = selectedValues[key];
112118
return entry != null && entry.included.size + entry.excluded.size > 0;
113119
};
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.
117-
const [expandedChildren, setExpandedChildren] = useState<
118-
Record<string, { expanded: boolean; hadSelections: boolean }>
119-
>({});
120-
const changedKeys = Object.keys({
121-
...selectedValues,
122-
...expandedChildren,
123-
}).filter(
124-
key =>
125-
(expandedChildren[key]?.hadSelections ?? false) !== keyHasSelections(key),
120+
// Rows unmount when scrolled out of view, so their state lives here. Like
121+
// regular filter groups, a key expands when it gains a filter (including one
122+
// added outside the sidebar) and keeps its state when it loses one.
123+
const [childState, setChildState] = useState<Record<string, ChildState>>({});
124+
const updateChildState = (key: string, patch: Partial<ChildState>) =>
125+
setChildState(prev => ({ ...prev, [key]: { ...prev[key], ...patch } }));
126+
const changedKeys = Object.keys({ ...selectedValues, ...childState }).filter(
127+
key => (childState[key]?.hadSelections ?? false) !== keyHasSelections(key),
126128
);
127129
if (changedKeys.length > 0) {
128-
setExpandedChildren(prev => {
130+
setChildState(prev => {
129131
const next = { ...prev };
130132
for (const key of changedKeys) {
131133
next[key] = keyHasSelections(key)
132-
? { expanded: true, hadSelections: true }
134+
? { ...prev[key], expanded: true, hadSelections: true }
133135
: { ...prev[key], hadSelections: false };
134136
}
135137
return next;
@@ -301,17 +303,20 @@ export const NestedFilterGroup = ({
301303
hasLoadedMore={hasLoadedMore[child.key] || false}
302304
showFilterCounts={showFilterCounts}
303305
expanded={
304-
expandedChildren[child.key]?.expanded ??
306+
childState[child.key]?.expanded ??
305307
childHasSelections
306308
}
307309
onExpandedChange={expanded =>
308-
setExpandedChildren(prev => ({
309-
...prev,
310-
[child.key]: {
311-
expanded,
312-
hadSelections: childHasSelections,
313-
},
314-
}))
310+
updateChildState(child.key, {
311+
expanded,
312+
hadSelections: childHasSelections,
313+
})
314+
}
315+
showDistributions={
316+
childState[child.key]?.showDistributions ?? false
317+
}
318+
onShowDistributionsChange={showDistributions =>
319+
updateChildState(child.key, { showDistributions })
315320
}
316321
chartConfig={chartConfig}
317322
isLive={isLive}

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

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,14 @@ jest.mock('@/hooks/useMetadata', () => ({
7070
.mockReturnValue({ data: undefined, isFetching: false, error: undefined }),
7171
}));
7272

73-
const mockDistribution = (data: Map<string, number>) =>
73+
const mockDistribution = (
74+
data: Map<string, number> | undefined,
75+
error: Error | null = null,
76+
) =>
7477
jest.mocked(useGetValuesDistribution).mockReturnValue({
7578
data,
7679
isFetching: false,
77-
error: null,
80+
error,
7881
} as UseQueryResult<Map<string, number>>);
7982

8083
describe('cleanedFacetName', () => {
@@ -969,6 +972,56 @@ describe('NestedFilterGroup', () => {
969972
);
970973
});
971974

975+
it('should keep distributions on after a row unmounts and remounts', async () => {
976+
renderWithMantine(
977+
<NestedFilterGroup
978+
{...defaultNestedProps}
979+
childFilters={manyChildFilters}
980+
isDefaultExpanded={true}
981+
/>,
982+
);
983+
const toggleTestId = `toggle-distribution-button-${manyChildFilters[0].propertyPath}`;
984+
985+
await userEvent.click(screen.getByTestId(toggleTestId));
986+
expect(screen.getByTestId(toggleTestId)).toHaveAttribute(
987+
'aria-checked',
988+
'true',
989+
);
990+
991+
const search = screen.getByPlaceholderText('Search keys...');
992+
await userEvent.type(search, 'k8s');
993+
expect(screen.queryByTestId(toggleTestId)).toBeNull();
994+
await userEvent.clear(search);
995+
996+
expect(screen.getByTestId(toggleTestId)).toHaveAttribute(
997+
'aria-checked',
998+
'true',
999+
);
1000+
});
1001+
1002+
it('should turn distributions off and notify once when they fail to load', async () => {
1003+
mockDistribution(undefined, new Error('boom'));
1004+
renderWithMantine(
1005+
<NestedFilterGroup
1006+
{...defaultNestedProps}
1007+
childFilters={manyChildFilters}
1008+
isDefaultExpanded={true}
1009+
/>,
1010+
);
1011+
const toggleTestId = `toggle-distribution-button-${manyChildFilters[0].propertyPath}`;
1012+
1013+
await userEvent.click(screen.getByTestId(toggleTestId));
1014+
1015+
expect(screen.getByTestId(toggleTestId)).toHaveAttribute(
1016+
'aria-checked',
1017+
'false',
1018+
);
1019+
expect(
1020+
await screen.findAllByText('Error loading filter distribution'),
1021+
).toHaveLength(1);
1022+
mockDistribution(undefined);
1023+
});
1024+
9721025
it('should expand a collapsed child when a filter for it is added elsewhere', async () => {
9731026
const props = {
9741027
...defaultNestedProps,

0 commit comments

Comments
 (0)