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
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ export function hasSavedColumnVisibility(columns: null | Record<string, boolean>
return columns != null;
}

export function savedViewColumnsEqual(
a: ColumnVisibilityState | undefined,
b: null | Record<string, boolean> | undefined,
defaultColumnVisibility: ColumnVisibilityState = {}
): boolean {
const normalize = (value: ColumnVisibilityState | null | undefined) => ({ ...defaultColumnVisibility, ...(value ?? {}) });
const aEntries = Object.entries(normalize(a)).sort(([k1], [k2]) => k1.localeCompare(k2));
const bEntries = Object.entries(normalize(b)).sort(([k1], [k2]) => k1.localeCompare(k2));

if (aEntries.length !== bEntries.length) {
return false;
}

return aEntries.every(([k, v], i) => {
const bEntry = bEntries[i];
return bEntry !== undefined && bEntry[0] === k && bEntry[1] === v;
});
}

export function setSortQueryParam(queryParams: SavedViewQueryParams, value: null | string): void {
if (supportsSortQueryParam(queryParams)) {
queryParams.sort = value;
Expand Down Expand Up @@ -256,7 +275,7 @@ export function useSavedViews(options: UseSavedViewsOptions): UseSavedViewsRetur
if (
options.getColumnVisibility &&
hasSavedColumnVisibility(view.columns) &&
!columnsEqual(options.getColumnVisibility(), view.columns, options.defaultColumnVisibility)
!savedViewColumnsEqual(options.getColumnVisibility(), view.columns, options.defaultColumnVisibility)
) {
return true;
}
Expand Down Expand Up @@ -364,25 +383,6 @@ function columnOrderEqual(a: ColumnOrderState | undefined, b: null | string[] |
return aOrder.every((columnId, index) => columnId === bOrder[index]);
}

function columnsEqual(
a: ColumnVisibilityState | undefined,
b: null | Record<string, boolean> | undefined,
defaultColumnVisibility: ColumnVisibilityState = {}
): boolean {
const normalize = (value: ColumnVisibilityState | null | undefined) => ({ ...defaultColumnVisibility, ...(value ?? {}) });
const aEntries = Object.entries(normalize(a)).sort(([k1], [k2]) => k1.localeCompare(k2));
const bEntries = Object.entries(normalize(b)).sort(([k1], [k2]) => k1.localeCompare(k2));

if (aEntries.length !== bEntries.length) {
return false;
}

return aEntries.every(([k, v], i) => {
const bEntry = bEntries[i];
return bEntry !== undefined && bEntry[0] === k && bEntry[1] === v;
});
}

function normalizeFilterDefinitions(value: null | string | undefined): string {
if (!value) {
return '[]';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
hasMissingSavedViewSlug,
hasSavedColumnOrder,
hasSavedColumnVisibility,
savedViewColumnsEqual,
type SavedViewQueryParams,
setSortQueryParam,
setTimeQueryParam,
Expand Down Expand Up @@ -211,6 +212,32 @@ describe('useSavedViews', () => {
expect(hasSavedColumnVisibility({ events: false })).toBe(true);
});

it('treats legacy visibility missing default-hidden columns as unchanged', () => {
// Arrange
const current = { project: false, summary: true, tags: false };
const legacySaved = { summary: true };
const defaults = { project: false, tags: false };

// Act
const result = savedViewColumnsEqual(current, legacySaved, defaults);

// Assert
expect(result).toBe(true);
});

it('detects a changed column after applying default visibility', () => {
// Arrange
const current = { project: true, summary: true, tags: false };
const legacySaved = { summary: true };
const defaults = { project: false, tags: false };

// Act
const result = savedViewColumnsEqual(current, legacySaved, defaults);

// Assert
expect(result).toBe(false);
});

it('does not compare column order when a saved view omits or clears column order', () => {
// Act & Assert
expect(hasSavedColumnOrder(null)).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
let showChart = $state(true);
const savedViewsState = useSavedViews({
baseHref: resolve('/(app)/stack'),
defaultColumnVisibility: defaultStackColumnVisibility,
defaultFilter: DEFAULT_FILTER,
defaultTime: DEFAULT_TIME_RANGE,
filterCacheKey,
Expand Down
Loading