Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(views): migrate a raw DataGrid cfg before the envelope normalises it

Juan Denis70ed72d01ca8
diff --git a/frontend/src/components/ds/grid/useGridConfig.js b/frontend/src/components/ds/grid/useGridConfig.js
index 9c52bc7b..0e2cc01e 100644
--- a/frontend/src/components/ds/grid/useGridConfig.js
+++ b/frontend/src/components/ds/grid/useGridConfig.js
@@ -1,6 +1,6 @@
import { useCallback, useMemo, useState } from 'react';
import useGridViews from './useGridViews';
-import { cfgToEnvelope, envelopeToCfg } from './viewState';
+import { cfgToEnvelope, envelopeToCfg, isLegacyCfg } from './viewState';
import { emptyValueFor, isFilterable, OPS, ruleId } from './fields';
// The whole of a grid's chrome is ONE serialisable object, so a saved view is
@@ -83,11 +83,22 @@ export function useGridConfig({ page, columns, initial, builtinViews = [] }) {
// a dependency cycle: it needs `views`, which needs this).
const resetToBase = useCallback(() => setCfg(clone(base)), [base]);
+ // This page's built-in presets, and every view saved before the envelope
+ // existed, are raw cfg objects. Convert them on the way IN — left alone,
+ // `cols`/`sort`/`filters` look like unknown keys and get filed into the
+ // per-page bag, which drops every rule the preset declared and leaves a
+ // view that renders its name and filters nothing.
+ const migrate = useCallback(
+ (state) => (isLegacyCfg(state) ? cfgToEnvelope(state, allKeys) : state),
+ [allKeys],
+ );
+
const { views, copyLink, createView } = useGridViews({
page,
builtinViews,
capture,
apply,
+ migrate,
resetToView: resetToBase,
});
diff --git a/frontend/src/components/ds/grid/useGridViews.js b/frontend/src/components/ds/grid/useGridViews.js
index afe8e138..f61216dd 100644
--- a/frontend/src/components/ds/grid/useGridViews.js
+++ b/frontend/src/components/ds/grid/useGridViews.js
@@ -24,7 +24,14 @@ import { emptyViewState, sameViewState, toEnvelope } from './viewState';
* normalised to the envelope here, so a page never has to think about the
* shape a previous release happened to persist.
*/
-export function useGridViews({ page, builtinViews = [], capture, apply, rename, resetToView }) {
+export function useGridViews({
+ page, builtinViews = [], capture, apply, rename, resetToView,
+ // Optional pre-pass run on any INBOUND state (a built-in preset, a saved
+ // view, a shared link) before it is normalised. A host whose older states
+ // used a private shape converts it here — without this, `toEnvelope` files
+ // those unknown top-level keys into `page` and the state's rules vanish.
+ migrate,
+}) {
// `rename` is almost always an inline literal. Freeze the first one rather
// than rebuilding every callback below on each render — the map is a
// per-page constant, and a changing identity here would reach `apply`,
@@ -36,11 +43,16 @@ export function useGridViews({ page, builtinViews = [], capture, apply, rename,
// page can adopt it without rewriting its built-ins in the same commit.
// Keyed on names for the same reason useTableViews is: an inline
// `builtinViews={[…]}` literal is a new array identity every render.
+ const normalize = useCallback(
+ (state) => toEnvelope(migrate ? migrate(state) : state, { rename: renameMap }),
+ [migrate, renameMap],
+ );
+
const builtinKey = builtinViews.map((v) => v.name).join(' ');
const migratedBuiltins = useMemo(
- () => builtinViews.map((v) => ({ ...v, state: toEnvelope(v.state, { rename: renameMap }) })),
+ () => builtinViews.map((v) => ({ ...v, state: normalize(v.state) })),
// eslint-disable-next-line react-hooks/exhaustive-deps
- [builtinKey, renameMap],
+ [builtinKey, normalize],
);
const captureEnvelope = useCallback(
@@ -48,14 +60,11 @@ export function useGridViews({ page, builtinViews = [], capture, apply, rename,
[capture, renameMap],
);
- const applyEnvelope = useCallback(
- (state) => apply?.(toEnvelope(state, { rename: renameMap })),
- [apply, renameMap],
- );
+ const applyEnvelope = useCallback((state) => apply?.(normalize(state)), [apply, normalize]);
const isSame = useCallback(
- (a, b) => sameViewState(a, b, { rename: renameMap }),
- [renameMap],
+ (a, b) => sameViewState(normalize(a), normalize(b), { rename: renameMap }),
+ [normalize, renameMap],
);
const views = useTableViews({
diff --git a/frontend/src/components/ds/grid/viewState.js b/frontend/src/components/ds/grid/viewState.js
index ab2ea463..19176b5b 100644
--- a/frontend/src/components/ds/grid/viewState.js
+++ b/frontend/src/components/ds/grid/viewState.js
@@ -129,6 +129,14 @@ export function sameViewState(a, b, { rename } = {}) {
// Domains view and a Cron view are the same JSON and a link from one page's
// chrome means the same thing on the other's.
+// A view saved (or authored) as a raw DataGrid cfg rather than an envelope:
+// `cols` is a cfg-only key and `sorts` is envelope-only, so the pair is
+// unambiguous. Detecting this matters — run such a state through `toEnvelope`
+// directly and `cols`/`sort`/`filters` all get filed into `page` as unknown
+// keys, which silently drops every rule the preset declared.
+export const isLegacyCfg = (state) => !!state && typeof state === 'object'
+ && Array.isArray(state.cols) && !Array.isArray(state.sorts);
+
export function cfgToEnvelope(cfg, allKeys = []) {
const cols = Array.isArray(cfg?.cols) ? cfg.cols : [];
return {
@@ -149,9 +157,7 @@ export function cfgToEnvelope(cfg, allKeys = []) {
*/
export function envelopeToCfg(state, allKeys = [], fallback = {}) {
// A view saved by an older build is the raw cfg itself.
- const isLegacyCfg = state && typeof state === 'object'
- && Array.isArray(state.cols) && !Array.isArray(state.sorts);
- if (isLegacyCfg) {
+ if (isLegacyCfg(state)) {
return {
cols: state.cols,
sort: state.sort || { key: null, dir: 'asc' },