Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: table sorting not persisting across reloads (#1721)
Kyle Mendell9d950756e382
diff --git a/frontend/src/lib/components/arcane-table/arcane-table.svelte b/frontend/src/lib/components/arcane-table/arcane-table.svelteindex c17ce71be..f2aaf8a6f 100644--- a/frontend/src/lib/components/arcane-table/arcane-table.svelte+++ b/frontend/src/lib/components/arcane-table/arcane-table.svelte@@ -27,6 +27,7 @@encodeHidden,applyHiddenPatch,encodeFilters,+ encodeSort,encodeMobileVisibility,buildMobileVisibility,type BulkAction@@ -195,6 +196,19 @@requestOptions = { ...requestOptions, pagination: { page: 1, limit: persistedLimit } };shouldRefresh = true;}++ const persistedSort = snapshot.sort;+ const currentSort = requestOptions?.sort;+ if (persistedSort) {+ if (currentSort?.column !== persistedSort.column || currentSort?.direction !== persistedSort.direction) {+ requestOptions = {+ ...requestOptions,+ sort: persistedSort,+ pagination: { page: 1, limit: requestOptions?.pagination?.limit ?? getEffectiveLimit() }+ };+ shouldRefresh = true;+ }+ }if (shouldRefresh) onRefresh(requestOptions);if (mobileFields.length && !Object.keys(mobileFieldVisibility).length) {@@ -406,10 +420,19 @@const next = typeof updater === 'function' ? updater(sorting) : updater;sorting = next;const first = next[0];- if (first) {+ const sortState = first+ ? { column: String(first.id), direction: (first.desc ? 'desc' : 'asc') as 'asc' | 'desc' }+ : undefined;+ if (enablePersist && prefs) {+ prefs.current = {+ ...prefs.current,+ s: encodeSort(sortState)+ };+ }+ if (sortState) {requestOptions = {...requestOptions,- sort: { column: String(first.id), direction: first.desc ? 'desc' : 'asc' },+ sort: sortState,pagination: {page: 1,limit: requestOptions?.pagination?.limit ?? items?.pagination?.itemsPerPage ?? 10diff --git a/frontend/src/lib/components/arcane-table/arcane-table.types.svelte.ts b/frontend/src/lib/components/arcane-table/arcane-table.types.svelte.tsindex ec5143f03..e0304bf48 100644--- a/frontend/src/lib/components/arcane-table/arcane-table.types.svelte.ts+++ b/frontend/src/lib/components/arcane-table/arcane-table.types.svelte.ts@@ -8,6 +8,9 @@ export type FieldSpec = {};export type MobileFieldVisibility = Record<string, boolean>;+export type SortDirection = 'asc' | 'desc';+export type SortPreference = [string, SortDirection];+export type SortState = { column: string; direction: SortDirection };export type ColumnWidth = 'auto' | 'min' | 'max' | number;export type ColumnAlign = 'left' | 'center' | 'right';@@ -36,6 +39,8 @@ export type CompactTablePrefs = {f?: [string, unknown][];// g: global filter stringg?: string;+ // s: sort as [column, direction]+ s?: SortPreference;// l: page size (limit)l?: number;// m: list of hidden mobile field ids@@ -67,6 +72,19 @@ export function decodeFilters(pairs?: [string, unknown][]): ColumnFiltersState {return pairs.map(([id, value]) => ({ id, value }));}+export function encodeSort(sort?: SortState): SortPreference | undefined {+ if (!sort?.column) return undefined;+ return [sort.column, sort.direction];+}++export function decodeSort(value: unknown): SortState | undefined {+ if (!Array.isArray(value) || value.length !== 2) return undefined;+ const [column, direction] = value;+ if (typeof column !== 'string') return undefined;+ if (direction !== 'asc' && direction !== 'desc') return undefined;+ return { column, direction };+}+export function encodeMobileVisibility(visibility: Record<string, boolean>): string[] {const encoded: string[] = [];for (const [id, visible] of Object.entries(visibility)) {diff --git a/frontend/src/lib/components/arcane-table/arcane-table.utils.ts b/frontend/src/lib/components/arcane-table/arcane-table.utils.tsindex 890368ae3..1a77b5264 100644--- a/frontend/src/lib/components/arcane-table/arcane-table.utils.ts+++ b/frontend/src/lib/components/arcane-table/arcane-table.utils.ts@@ -1,13 +1,14 @@import type { ColumnFiltersState } from '@tanstack/table-core';import type { FilterMap } from '$lib/types/pagination.type';import type { CompactTablePrefs } from './arcane-table.types.svelte';-import { decodeFilters } from './arcane-table.types.svelte';+import { decodeFilters, decodeSort } from './arcane-table.types.svelte';export type PersistedPreferencesSnapshot = {hiddenColumns: string[];restoredFilters: ColumnFiltersState;filtersMap: FilterMap;search: string;+ sort?: { column: string; direction: 'asc' | 'desc' };limit: number;mobileVisibility?: string[];customSettings?: Record<string, unknown>;@@ -65,6 +66,7 @@ export function extractPersistedPreferences(const restoredFilters = decodeFilters(prefs.f);const filtersMap = toFilterMap(restoredFilters);const search = (prefs.g ?? '').trim();+ const sort = decodeSort(prefs.s);const limit = prefs.l ?? fallbackLimit;return {@@ -72,6 +74,7 @@ export function extractPersistedPreferences(restoredFilters,filtersMap,search,+ sort,limit,mobileVisibility: prefs.m,customSettings: prefs.cdiff --git a/frontend/src/lib/utils/table-persistence.util.ts b/frontend/src/lib/utils/table-persistence.util.tsindex 26e9dfa15..46065c426 100644--- a/frontend/src/lib/utils/table-persistence.util.ts+++ b/frontend/src/lib/utils/table-persistence.util.ts@@ -1,6 +1,6 @@import { browser } from '$app/environment';import { PersistedState } from 'runed';-import type { CompactTablePrefs } from '$lib/components/arcane-table/arcane-table.types.svelte';+import { decodeSort, type CompactTablePrefs } from '$lib/components/arcane-table/arcane-table.types.svelte';import type { FilterMap, FilterValue, SearchPaginationSortRequest } from '$lib/types/pagination.type';const DEFAULT_LIMIT = 20;@@ -103,6 +103,11 @@ export function resolveInitialTableRequest(if (limit !== undefined && base.pagination?.limit !== limit) {base.pagination = { page: 1, limit };}++ const sort = decodeSort(current.s);+ if (sort) {+ base.sort = sort;+ }} catch (error) {return base;}