Skip to content

A local-first personal finance app

[AI] Refresh the report dashboard when another device syncs changes (#9277)

youngcwd2dae07f5630
diff --git a/packages/desktop-client/src/global-events.ts b/packages/desktop-client/src/global-events.ts
index 818991f373..69ad969b19 100644
--- a/packages/desktop-client/src/global-events.ts
+++ b/packages/desktop-client/src/global-events.ts
@@ -17,6 +17,7 @@ import {
import { payeeQueries } from './payees';
import { loadPrefs } from './prefs/prefsSlice';
import type { AppStore } from './redux/store';
+import { dashboardQueries, reportQueries } from './reports';
import * as syncEvents from './sync-events';
export function handleGlobalEvents(store: AppStore, queryClient: QueryClient) {
@@ -84,6 +85,22 @@ export function handleGlobalEvents(store: AppStore, queryClient: QueryClient) {
);
}
+ if (tables.includes('dashboard') || tables.includes('dashboard_pages')) {
+ promises.push(
+ queryClient.invalidateQueries({
+ queryKey: dashboardQueries.lists(),
+ }),
+ );
+ }
+
+ if (tables.includes('custom_reports')) {
+ promises.push(
+ queryClient.invalidateQueries({
+ queryKey: reportQueries.lists(),
+ }),
+ );
+ }
+
const tagged = undo.getTaggedState(undoTag);
if (tagged) {
diff --git a/packages/desktop-client/src/sync-events.test.ts b/packages/desktop-client/src/sync-events.test.ts
new file mode 100644
index 0000000000..e115542d43
--- /dev/null
+++ b/packages/desktop-client/src/sync-events.test.ts
@@ -0,0 +1,105 @@
+import * as connection from '@actual-app/core/platform/client/connection';
+import type { ServerEvents } from '@actual-app/core/types/server-events';
+import type { QueryClient } from '@tanstack/react-query';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import type { AppStore } from './redux/store';
+import { dashboardQueries, reportQueries } from './reports';
+import { listenForSyncEvent } from './sync-events';
+
+const eventListeners = new Map<string, Array<(args: unknown) => void>>();
+
+function mockListen(name: string, listener: (args: unknown) => void) {
+ const listeners = eventListeners.get(name) ?? [];
+ listeners.push(listener);
+ eventListeners.set(name, listeners);
+
+ return () => {
+ eventListeners.set(
+ name,
+ (eventListeners.get(name) ?? []).filter(l => l !== listener),
+ );
+ };
+}
+
+function publishSyncEvent(event: ServerEvents['sync-event']) {
+ (eventListeners.get('sync-event') ?? []).forEach(listener => listener(event));
+}
+
+function setup() {
+ vi.spyOn(connection, 'listen').mockImplementation(
+ // The real `listen` is overloaded per event name; the mock only needs
+ // to record the handler
+ mockListen as unknown as typeof connection.listen,
+ );
+
+ const invalidateQueries = vi.fn();
+ const queryClient = { invalidateQueries } as unknown as QueryClient;
+
+ // A budget must be loaded for the table -> invalidate mapping to run
+ const store = {
+ getState: () => ({
+ prefs: { local: { id: 'budget-id' } },
+ app: { loadingText: null },
+ }),
+ dispatch: vi.fn(),
+ } as unknown as AppStore;
+
+ listenForSyncEvent(store, queryClient);
+
+ return { invalidateQueries };
+}
+
+function appliedEvent(tables: string[]): ServerEvents['sync-event'] {
+ return { type: 'applied', tables } as ServerEvents['sync-event'];
+}
+
+describe('listenForSyncEvent', () => {
+ beforeEach(() => {
+ eventListeners.clear();
+ vi.clearAllMocks();
+ });
+
+ it('invalidates the dashboard queries when widgets sync', () => {
+ const { invalidateQueries } = setup();
+
+ publishSyncEvent(appliedEvent(['dashboard']));
+
+ expect(invalidateQueries).toHaveBeenCalledWith({
+ queryKey: dashboardQueries.lists(),
+ });
+ });
+
+ it('invalidates the dashboard queries when dashboard pages sync', () => {
+ const { invalidateQueries } = setup();
+
+ publishSyncEvent(appliedEvent(['dashboard_pages']));
+
+ expect(invalidateQueries).toHaveBeenCalledWith({
+ queryKey: dashboardQueries.lists(),
+ });
+ });
+
+ it('invalidates the report queries when custom reports sync', () => {
+ const { invalidateQueries } = setup();
+
+ publishSyncEvent(appliedEvent(['custom_reports']));
+
+ expect(invalidateQueries).toHaveBeenCalledWith({
+ queryKey: reportQueries.lists(),
+ });
+ });
+
+ it('leaves the dashboard and report queries alone for unrelated tables', () => {
+ const { invalidateQueries } = setup();
+
+ publishSyncEvent(appliedEvent(['transactions']));
+
+ expect(invalidateQueries).not.toHaveBeenCalledWith({
+ queryKey: dashboardQueries.lists(),
+ });
+ expect(invalidateQueries).not.toHaveBeenCalledWith({
+ queryKey: reportQueries.lists(),
+ });
+ });
+});
diff --git a/packages/desktop-client/src/sync-events.ts b/packages/desktop-client/src/sync-events.ts
index e8182ae38c..fc8f0bc9ee 100644
--- a/packages/desktop-client/src/sync-events.ts
+++ b/packages/desktop-client/src/sync-events.ts
@@ -17,6 +17,7 @@ import type { Notification } from './notifications/notificationsSlice';
import { payeeQueries } from './payees';
import { loadPrefs } from './prefs/prefsSlice';
import type { AppStore } from './redux/store';
+import { dashboardQueries, reportQueries } from './reports';
import { signOut } from './users/usersSlice';
// Notifications for sync events that can fire during budget load
@@ -139,6 +140,18 @@ export function listenForSyncEvent(store: AppStore, queryClient: QueryClient) {
queryKey: accountGroupQueries.lists(),
});
}
+
+ if (tables.includes('dashboard') || tables.includes('dashboard_pages')) {
+ void queryClient.invalidateQueries({
+ queryKey: dashboardQueries.lists(),
+ });
+ }
+
+ if (tables.includes('custom_reports')) {
+ void queryClient.invalidateQueries({
+ queryKey: reportQueries.lists(),
+ });
+ }
} else if (event.type === 'error') {
let notif: Notification | null = null;
const learnMore = `[${t('Learn more')}](https://actualbudget.org/docs/getting-started/sync/#debugging-sync-issues)`;
diff --git a/upcoming-release-notes/refresh-dashboard-on-sync.md b/upcoming-release-notes/refresh-dashboard-on-sync.md
new file mode 100644
index 0000000000..d15327d4b9
--- /dev/null
+++ b/upcoming-release-notes/refresh-dashboard-on-sync.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [youngcw]
+---
+
+Show reports added or changed on another device right away, instead of only after switching pages