Skip to content

πŸͺ’ Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(users): stop the column order and visibility keys colliding (#17445)

Nikita Kabardin4dbad957e681
diff --git a/web/src/components/table/data-table-column-visibility-filter.clienttest.tsx b/web/src/components/table/data-table-column-visibility-filter.clienttest.tsx
index 26d18730f1..304e99aa7d 100644
--- a/web/src/components/table/data-table-column-visibility-filter.clienttest.tsx
+++ b/web/src/components/table/data-table-column-visibility-filter.clienttest.tsx
@@ -2,6 +2,10 @@ import { StrictMode, useState } from "react";
import { fireEvent, render, screen } from "@testing-library/react";
import { type VisibilityState } from "@tanstack/react-table";
import { DataTableColumnVisibilityFilter } from "@/src/components/table/data-table-column-visibility-filter";
+import {
+ useColumnOrder,
+ useColumnVisibility,
+} from "@/src/features/column-visibility";
import { LAYER_ORDER } from "@/src/components/ui/layer";
import { type LangfuseColumnDef } from "@/src/components/table/types";
@@ -132,6 +136,36 @@ function DefaultSettingsHarness({
);
}
+// The picker as the app wires it: both pieces of column state come from local
+// storage through their hooks, rather than from useState.
+function StoredStateHarness({
+ orderKey,
+ visibilityKey,
+}: {
+ orderKey: string;
+ visibilityKey: string;
+}) {
+ const [columnVisibility, setColumnVisibility] = useColumnVisibility<{
+ id: string;
+ }>(visibilityKey, groupedColumns);
+ const [columnOrder, setColumnOrder] = useColumnOrder<{ id: string }>(
+ orderKey,
+ groupedColumns,
+ );
+
+ return (
+ <DataTableColumnVisibilityFilter
+ columns={groupedColumns}
+ columnVisibility={columnVisibility}
+ setColumnVisibility={setColumnVisibility}
+ columnOrder={columnOrder}
+ setColumnOrder={setColumnOrder}
+ tableName="experiments"
+ isV4={true}
+ />
+ );
+}
+
function installOverlayLayers() {
const overlayRoot = document.createElement("div");
overlayRoot.setAttribute("data-overlay-root", "");
@@ -171,6 +205,7 @@ describe("DataTableColumnVisibilityFilter", () => {
});
beforeEach(() => {
+ localStorage.clear();
h.capture.mockClear();
h.onColumnGroupToggle.mockClear();
installOverlayLayers();
@@ -304,6 +339,24 @@ describe("DataTableColumnVisibilityFilter", () => {
).toBeNull();
});
+ // A visibility map under the order key β€” what a local storage key shared
+ // between the two hooks left behind β€” reached `columnIdsOrder.map` and threw
+ // "map is not a function" as the popover rendered.
+ it("renders with a stored column order that is not an array", () => {
+ localStorage.setItem("storedOrder", JSON.stringify({ name: true }));
+
+ render(
+ <StoredStateHarness
+ orderKey="storedOrder"
+ visibilityKey="storedVisibility"
+ />,
+ );
+ fireEvent.click(screen.getByRole("button", { name: /^Columns/ }));
+
+ expect(screen.getByRole("checkbox", { name: "Name" })).toBeChecked();
+ expect(screen.getByText("Trace Item Scores")).toBeVisible();
+ });
+
it("offers reset for comparison content settings even with default columns", () => {
render(<DefaultSettingsHarness />);
fireEvent.click(screen.getByRole("button", { name: /^Columns/ }));
diff --git a/web/src/components/table/data-table-column-visibility-filter.tsx b/web/src/components/table/data-table-column-visibility-filter.tsx
index 1f75cb13d6..a117723d31 100644
--- a/web/src/components/table/data-table-column-visibility-filter.tsx
+++ b/web/src/components/table/data-table-column-visibility-filter.tsx
@@ -417,7 +417,11 @@ export function DataTableColumnVisibilityFilter<TData, TValue>({
);
const { count, total } = calculateColumnCounts(columns, columnVisibility);
- const columnIdsOrder = columnOrder ?? columns.map((col) => col.accessorKey);
+ // Shape-checked, not just nullish-checked: ~30 tables share this picker and
+ // a persisted order can come back as anything.
+ const columnIdsOrder = Array.isArray(columnOrder)
+ ? columnOrder
+ : columns.map((col) => col.accessorKey);
const isColumnOrderingEnabled = !!setColumnOrder;
const hasOrderChanges =
isColumnOrderingEnabled &&
diff --git a/web/src/features/column-visibility/hooks/useColumnOrder.clienttest.ts b/web/src/features/column-visibility/hooks/useColumnOrder.clienttest.ts
new file mode 100644
index 0000000000..fa5db23e6c
--- /dev/null
+++ b/web/src/features/column-visibility/hooks/useColumnOrder.clienttest.ts
@@ -0,0 +1,59 @@
+import { globSync, readFileSync } from "node:fs";
+import { join } from "node:path";
+
+// Matches `useColumnOrder("key"` and `useColumnVisibility<Row>(\n `k-${id}`,`,
+// capturing the key expression as written.
+const HOOK_CALL =
+ /(useColumnOrder|useColumnVisibility)\s*(?:<[^>()]*>)?\s*\(\s*([^,\n]+)/g;
+
+const SRC = join(import.meta.dirname, "../../..");
+
+// Normalize a captured key expression so `"users"` and `'users'` collide.
+const normalizeKeyExpression = (key: string): string => {
+ const trimmed = key.trim();
+ const quote = trimmed[0];
+ if (
+ (quote === '"' || quote === "'" || quote === "`") &&
+ trimmed.length >= 2 &&
+ trimmed[trimmed.length - 1] === quote
+ ) {
+ return trimmed.slice(1, -1);
+ }
+ return trimmed;
+};
+
+describe("useColumnOrder", () => {
+ // The Users table passed one key to both hooks. They persist incompatible
+ // shapes (a string list vs an object) and useLocalStorage broadcasts every
+ // write into the other's state, so the column picker ran `.map` on an
+ // object. The guards in both hooks stop that from throwing; this stops a
+ // key-tidying refactor from re-creating the collision.
+ it("shares no localStorage key with useColumnVisibility", () => {
+ const keys = {
+ useColumnOrder: new Set<string>(),
+ useColumnVisibility: new Set<string>(),
+ };
+
+ const files = globSync("**/*.{ts,tsx}", { cwd: SRC }).filter(
+ // The hooks' own signatures, and tests that pass keys on purpose.
+ (file) =>
+ !file.startsWith("features/column-visibility/") &&
+ !file.includes(".clienttest."),
+ );
+
+ for (const file of files) {
+ const source = readFileSync(join(SRC, file), "utf8");
+ for (const [, hook, key] of source.matchAll(HOOK_CALL)) {
+ keys[hook as keyof typeof keys].add(normalizeKeyExpression(key));
+ }
+ }
+
+ // Guards the sweep itself: an empty scan must not read as "no collisions".
+ expect(keys.useColumnOrder.size).toBeGreaterThan(10);
+ expect(
+ [...keys.useColumnOrder].filter((key) =>
+ keys.useColumnVisibility.has(key),
+ ),
+ ).toEqual([]);
+ });
+});
diff --git a/web/src/features/column-visibility/hooks/useColumnOrder.ts b/web/src/features/column-visibility/hooks/useColumnOrder.ts
index ee0f4dea4e..04ce1be3e0 100644
--- a/web/src/features/column-visibility/hooks/useColumnOrder.ts
+++ b/web/src/features/column-visibility/hooks/useColumnOrder.ts
@@ -1,4 +1,9 @@
-import { useEffect } from "react";
+import {
+ useCallback,
+ useEffect,
+ type Dispatch,
+ type SetStateAction,
+} from "react";
import { type LangfuseColumnDef } from "@/src/components/table/types";
import useLocalStorage from "@/src/components/useLocalStorage";
import {
@@ -7,6 +12,10 @@ import {
type OneTimeMigration,
} from "@/src/features/column-visibility/lib/one-time-migration";
+// Stable identity, so a missing or unusable stored value does not make a new
+// array on every render (the value is a dependency of the effect below).
+const EMPTY_COLUMN_ORDER: string[] = [];
+
// returns deep copy of local storage object
const readStoredColumnOrder = (localStorageKey: string): string[] => {
if (typeof window === "undefined") {
@@ -14,7 +23,12 @@ const readStoredColumnOrder = (localStorageKey: string): string[] => {
}
try {
const storedValue = localStorage.getItem(localStorageKey);
- return storedValue ? JSON.parse(storedValue) : [];
+ if (!storedValue) return [];
+ const parsed: unknown = JSON.parse(storedValue);
+ // Local storage is hand-editable and keys get reused, so the parsed value
+ // can be any shape. Anything but a list of column ids is discarded.
+ if (!Array.isArray(parsed)) return [];
+ return parsed.filter((id): id is string => typeof id === "string");
} catch (error) {
console.warn("Error reading from local storage", error);
return [];
@@ -26,9 +40,29 @@ function useColumnOrder<TData>(
columns: LangfuseColumnDef<TData>[],
migrations?: OneTimeMigration<string[]>[],
) {
- const [columnOrder, setColumnOrder] = useLocalStorage<string[]>(
+ const [storedColumnOrder, setStoredColumnOrder] = useLocalStorage<string[]>(
localStorageKey,
- [],
+ EMPTY_COLUMN_ORDER,
+ );
+ // useLocalStorage hands back whatever the key holds β€” its cross-tab and
+ // same-tab listeners parse and set without checking β€” so coerce here: the
+ // value this hook exposes is always an array. The effect below rewrites the
+ // key, which heals a wrong-shaped one.
+ const columnOrder = Array.isArray(storedColumnOrder)
+ ? storedColumnOrder
+ : EMPTY_COLUMN_ORDER;
+
+ // The setter is coerced too: callers pass an updater (the picker's drag
+ // handler calls `.indexOf` on the previous order), and an updater reads the
+ // raw stored value rather than the one above.
+ const setColumnOrder = useCallback<Dispatch<SetStateAction<string[]>>>(
+ (next) =>
+ setStoredColumnOrder((previous) =>
+ typeof next === "function"
+ ? next(Array.isArray(previous) ? previous : EMPTY_COLUMN_ORDER)
+ : next,
+ ),
+ [setStoredColumnOrder],
);
useEffect(() => {
diff --git a/web/src/features/column-visibility/hooks/useColumnVisibility.clienttest.ts b/web/src/features/column-visibility/hooks/useColumnVisibility.clienttest.ts
new file mode 100644
index 0000000000..7743430bfe
--- /dev/null
+++ b/web/src/features/column-visibility/hooks/useColumnVisibility.clienttest.ts
@@ -0,0 +1,76 @@
+import { renderHook } from "@testing-library/react";
+import { type LangfuseColumnDef } from "@/src/components/table/types";
+import useColumnVisibility from "@/src/features/column-visibility/hooks/useColumnVisibility";
+
+const columns: LangfuseColumnDef<{ id: string }>[] = [
+ { accessorKey: "name", header: "Name", enableHiding: true },
+ {
+ accessorKey: "input",
+ header: "Input",
+ enableHiding: true,
+ defaultHidden: true,
+ },
+];
+
+describe("useColumnVisibility", () => {
+ beforeEach(() => {
+ localStorage.clear();
+ });
+
+ // A column ORDER (a list of ids) under a visibility key is what a key shared
+ // between useColumnVisibility and useColumnOrder used to leave behind. Read
+ // back as a visibility map it carried no visibility at all.
+ it("falls back to the column defaults when the stored value is an array", () => {
+ localStorage.setItem("visibilityKey", JSON.stringify(["name", "input"]));
+
+ const { result } = renderHook(() =>
+ useColumnVisibility("visibilityKey", columns),
+ );
+
+ expect(result.current[0]).toEqual({ name: true, input: false });
+ });
+
+ // What the affected browsers actually hold: toggling a column while the
+ // visibility state was a column-order array spread that array into the
+ // object (`{...["name","input"]}`). Those entries are not visibility, and
+ // nothing else prunes them β€” a saved view rejects them permanently.
+ it("drops entries whose value is not a boolean and rewrites the key", () => {
+ localStorage.setItem(
+ "visibilityKey",
+ JSON.stringify({ "0": "name", "1": "input", input: false }),
+ );
+
+ const { result } = renderHook(() =>
+ useColumnVisibility("visibilityKey", columns),
+ );
+
+ expect(result.current[0]).toEqual({ name: true, input: false });
+ expect(JSON.parse(localStorage.getItem("visibilityKey") ?? "null")).toEqual(
+ {
+ name: true,
+ input: false,
+ },
+ );
+ });
+
+ // A known column whose stored value is not a boolean must not be copied back
+ // into the repaired state β€” only boolean preferences survive.
+ it("ignores non-boolean values for known columns when repairing", () => {
+ localStorage.setItem(
+ "visibilityKey",
+ JSON.stringify({ name: "name", input: false }),
+ );
+
+ const { result } = renderHook(() =>
+ useColumnVisibility("visibilityKey", columns),
+ );
+
+ expect(result.current[0]).toEqual({ name: true, input: false });
+ expect(JSON.parse(localStorage.getItem("visibilityKey") ?? "null")).toEqual(
+ {
+ name: true,
+ input: false,
+ },
+ );
+ });
+});
diff --git a/web/src/features/column-visibility/hooks/useColumnVisibility.ts b/web/src/features/column-visibility/hooks/useColumnVisibility.ts
index c41c51ba5d..99c0839f54 100644
--- a/web/src/features/column-visibility/hooks/useColumnVisibility.ts
+++ b/web/src/features/column-visibility/hooks/useColumnVisibility.ts
@@ -9,6 +9,16 @@ import {
type OneTimeMigration,
} from "@/src/features/column-visibility/lib/one-time-migration";
+// Stable identity for the fallback, so an unusable stored value does not make
+// a new object on every render (the value is a dependency of the effect below).
+const EMPTY_VISIBILITY_STATE: VisibilityState = {};
+
+// Local storage is hand-editable and keys get reused, so a parsed value can be
+// any shape β€” an array, null, a primitive β€” and every consumer here indexes it
+// or runs `in` against it.
+const isVisibilityState = (value: unknown): value is VisibilityState =>
+ typeof value === "object" && value !== null && !Array.isArray(value);
+
// returns deep copy of local storage object
const readStoredVisibilityState = (
localStorageKey: string,
@@ -18,7 +28,17 @@ const readStoredVisibilityState = (
}
try {
const storedValue = localStorage.getItem(localStorageKey);
- return storedValue ? JSON.parse(storedValue) : {};
+ if (!storedValue) return {};
+ const parsed: unknown = JSON.parse(storedValue);
+ if (!isVisibilityState(parsed)) return {};
+ // Values matter as much as the shape: entries whose value is not a boolean
+ // are not column visibility, and they outlive the object they came from
+ // (nothing else prunes them) until a saved view rejects them.
+ return Object.fromEntries(
+ Object.entries(parsed).filter(
+ ([, visible]) => typeof visible === "boolean",
+ ),
+ ) as VisibilityState;
} catch (error) {
console.error("Error reading from local storage", error);
return {};
@@ -57,14 +77,31 @@ function useColumnVisibility<TData>(
return visibilityState;
};
- const [columnVisibility, setColumnVisibility] =
+ const [storedColumnVisibility, setColumnVisibility] =
useLocalStorage<VisibilityState>(localStorageKey, initialVisibilityState());
+ // useLocalStorage hands back whatever the key holds β€” its cross-tab and
+ // same-tab listeners parse and set without checking β€” so coerce here: the
+ // value this hook exposes is always an object of boolean entries. The effect
+ // below rewrites the key, which heals a wrong-shaped or poisoned one.
+ const columnVisibility = (() => {
+ if (!isVisibilityState(storedColumnVisibility)) {
+ return EMPTY_VISIBILITY_STATE;
+ }
+ const booleanEntries = Object.entries(storedColumnVisibility).filter(
+ ([, visible]) => typeof visible === "boolean",
+ );
+ if (booleanEntries.length === Object.keys(storedColumnVisibility).length) {
+ return storedColumnVisibility;
+ }
+ return Object.fromEntries(booleanEntries) as VisibilityState;
+ })();
useEffect(() => {
let initialColumnVisibility = initialVisibilityState();
Object.keys(initialColumnVisibility).forEach((key) => {
- if (Object.hasOwn(columnVisibility, key)) {
- initialColumnVisibility[key] = columnVisibility[key];
+ const stored = columnVisibility[key];
+ if (typeof stored === "boolean") {
+ initialColumnVisibility[key] = stored;
}
});
diff --git a/web/src/features/users/UsersPage.tsx b/web/src/features/users/UsersPage.tsx
index 5f4da767ae..24c16e9dad 100644
--- a/web/src/features/users/UsersPage.tsx
+++ b/web/src/features/users/UsersPage.tsx
@@ -541,8 +541,11 @@ const UsersTable = ({
"users",
columns,
);
+ // Its own key: "users" is the visibility state's, and one key cannot hold
+ // two shapes (an object and a string list) without them overwriting each
+ // other and being read back as the wrong type.
const [columnOrder, setColumnOrder] = useColumnOrder<RowData>(
- "users",
+ "usersColumnOrder",
columns,
);
const { isLoading: isViewLoading, ...viewControllers } = useTableViewManager({