Skip to content

Build your personal knowledge base with Trilium Notes

feat(collections/dashboard): respond to external changes

Elian Doran3ab69bfea5d2
diff --git a/apps/client/src/widgets/collections/dashboard/index.tsx b/apps/client/src/widgets/collections/dashboard/index.tsx
index c7e8977af5..ed01a6ad1f 100644
--- a/apps/client/src/widgets/collections/dashboard/index.tsx
+++ b/apps/client/src/widgets/collections/dashboard/index.tsx
@@ -11,11 +11,12 @@ import linkContextMenuService from "../../../menus/link_context_menu";
import froca from "../../../services/froca";
import CollectionProperties from "../../note_bars/CollectionProperties";
import ActionButton from "../../react/ActionButton";
-import { useNoteLabelBoolean, useSpacedUpdate } from "../../react/hooks";
+import { useNoteLabelBoolean, useSpacedUpdate, useTriliumEvent } from "../../react/hooks";
import Icon from "../../react/Icon";
import NoteLink from "../../react/NoteLink";
import { ViewModeProps } from "../interface";
import { getNotePath, NoteContent } from "../legacy/ListOrGridView";
+import ViewModeStorage from "../view_mode_storage";
interface DashboardWidgetLayout {
x: number;
@@ -30,6 +31,7 @@ export interface DashboardViewConfig {
const DEFAULT_WIDGET_SIZE = { w: 4, h: 3 };
const INITIALIZED_CLASS = "dashboard-widget-initialized";
+const CONFIG_ATTACHMENT_TITLE = "dashboard.json";
export default function DashboardView({ note, noteIds, viewConfig, saveConfig, highlightedTokens, showTextRepresentation }: ViewModeProps<DashboardViewConfig>) {
const [ notes, setNotes ] = useState<FNote[]>([]);
@@ -124,6 +126,41 @@ export default function DashboardView({ note, noteIds, viewConfig, saveConfig, h
persistLayout(grid);
}, [ notes ]);
+ // React to external changes of the layout attachment (e.g. the same dashboard open in
+ // another split or synced from another instance).
+ useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
+ if (!loadResults.getAttachmentRows().some((att) => att.ownerId === note.noteId && att.title === CONFIG_ATTACHMENT_TITLE)) {
+ return;
+ }
+ new ViewModeStorage<DashboardViewConfig>(note, "dashboard").restore().then((config) => {
+ const widgets = config?.widgets ?? {};
+ if (sameLayout(widgets, savedWidgetsRef.current)) {
+ // Our own save echoing back, or no effective change.
+ return;
+ }
+ savedWidgetsRef.current = widgets;
+ applyLayout(widgets);
+ });
+ });
+
+ function applyLayout(widgets: Record<string, DashboardWidgetLayout>) {
+ const grid = gridRef.current;
+ const container = containerRef.current;
+ if (!grid || !container) return;
+
+ grid.batchUpdate();
+ try {
+ for (const el of container.querySelectorAll<HTMLElement>(`.grid-stack-item.${INITIALIZED_CLASS}`)) {
+ const layout = el.dataset.noteId ? widgets[el.dataset.noteId] : undefined;
+ if (layout) {
+ grid.update(el, layout);
+ }
+ }
+ } finally {
+ grid.batchUpdate(false);
+ }
+ }
+
return (
<div className="dashboard-view">
<CollectionProperties note={note} />