Skip to content

Build your personal knowledge base with Trilium Notes

fix(dashboard): prune layout entries for notes no longer children

Elian Doranc18ffd90949d
diff --git a/apps/client/src/widgets/collections/dashboard/index.tsx b/apps/client/src/widgets/collections/dashboard/index.tsx
index 95f52b75c3..d043c1487e 100644
--- a/apps/client/src/widgets/collections/dashboard/index.tsx
+++ b/apps/client/src/widgets/collections/dashboard/index.tsx
@@ -92,7 +92,8 @@ function useIsCollapsed(containerRef: RefObject<HTMLElement>) {
* layout and external changes arriving through the viewConfig prop. Returns the bits the grid
* lifecycle needs: a `persistLayout(grid)` to call after the grid mutates, and `savedWidgetsRef`,
* the last-known persisted layout used to position widgets as they're created. */
-function useDashboardLayoutPersistence({ viewConfig, saveConfig, gridRef, containerRef }: {
+function useDashboardLayoutPersistence({ note, viewConfig, saveConfig, gridRef, containerRef }: {
+ note: FNote;
viewConfig: DashboardViewConfig | undefined;
saveConfig: (config: DashboardViewConfig) => void;
gridRef: MutableRef<GridStack | null>;
@@ -122,7 +123,10 @@ function useDashboardLayoutPersistence({ viewConfig, saveConfig, gridRef, contai
present[node.id] = { x: node.x ?? 0, y: node.y ?? 0, w: node.w ?? 1, h: node.h ?? 1 };
}
}
- const widgets = reconcilePersistedLayout(savedWidgetsRef.current, present);
+ // The grid only carries the widgets currently shown; pass the dashboard's full child set so
+ // hidden (e.g. archived) widgets keep their saved geometry while widgets whose note is no
+ // longer a child are pruned instead of lingering in the persisted layout forever.
+ const widgets = reconcilePersistedLayout(savedWidgetsRef.current, present, new Set(note.children));
if (sameLayout(widgets, savedWidgetsRef.current)) {
return;
}
@@ -184,7 +188,7 @@ function useDashboardGrid({ note, notes, viewConfig, saveConfig, containerRef, g
dropPositionsRef: MutableRef<WidgetLayouts>;
isCollapsed: boolean;
}) {
- const { persistLayout, savedWidgetsRef } = useDashboardLayoutPersistence({ viewConfig, saveConfig, gridRef, containerRef });
+ const { persistLayout, savedWidgetsRef } = useDashboardLayoutPersistence({ note, viewConfig, saveConfig, gridRef, containerRef });
// Initialize the grid once per parent note.
useLayoutEffect(() => {
diff --git a/apps/client/src/widgets/collections/dashboard/layout.spec.ts b/apps/client/src/widgets/collections/dashboard/layout.spec.ts
index 5cf7cd2ccb..17d4cc977f 100644
--- a/apps/client/src/widgets/collections/dashboard/layout.spec.ts
+++ b/apps/client/src/widgets/collections/dashboard/layout.spec.ts
@@ -57,17 +57,32 @@ describe("reconcilePersistedLayout", () => {
// Archived notes hidden: only the normal widget remains in the grid. Persisting the layout
// now must not drop the archived widget's geometry just because it is no longer rendered —
- // otherwise re-showing it would auto-position it instead of restoring its placement.
- const afterHide = reconcilePersistedLayout(shown, { normal });
+ // it is still a child of the dashboard, so re-showing it should restore its placement
+ // instead of auto-positioning it.
+ const afterHide = reconcilePersistedLayout(shown, { normal }, new Set([ "archived", "normal" ]));
expect(afterHide).toEqual({ archived, normal });
});
it("applies position changes and additions coming from the grid", () => {
const previous: WidgetLayouts = { a: normal };
- const next = reconcilePersistedLayout(previous, { a: { ...normal, x: 6 }, b: archived });
+ const next = reconcilePersistedLayout(previous, { a: { ...normal, x: 6 }, b: archived }, new Set([ "a", "b" ]));
// Moved widgets win over their previous geometry, and brand-new widgets are included.
expect(next).toEqual({ a: { ...normal, x: 6 }, b: archived });
});
+
+ it("prunes the saved position of a widget whose note is no longer a child of the dashboard", () => {
+ // `removed` is absent from the grid AND no longer a live child (its branch was deleted) —
+ // its stale geometry must be dropped so the persisted layout doesn't grow unbounded.
+ const previous: WidgetLayouts = { normal, removed: archived };
+ const next = reconcilePersistedLayout(previous, { normal }, new Set([ "normal" ]));
+ expect(next).toEqual({ normal });
+ });
+
+ it("keeps a hidden child but prunes a removed note in the same pass", () => {
+ const previous: WidgetLayouts = { normal, hidden: archived, removed: { x: 8, y: 8, w: 1, h: 1 } };
+ const next = reconcilePersistedLayout(previous, { normal }, new Set([ "normal", "hidden" ]));
+ expect(next).toEqual({ normal, hidden: archived });
+ });
});
describe("sameLayout", () => {
diff --git a/apps/client/src/widgets/collections/dashboard/layout.ts b/apps/client/src/widgets/collections/dashboard/layout.ts
index 21e27b71ac..d71e6a7112 100644
--- a/apps/client/src/widgets/collections/dashboard/layout.ts
+++ b/apps/client/src/widgets/collections/dashboard/layout.ts
@@ -34,11 +34,20 @@ export function computeDropCell(grid: GridStack, container: HTMLElement, e: { cl
* previously-persisted layout (`previous`), producing the layout to persist next.
*
* A widget can be absent from `present` not only because it was removed from the dashboard, but
- * also because it was filtered out of view (e.g. archived notes hidden). Its saved position must be
- * retained so that toggling it back restores its placement instead of auto-positioning it.
+ * also because it was filtered out of view (e.g. archived notes hidden). To tell the two apart we
+ * take `liveNoteIds` — the note IDs that are still children of the dashboard. A saved position is
+ * retained only while its note is still a live child (so toggling a hidden widget back restores its
+ * placement); the geometry of notes that are no longer children is pruned so the persisted layout
+ * doesn't accumulate stale entries over repeated remove-and-add cycles.
*/
-export function reconcilePersistedLayout(previous: WidgetLayouts, present: WidgetLayouts): WidgetLayouts {
- return { ...previous, ...present };
+export function reconcilePersistedLayout(previous: WidgetLayouts, present: WidgetLayouts, liveNoteIds: Set<string>): WidgetLayouts {
+ const result: WidgetLayouts = { ...present };
+ for (const [ noteId, layout ] of Object.entries(previous)) {
+ if (!(noteId in result) && liveNoteIds.has(noteId)) {
+ result[noteId] = layout;
+ }
+ }
+ return result;
}
/** Whether two layouts describe the same widgets in the same positions. */