Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(collections/dashboard): first implementation
Elian Doranbefb299c7b66
diff --git a/apps/client/package.json b/apps/client/package.jsonindex a6e7659e07..d110c512c0 100644--- a/apps/client/package.json+++ b/apps/client/package.json@@ -53,6 +53,7 @@"dompurify": "3.4.7","draggabilly": "3.0.0","force-graph": "1.51.4",+ "gridstack": "12.6.0","htmldiff-js": "1.0.5","i18next": "26.3.1","i18next-http-backend": "4.0.0",diff --git a/apps/client/src/services/in_app_help.ts b/apps/client/src/services/in_app_help.tsindex a59a55139c..014527c5b0 100644--- a/apps/client/src/services/in_app_help.ts+++ b/apps/client/src/services/in_app_help.ts@@ -30,7 +30,8 @@ export const byBookType: Record<ViewTypeOptions, string | null> = {table: "2FvYrpmOXm29",geoMap: "81SGnPGMk7Xc",board: "CtBQqbwXDx1w",- presentation: null+ presentation: null,+ dashboard: null};export function getHelpUrlForNote(note: FNote | null | undefined) {diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex c1535f0cbe..b3bf6bfe3d 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -926,6 +926,7 @@"geo-map": "Geo Map","board": "Board","presentation": "Presentation",+ "dashboard": "Dashboard","include_archived_notes": "Show archived notes","hide_child_notes": "Hide child notes in tree","open_all_in_tabs": "Open all",diff --git a/apps/client/src/widgets/collections/NoteList.tsx b/apps/client/src/widgets/collections/NoteList.tsxindex 038d90a052..01d9a1e13e 100644--- a/apps/client/src/widgets/collections/NoteList.tsx+++ b/apps/client/src/widgets/collections/NoteList.tsx@@ -52,6 +52,9 @@ const ViewComponents: Record<ViewTypeOptions, { normal: LazyLoadedComponent, pri},presentation: {normal: lazy(() => import("./presentation/index.js"))+ },+ dashboard: {+ normal: lazy(() => import("./dashboard/index.js"))}};@@ -155,7 +158,7 @@ export function useNoteViewType(note?: FNote | null): ViewTypeOptions | undefineexport function useNoteIds(note: FNote | null | undefined, viewType: ViewTypeOptions | undefined, ntxId: string | null | undefined) {const [ noteIds, setNoteIds ] = useState<string[]>([]);const [ includeArchived ] = useNoteLabelBoolean(note, "includeArchived");- const directChildrenOnly = (viewType === "list" || viewType === "grid" || viewType === "table" || note?.type === "search");+ const directChildrenOnly = (viewType === "list" || viewType === "grid" || viewType === "table" || viewType === "dashboard" || note?.type === "search");async function refreshNoteIds() {if (!note) {diff --git a/apps/client/src/widgets/collections/dashboard/index.css b/apps/client/src/widgets/collections/dashboard/index.cssnew file mode 100644index 0000000000..f98582eac5--- /dev/null+++ b/apps/client/src/widgets/collections/dashboard/index.css@@ -0,0 +1,46 @@+.dashboard-view {+ height: 100%;+ display: flex;+ flex-direction: column;++ .dashboard-scroll-container {+ flex: 1;+ overflow-y: auto;+ position: relative;+ }++ .dashboard-widget {+ display: flex;+ flex-direction: column;+ border: 1px solid var(--main-border-color);+ border-radius: 6px;+ background: var(--main-background-color);+ overflow: hidden;++ &.archived {+ opacity: 0.6;+ }+ }++ .dashboard-widget-header {+ display: flex;+ align-items: center;+ gap: 4px;+ padding: 4px 8px;+ cursor: grab;+ border-bottom: 1px solid var(--main-border-color);++ .note-book-title {+ flex: 1;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }+ }++ .dashboard-widget-content {+ flex: 1;+ overflow: auto;+ padding: 8px;+ }+}diff --git a/apps/client/src/widgets/collections/dashboard/index.tsx b/apps/client/src/widgets/collections/dashboard/index.tsxnew file mode 100644index 0000000000..c7e8977af5--- /dev/null+++ b/apps/client/src/widgets/collections/dashboard/index.tsx@@ -0,0 +1,200 @@+import "./index.css";+import "gridstack/dist/gridstack.min.css";++import { clsx } from "clsx";+import { GridStack } from "gridstack";+import { TargetedMouseEvent } from "preact";+import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";++import FNote from "../../../entities/fnote";+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 Icon from "../../react/Icon";+import NoteLink from "../../react/NoteLink";+import { ViewModeProps } from "../interface";+import { getNotePath, NoteContent } from "../legacy/ListOrGridView";++interface DashboardWidgetLayout {+ x: number;+ y: number;+ w: number;+ h: number;+}++export interface DashboardViewConfig {+ widgets?: Record<string, DashboardWidgetLayout>;+}++const DEFAULT_WIDGET_SIZE = { w: 4, h: 3 };+const INITIALIZED_CLASS = "dashboard-widget-initialized";++export default function DashboardView({ note, noteIds, viewConfig, saveConfig, highlightedTokens, showTextRepresentation }: ViewModeProps<DashboardViewConfig>) {+ const [ notes, setNotes ] = useState<FNote[]>([]);+ const [ includeArchived ] = useNoteLabelBoolean(note, "includeArchived");+ const containerRef = useRef<HTMLDivElement>(null);+ const gridRef = useRef<GridStack | null>(null);+ // Gridstack becomes the source of truth for geometry after init; capture the saved layout once+ // since the viewConfig prop changes identity after every save.+ const savedWidgetsRef = useRef(viewConfig?.widgets ?? {});+ const pendingConfigRef = useRef<DashboardViewConfig | null>(null);+ const spacedUpdate = useSpacedUpdate(() => {+ if (pendingConfigRef.current) {+ saveConfig(pendingConfigRef.current);+ pendingConfigRef.current = null;+ }+ });++ useEffect(() => {+ froca.getNotes(noteIds).then(setNotes);+ }, [ noteIds ]);++ function persistLayout(grid: GridStack) {+ const widgets: Record<string, DashboardWidgetLayout> = {};+ for (const node of grid.engine.nodes) {+ if (typeof node.id === "string") {+ widgets[node.id] = { x: node.x ?? 0, y: node.y ?? 0, w: node.w ?? 1, h: node.h ?? 1 };+ }+ }+ if (sameLayout(widgets, savedWidgetsRef.current)) {+ return;+ }+ savedWidgetsRef.current = widgets;+ pendingConfigRef.current = { widgets };+ spacedUpdate.scheduleUpdate();+ }++ // Initialize the grid once per parent note.+ useLayoutEffect(() => {+ const container = containerRef.current;+ if (!container) return;++ const grid = GridStack.init({+ column: 12,+ cellHeight: 80,+ margin: 8,+ float: true,+ handle: ".dashboard-widget-header"+ }, container);+ gridRef.current = grid;+ grid.on("change", () => persistLayout(grid));++ return () => {+ grid.destroy(false);+ gridRef.current = null;+ };+ }, [ note ]);++ // Reconcile the rendered children with the gridstack engine: Preact owns the element lifecycle+ // (keyed by noteId), gridstack owns the geometry.+ useLayoutEffect(() => {+ const grid = gridRef.current;+ const container = containerRef.current;+ if (!grid || !container) return;++ grid.batchUpdate();+ try {+ // Drop engine nodes whose element Preact has already unmounted.+ for (const node of [ ...grid.engine.nodes ]) {+ if (node.el && !node.el.isConnected) {+ grid.removeWidget(node.el, false, false);+ }+ }++ // Promote newly rendered items to widgets, restoring their saved geometry.+ for (const el of container.querySelectorAll<HTMLElement>(`.grid-stack-item:not(.${INITIALIZED_CLASS})`)) {+ const noteId = el.dataset.noteId;+ if (!noteId) continue;+ const saved = savedWidgetsRef.current[noteId];+ grid.makeWidget(el, {+ id: noteId,+ ...DEFAULT_WIDGET_SIZE,+ ...saved,+ autoPosition: !saved+ });+ el.classList.add(INITIALIZED_CLASS);+ }+ } finally {+ grid.batchUpdate(false);+ }++ // Persist auto-assigned positions and prune entries of removed notes.+ persistLayout(grid);+ }, [ notes ]);++ return (+ <div className="dashboard-view">+ <CollectionProperties note={note} />+ <div className="dashboard-scroll-container">+ <div className="grid-stack" ref={containerRef}>+ {notes.map((childNote) => (+ <DashboardWidget+ key={childNote.noteId}+ note={childNote}+ parentNote={note}+ highlightedTokens={highlightedTokens}+ includeArchived={includeArchived}+ showTextRepresentation={showTextRepresentation}+ />+ ))}+ </div>+ </div>+ </div>+ );+}++interface DashboardWidgetProps {+ note: FNote;+ parentNote: FNote;+ highlightedTokens: string[] | null | undefined;+ includeArchived: boolean;+ showTextRepresentation?: boolean;+}++function DashboardWidget({ note, parentNote, highlightedTokens, includeArchived, showTextRepresentation }: DashboardWidgetProps) {+ const notePath = getNotePath(parentNote, note);++ /* The outer .grid-stack-item class list must stay constant across renders so that Preact never+ clobbers the classes gridstack adds there; dynamic classes go on the inner content element. */+ return (+ <div className="grid-stack-item" data-note-id={note.noteId}>+ <div className={clsx("grid-stack-item-content", "dashboard-widget", "no-tooltip-preview", note.getColorClass(), {+ "archived": note.isArchived+ })}>+ <div className="dashboard-widget-header">+ <Icon className="note-icon" icon={note.getIcon()} />+ <NoteLink className="note-book-title"+ notePath={notePath}+ noPreview+ highlightedTokens={highlightedTokens} />+ <ActionButton className="note-book-item-menu"+ icon="bx bx-dots-vertical-rounded" text=""+ onClick={(e: TargetedMouseEvent<HTMLElement>) => {+ linkContextMenuService.openContextMenu(notePath, e);+ e.stopPropagation();+ }} />+ </div>+ <div className="dashboard-widget-content">+ <NoteContent note={note}+ trim+ highlightedTokens={highlightedTokens}+ includeArchivedNotes={includeArchived}+ showTextRepresentation={showTextRepresentation} />+ </div>+ </div>+ </div>+ );+}++function sameLayout(a: Record<string, DashboardWidgetLayout>, b: Record<string, DashboardWidgetLayout>) {+ const aKeys = Object.keys(a);+ if (aKeys.length !== Object.keys(b).length) {+ return false;+ }+ return aKeys.every((key) => {+ const other = b[key];+ return other && a[key].x === other.x && a[key].y === other.y && a[key].w === other.w && a[key].h === other.h;+ });+}diff --git a/apps/client/src/widgets/collections/interface.ts b/apps/client/src/widgets/collections/interface.tsindex 15acbfa8e2..c8b9c2401a 100644--- a/apps/client/src/widgets/collections/interface.ts+++ b/apps/client/src/widgets/collections/interface.ts@@ -1,7 +1,7 @@import FNote from "../../entities/fnote";import type { PrintReport } from "../../print";-export const allViewTypes = ["list", "grid", "calendar", "table", "geoMap", "board", "presentation"] as const;+export const allViewTypes = ["list", "grid", "calendar", "table", "geoMap", "board", "presentation", "dashboard"] as const;export type ViewTypeOptions = typeof allViewTypes[number];export type ViewModeMedia = "screen" | "print";diff --git a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsxindex 211c83cffa..ed4ff377a4 100644--- a/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx+++ b/apps/client/src/widgets/collections/legacy/ListOrGridView.tsx@@ -304,7 +304,7 @@ function NoteMenuButton(props: {notePath: string}) {/>}-function getNotePath(parentNote: FNote, childNote: FNote) {+export function getNotePath(parentNote: FNote, childNote: FNote) {if (parentNote.type === "search") {// for search note parent, we want to display a non-search pathreturn childNote.noteId;diff --git a/apps/client/src/widgets/note_bars/CollectionProperties.tsx b/apps/client/src/widgets/note_bars/CollectionProperties.tsxindex 14f8685eaa..bf85dc9c0d 100644--- a/apps/client/src/widgets/note_bars/CollectionProperties.tsx+++ b/apps/client/src/widgets/note_bars/CollectionProperties.tsx@@ -24,7 +24,8 @@ export const ICON_MAPPINGS: Record<ViewTypeOptions, string> = {table: "bx bx-table",geoMap: "bx bx-map-alt",board: "bx bx-columns",- presentation: "bx bx-rectangle"+ presentation: "bx bx-rectangle",+ dashboard: "bx bxs-dashboard"};const MAX_OPEN_TABS = 50;diff --git a/apps/client/src/widgets/ribbon/CollectionPropertiesTab.tsx b/apps/client/src/widgets/ribbon/CollectionPropertiesTab.tsxindex e25f2650c9..7a0f9080d9 100644--- a/apps/client/src/widgets/ribbon/CollectionPropertiesTab.tsx+++ b/apps/client/src/widgets/ribbon/CollectionPropertiesTab.tsx@@ -23,7 +23,8 @@ export const VIEW_TYPE_MAPPINGS: Record<ViewTypeOptions, string> = {table: t("book_properties.table"),geoMap: t("book_properties.geo-map"),board: t("book_properties.board"),- presentation: t("book_properties.presentation")+ presentation: t("book_properties.presentation"),+ dashboard: t("book_properties.dashboard")};const isNewLayout = isExperimentalFeatureEnabled("new-layout");diff --git a/apps/client/src/widgets/ribbon/collection-properties-config.tsx b/apps/client/src/widgets/ribbon/collection-properties-config.tsxindex b4a46a89bc..56a5e55ba6 100644--- a/apps/client/src/widgets/ribbon/collection-properties-config.tsx+++ b/apps/client/src/widgets/ribbon/collection-properties-config.tsx@@ -135,6 +135,9 @@ export const bookPropertiesConfig: Record<ViewTypeOptions, BookConfig> = {}))}]+ },+ dashboard: {+ properties: []}};diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 0f1999b86d..8b272c51b8 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -304,6 +304,9 @@ importers:force-graph:specifier: 1.51.4version: 1.51.4+ gridstack:+ specifier: 12.6.0+ version: 12.6.0htmldiff-js:specifier: 1.0.5version: 1.0.5@@ -9088,6 +9091,9 @@ packages:resolution: {integrity: sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ==}+ resolution: {integrity: sha512-dUrqsormSybFn/2P4Dz8AgprftKD5e/IiV7UmC0XLQU+G+/WtkAeFiCSNLoAGhPDXoJ/O61Xtj3gljY/Ds83yQ==}+resolution: {integrity: sha512-+xGQY0YyAWCnqy7Cd++hc2JqMYzlm0dG30Jd0beaA64sROr8C4nt8Yc9V5Ro3avlSUDTN0ulqP/VBKi1/lLygw==}@@ -23887,6 +23893,8 @@ snapshots:+ [email protected]: {}+