Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/nested-filter-keep-expanded.md
Original file line number Diff line number Diff line change
@@ -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.
58 changes: 41 additions & 17 deletions packages/app/src/components/DBSearchPageFilters.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Comment thread
knudtty marked this conversation as resolved.
Resolved
notifications.show({
Expand All @@ -525,9 +538,9 @@ const FilterGroupBody = ({
message: distributionError?.message,
autoClose: 5000,
});
onDistributionError();
handleDistributionError();
}
}, [distributionError, onDistributionError]);
}, [distributionError]);

const totalAppliedFiltersSize =
selectedValues.included.size +
Expand Down Expand Up @@ -920,15 +933,32 @@ export const FilterGroup = ({
loadMoreLoading,
hasLoadedMore,
isDefaultExpanded,
expanded,
onExpandedChange,
showDistributions: _showDistributions,
onShowDistributionsChange,
showFilterCounts,
'data-testid': dataTestId,
chartConfig,
isLive,
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(
Expand All @@ -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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ type NestedFilterGroupProps = {
isLive?: boolean;
};

type ChildState = {
Comment thread
knudtty marked this conversation as resolved.
Resolved
expanded: boolean;
showDistributions?: boolean;
hadSelections: boolean;
};

const VIRTUAL_ITEM_HEIGHT_ESTIMATE = 26;
const MAX_VIRTUAL_LIST_HEIGHT = 350;

Expand Down Expand Up @@ -107,6 +113,30 @@ export const NestedFilterGroup = ({
}, [childFilters, search]);

const scrollContainerRef = useRef<HTMLDivElement>(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<Record<string, ChildState>>({});
const updateChildState = (key: string, patch: Partial<ChildState>) =>
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,
Expand Down Expand Up @@ -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 (
<div
Expand Down Expand Up @@ -275,7 +302,22 @@ export const NestedFilterGroup = ({
}
hasLoadedMore={hasLoadedMore[child.key] || false}
showFilterCounts={showFilterCounts}
isDefaultExpanded={childHasSelections}
expanded={
Comment thread
knudtty marked this conversation as resolved.
Resolved
childState[child.key]?.expanded ??
childHasSelections
}
onExpandedChange={expanded =>
updateChildState(child.key, {
expanded,
hadSelections: childHasSelections,
})
}
showDistributions={
childState[child.key]?.showDistributions ?? false
}
onShowDistributionsChange={showDistributions =>
updateChildState(child.key, { showDistributions })
}
Comment thread
knudtty marked this conversation as resolved.
Resolved
chartConfig={chartConfig}
isLive={isLive}
/>
Expand Down
Loading
Loading