Skip to content

A local-first personal finance app

[AI] Batch dashboard layout updates (#9280)

youngcw638c9bc343ef
diff --git a/packages/loot-core/src/server/dashboard/app.test.ts b/packages/loot-core/src/server/dashboard/app.test.ts
index 4d416f7e42..e74f3a96be 100644
--- a/packages/loot-core/src/server/dashboard/app.test.ts
+++ b/packages/loot-core/src/server/dashboard/app.test.ts
@@ -1,8 +1,13 @@
-import { describe, expect, it } from 'vitest';
+import { beforeEach, describe, expect, it } from 'vitest';
+import { aqlQuery } from '#server/aql';
+import * as db from '#server/db';
+import { loadMappings } from '#server/db/mappings';
+import { runMutator } from '#server/mutators';
+import { q } from '#shared/query';
import type { DashboardWidgetEntity } from '#types/models';
-import { isWidgetType } from './app';
+import { app as dashboardApp, isWidgetType } from './app';
function allWidgetTypes<T extends DashboardWidgetEntity['type'][]>(
...types: T &
@@ -39,3 +44,73 @@ describe('isWidgetType', () => {
expect(isWidgetType('unknown-card')).toBe(false);
});
});
+
+describe('dashboard-update', () => {
+ beforeEach(async () => {
+ await global.emptyDatabase()();
+ await loadMappings();
+ });
+
+ async function addWidget(id: string, x: number) {
+ await db.insertWithSchema('dashboard', {
+ id,
+ type: 'markdown-card',
+ width: 4,
+ height: 2,
+ x,
+ y: 0,
+ dashboard_page_id: 'dashboard-page-id',
+ });
+ }
+
+ // An empty database still carries the default dashboard the migrations
+ // seed, so only look at the widgets the test added
+ async function getTestWidgets(ids: string[]) {
+ const { data } = await aqlQuery(
+ q('dashboard')
+ .filter({ id: { $oneof: ids } })
+ .select('*'),
+ );
+ return (data as DashboardWidgetEntity[]).sort((a, b) =>
+ a.id.localeCompare(b.id),
+ );
+ }
+
+ it('persists every widget in a layout change', async () => {
+ await addWidget('widget-1', 0);
+ await addWidget('widget-2', 4);
+ await addWidget('widget-3', 8);
+
+ await runMutator(() =>
+ dashboardApp.handlers['dashboard-update']([
+ { id: 'widget-1', x: 8, y: 2 },
+ { id: 'widget-2', x: 0, y: 4 },
+ { id: 'widget-3', x: 4, y: 6 },
+ ]),
+ );
+
+ expect(
+ (await getTestWidgets(['widget-1', 'widget-2', 'widget-3'])).map(
+ ({ id, x, y }) => ({ id, x, y }),
+ ),
+ ).toEqual([
+ { id: 'widget-1', x: 8, y: 2 },
+ { id: 'widget-2', x: 0, y: 4 },
+ { id: 'widget-3', x: 4, y: 6 },
+ ]);
+ });
+
+ it('round-trips a meta object', async () => {
+ await addWidget('widget-1', 0);
+
+ await runMutator(() =>
+ dashboardApp.handlers['dashboard-update']([
+ { id: 'widget-1', meta: { content: '## Notes' } },
+ ]),
+ );
+
+ expect((await getTestWidgets(['widget-1']))[0].meta).toEqual({
+ content: '## Notes',
+ });
+ });
+});
diff --git a/packages/loot-core/src/server/dashboard/app.ts b/packages/loot-core/src/server/dashboard/app.ts
index a5f35076fd..c0c751b28c 100644
--- a/packages/loot-core/src/server/dashboard/app.ts
+++ b/packages/loot-core/src/server/dashboard/app.ts
@@ -152,12 +152,14 @@ async function updateDashboard(
(dbWidgets as DashboardWidgetEntity[]).map(widget => [widget.id, widget]),
);
- await Promise.all(
- widgets
- // Perform an update query only if the widget actually has changes
- .filter(widget => !isMatch(dbWidgetMap.get(widget.id) ?? {}, widget))
- .map(widget => db.update('dashboard', widget)),
- );
+ await batchMessages(async () => {
+ await Promise.all(
+ widgets
+ // Perform an update query only if the widget actually has changes
+ .filter(widget => !isMatch(dbWidgetMap.get(widget.id) ?? {}, widget))
+ .map(widget => db.updateWithSchema('dashboard', widget)),
+ );
+ });
}
async function updateDashboardWidget(
diff --git a/upcoming-release-notes/batch-dashboard-layout-updates.md b/upcoming-release-notes/batch-dashboard-layout-updates.md
new file mode 100644
index 0000000000..00c4b71ce7
--- /dev/null
+++ b/upcoming-release-notes/batch-dashboard-layout-updates.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [youngcw]
+---
+
+Send one sync instead of one per widget when rearranging the report dashboard, and run widget layout updates through the schema