Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(ui): expand sidebar filter sections with active URL filters on load (#14487)

Nikita Kabardinbb8bab9c8729
diff --git a/web/src/features/filters/hooks/useSidebarFilterState.tsx b/web/src/features/filters/hooks/useSidebarFilterState.tsx
index 8c9cab5342..74b32a86ba 100644
--- a/web/src/features/filters/hooks/useSidebarFilterState.tsx
+++ b/web/src/features/filters/hooks/useSidebarFilterState.tsx
@@ -439,6 +439,12 @@ export function useSidebarFilterState(
const setPeekTableState = peekContext?.setTableState;
const FILTER_EXPANDED_STORAGE_KEY = `${config.tableName}-filters-expanded`;
+ // Tracks which active-filter columns we have already auto-expanded once, so a
+ // section the user later collapsed is never re-expanded — even across remounts
+ // (route navigation away-and-back, tab reload). It shares the session lifecycle
+ // of the expanded state itself, so the "already reconciled" knowledge survives
+ // exactly as long as the manual collapse it must respect. See LFE-10164 below.
+ const FILTER_SEEDED_STORAGE_KEY = `${config.tableName}-filters-seeded`;
const DEFAULT_EXPANDED_FILTERS = config.defaultExpanded ?? [];
const [expandedString, setExpandedString] = useSessionStorage<string>(
@@ -455,6 +461,11 @@ export function useSidebarFilterState(
[setExpandedString],
);
+ const [seededString, setSeededString] = useSessionStorage<string>(
+ FILTER_SEEDED_STORAGE_KEY,
+ "",
+ );
+
const normalizedSessionFilterContextId =
stateLocationType === "urlAndSessionStorage"
? (hookOptions.sessionFilterContextId ?? null)
@@ -549,6 +560,74 @@ export function useSidebarFilterState(
? memoryFilterState
: urlFilterState;
+ // LFE-10164: When arriving via a URL/deep link that already carries applied
+ // filters, expand the sidebar sections that have an active filter. Sidebar
+ // sections are collapsed by default; without this, a bookmarked/shared link
+ // would render its active facets collapsed. Sections without an active filter
+ // keep their default state.
+ //
+ // We derive this during render (the "adjust state while rendering when a
+ // derived input changes" pattern, https://react.dev/learn/you-might-not-need-an-effect)
+ // rather than in a mount effect. The mount-effect approach had two defects:
+ // 1. Late-arriving URL params: in the Pages Router `useQueryParam` reads
+ // `router.query`, which is empty on the first render of a direct
+ // navigation and only populated on a later render. A one-shot mount
+ // effect seeded against the empty first render and never re-ran, so
+ // deep-linked sections stayed collapsed. Reconciling during render means
+ // the moment the filters actually appear (a later render) we seed them.
+ // 2. Remount re-seed: a per-mount guard re-expanded a section the user had
+ // deliberately collapsed whenever the page remounted. We instead persist
+ // which columns have already been auto-expanded (`seededString`, same
+ // session lifecycle as the expanded state) and only expand columns that
+ // are newly active and not yet reconciled, so a collapsed section is
+ // never re-expanded.
+ //
+ // We key off `explicitFilterState` (the URL/memory/peek-authored filters),
+ // which already excludes the implicit hidden-environment default — so the
+ // managed-environment section is only auto-expanded when the user actually
+ // authored an environment filter.
+ const seededSet = useMemo(
+ () => new Set(seededString.split(",").filter(Boolean)),
+ [seededString],
+ );
+ const facetColumnSet = useMemo(
+ () => new Set(config.facets.map((facet) => facet.column)),
+ [config.facets],
+ );
+ const newlyActiveFacetColumns = explicitFilterState
+ .map((filter) => filter.column)
+ .filter((column) => facetColumnSet.has(column) && !seededSet.has(column));
+ if (newlyActiveFacetColumns.length > 0) {
+ // setState-during-render (not an effect): React discards this render and
+ // re-renders synchronously with the updated state before painting. The
+ // updates are idempotent — once a column is in `seededSet` it is no longer
+ // "newly active", so this branch does not re-run for it on the next render.
+ setExpandedString((current) => {
+ const expanded = current.split(",").filter(Boolean);
+ const expandedSet = new Set(expanded);
+ const next = [...expanded];
+ for (const column of newlyActiveFacetColumns) {
+ if (!expandedSet.has(column)) {
+ expandedSet.add(column);
+ next.push(column);
+ }
+ }
+ return next.length === expanded.length ? current : next.join(",");
+ });
+ setSeededString((current) => {
+ const seeded = current.split(",").filter(Boolean);
+ const seededColumnSet = new Set(seeded);
+ const next = [...seeded];
+ for (const column of newlyActiveFacetColumns) {
+ if (!seededColumnSet.has(column)) {
+ seededColumnSet.add(column);
+ next.push(column);
+ }
+ }
+ return next.length === seeded.length ? current : next.join(",");
+ });
+ }
+
const managedEnvironmentPolicyConfig = useMemo(
() => buildManagedEnvironmentPolicyConfig(implicitDefaultConfig),
[implicitDefaultConfig],
diff --git a/web/src/features/filters/sidebarFilterSessionPersistence.clienttest.tsx b/web/src/features/filters/sidebarFilterSessionPersistence.clienttest.tsx
index ca2befabfa..1335669a2f 100644
--- a/web/src/features/filters/sidebarFilterSessionPersistence.clienttest.tsx
+++ b/web/src/features/filters/sidebarFilterSessionPersistence.clienttest.tsx
@@ -6,6 +6,11 @@ import { encodeFiltersGeneric } from "./lib/filter-query-encoding";
import { buildSidebarFilterQueryStorageKey } from "./lib/persistedSidebarFilterQuery";
const queryParamStore = new Map<string, unknown>();
+// Values placed here are NOT visible on the first render; they are applied on
+// mount via the query-param setter, reproducing the Next.js Pages Router race
+// where `router.query` is empty on the first render and only populated on a
+// later (hydration) render. See the deep-link expansion tests below.
+const deferredQueryParams = new Map<string, unknown>();
vi.mock("use-query-params", async () => {
const React = require("react");
@@ -51,6 +56,17 @@ vi.mock("use-query-params", async () => {
queryParamStore.set(key, value);
}, [key, value]);
+ // Apply a deferred value once, on mount: this lands the param on a later
+ // render than the first, simulating Pages Router query hydration.
+ React.useEffect(() => {
+ if (deferredQueryParams.has(key)) {
+ const deferred = deferredQueryParams.get(key);
+ deferredQueryParams.delete(key);
+ setQueryValue(deferred);
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, []);
+
return [value, setQueryValue] as const;
},
};
@@ -125,6 +141,119 @@ function SessionPersistenceHarness(props: { contextId?: string | null }) {
);
}
+// Reports the expanded state of the single "name" facet and lets a test
+// collapse it. Used to assert the LFE-10164 deep-link expansion behavior.
+function ExpansionHarness() {
+ const queryFilter = useSidebarFilterState(TEST_FILTER_CONFIG, TEST_OPTIONS, {
+ stateLocation: "urlAndSessionStorage",
+ });
+
+ const nameFacet = queryFilter.filters.find((f) => f.column === "name");
+
+ return (
+ <div>
+ <pre data-testid="name-expanded">{String(nameFacet?.expanded)}</pre>
+ <pre data-testid="expanded-list">
+ {JSON.stringify(queryFilter.expanded)}
+ </pre>
+ <button
+ data-testid="collapse-name"
+ onClick={() =>
+ queryFilter.onExpandedChange(
+ queryFilter.expanded.filter((c) => c !== "name"),
+ )
+ }
+ >
+ Collapse name
+ </button>
+ </div>
+ );
+}
+
+describe("useSidebarFilterState deep-link expansion (LFE-10164)", () => {
+ const encodedFilterA = encodeFiltersGeneric(FILTER_A);
+ const EXPANDED_STORAGE_KEY = `${TEST_FILTER_CONFIG.tableName}-filters-expanded`;
+ const SEEDED_STORAGE_KEY = `${TEST_FILTER_CONFIG.tableName}-filters-seeded`;
+
+ beforeEach(() => {
+ sessionStorage.clear();
+ queryParamStore.clear();
+ deferredQueryParams.clear();
+ });
+
+ const getNameExpanded = () =>
+ screen.getByTestId("name-expanded").textContent === "true";
+
+ it("expands a facet section whose filter arrives on the first render", async () => {
+ queryParamStore.set("filter", encodedFilterA);
+
+ render(<ExpansionHarness />);
+
+ await waitFor(() => {
+ expect(getNameExpanded()).toBe(true);
+ });
+ expect(
+ JSON.parse(sessionStorage.getItem(SEEDED_STORAGE_KEY) ?? '""'),
+ ).toContain("name");
+ });
+
+ it("expands a facet section whose URL filter only arrives on a later render (Pages Router race)", async () => {
+ // Empty on first render; the filter param lands on a later render. The old
+ // one-shot mount effect seeded against the empty first render and never
+ // re-ran, leaving the section collapsed. The during-render reconciliation
+ // must expand it once the filter actually arrives.
+ deferredQueryParams.set("filter", encodedFilterA);
+
+ render(<ExpansionHarness />);
+
+ // Section starts collapsed (no filter on first render).
+ expect(getNameExpanded()).toBe(false);
+
+ // Once the deferred filter arrives, the section expands.
+ await waitFor(() => {
+ expect(getNameExpanded()).toBe(true);
+ });
+ });
+
+ it("does not re-expand a section the user collapsed, even though the URL filter is still active", async () => {
+ // Filter already active and already auto-expanded once in this session.
+ queryParamStore.set("filter", encodedFilterA);
+ sessionStorage.setItem(EXPANDED_STORAGE_KEY, JSON.stringify(""));
+ sessionStorage.setItem(SEEDED_STORAGE_KEY, JSON.stringify("name"));
+
+ render(<ExpansionHarness />);
+
+ // The seeded marker records that "name" was already reconciled, so the
+ // collapsed state is preserved instead of being re-expanded.
+ await waitFor(() => {
+ expect(screen.getByTestId("expanded-list").textContent).toBe("[]");
+ });
+ expect(getNameExpanded()).toBe(false);
+ });
+
+ it("keeps a manual collapse after it is reconciled within the same mount", async () => {
+ queryParamStore.set("filter", encodedFilterA);
+
+ render(<ExpansionHarness />);
+
+ // Auto-expanded on arrival.
+ await waitFor(() => {
+ expect(getNameExpanded()).toBe(true);
+ });
+
+ // User collapses it.
+ fireEvent.click(screen.getByTestId("collapse-name"));
+
+ // It stays collapsed; the still-active filter does not re-expand it.
+ await waitFor(() => {
+ expect(getNameExpanded()).toBe(false);
+ });
+ // Give any stray re-render a chance to (incorrectly) re-seed.
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ expect(getNameExpanded()).toBe(false);
+ });
+});
+
describe("useSidebarFilterState session persistence", () => {
const encodedFilterA = encodeFiltersGeneric(FILTER_A);
const encodedFilterB = encodeFiltersGeneric(FILTER_B);