A local-first personal finance app
:recycle: (typescript) refactor global-events to TS (#1942)
Matiss Janis Aboltins46988800efa8
diff --git a/packages/desktop-client/src/global-events.js b/packages/desktop-client/src/global-events.tssimilarity index 88%rename from packages/desktop-client/src/global-events.jsrename to packages/desktop-client/src/global-events.tsindex 2123b26756..ee7dbb50c3 100644--- a/packages/desktop-client/src/global-events.js+++ b/packages/desktop-client/src/global-events.ts@@ -1,32 +1,28 @@+import { type Store } from 'redux';+import * as sharedListeners from 'loot-core/src/client/shared-listeners';+import type { State } from 'loot-core/src/client/state-types';import { listen } from 'loot-core/src/platform/client/fetch';import * as undo from 'loot-core/src/platform/client/undo';-export function handleGlobalEvents(actions, store) {- global.Actual.onEventFromMain('update-downloaded', (event, info) => {- actions.setAppState({ updateInfo: info });+import { type BoundActions } from './hooks/useActions';++export function handleGlobalEvents(actions: BoundActions, store: Store<State>) {+ global.Actual.onEventFromMain('update-downloaded', (event, updateInfo) => {+ actions.setAppState({ updateInfo });});- global.Actual.onEventFromMain('update-error', msg => {+ global.Actual.onEventFromMain('update-error', () => {// Ignore errors. We don't want to constantly bug the user if they// always have a flaky connection or have intentionally disabled// updates. They will see the error in the about page if they try// to update.});- listen('server-error', info => {+ listen('server-error', () => {actions.addGenericErrorNotification();});- listen('update-loading-status', status => {- switch (status) {- case 'updating':- actions.updateStatusText('Updating...');- break;- default:- }- });-listen('orphaned-payees', ({ orphanedIds, updatedPayeeIds }) => {// Right now, it prompts to merge into the first payeeactions.pushModal('merge-unused-payees', {@@ -36,7 +32,7 @@ export function handleGlobalEvents(actions, store) {});listen('schedules-offline', ({ payees }) => {- actions.pushModal(`schedule-posts-offline-notification`, { payees });+ actions.pushModal('schedule-posts-offline-notification', { payees });});// This is experimental: we sync data locally automatically when@@ -71,7 +67,7 @@ export function handleGlobalEvents(actions, store) {listen('undo-event', undoState => {const { tables, undoTag } = undoState;- const promises = [];+ const promises: Promise<unknown>[] = [];if (tables.includes('categories') ||diff --git a/packages/desktop-client/src/index.tsx b/packages/desktop-client/src/index.tsxindex 4140c134e6..7a01a023e7 100644--- a/packages/desktop-client/src/index.tsx+++ b/packages/desktop-client/src/index.tsx@@ -29,6 +29,7 @@ import { send } from 'loot-core/src/platform/client/fetch';import App from './components/App';import { ServerProvider } from './components/ServerContext';import { handleGlobalEvents } from './global-events';+import { type BoundActions } from './hooks/useActions';// See https://github.com/WICG/focus-visible. Only makes the blue// focus outline appear from keyboard events.@@ -57,7 +58,10 @@ function rootReducer(state, action) {}const store = createStore(rootReducer, undefined, applyMiddleware(thunk));-const boundActions = bindActionCreators(actions, store.dispatch);+const boundActions = bindActionCreators(+ actions,+ store.dispatch,+) as unknown as BoundActions;// Listen for global events from the server or main processhandleGlobalEvents(boundActions, store);@@ -65,7 +69,7 @@ handleGlobalEvents(boundActions, store);declare global {// eslint-disable-next-line @typescript-eslint/consistent-type-definitionsinterface Window {- __actionsForMenu: typeof actions;+ __actionsForMenu: BoundActions;$send: typeof send;$query: typeof runQuery;diff --git a/packages/loot-core/src/client/actions/budgets.ts b/packages/loot-core/src/client/actions/budgets.tsindex ccac2680d9..3247472f4e 100644--- a/packages/loot-core/src/client/actions/budgets.ts+++ b/packages/loot-core/src/client/actions/budgets.ts@@ -8,19 +8,6 @@ import { closeModal, pushModal } from './modals';import { loadPrefs, loadGlobalPrefs } from './prefs';import type { Dispatch, GetState } from './types';-export function updateStatusText(text: string | null) {- return (dispatch: Dispatch, getState: GetState) => {- const { loadingText } = getState().app;- // The presence of any loading text puts the app in a "loading"- // state. We only ever want to update the text, we never want to- // set the app into a loading state. It's expected for workflows- // to set a blank loading text to show the loading screen.- if (loadingText != null) {- dispatch(setAppState({ loadingText: text }));- }- };-}-export function loadBudgets() {return async (dispatch: Dispatch) => {const budgets = await send('get-budgets');diff --git a/packages/loot-core/src/platform/client/undo/index.d.ts b/packages/loot-core/src/platform/client/undo/index.d.tsindex 602a703e1c..86a8e542f9 100644--- a/packages/loot-core/src/platform/client/undo/index.d.ts+++ b/packages/loot-core/src/platform/client/undo/index.d.ts@@ -1,7 +1,9 @@+import { type ModalType } from '../../client/state-types/modals';+export type UndoState = {id?: string;url: unknown;- openModal: unknown;+ openModal: ModalType;selectedItems: {name: string;items: Set<string>;diff --git a/packages/loot-core/src/types/server-events.d.ts b/packages/loot-core/src/types/server-events.d.tsindex 1f43a800fe..d256ddfd42 100644--- a/packages/loot-core/src/types/server-events.d.ts+++ b/packages/loot-core/src/types/server-events.d.ts@@ -1,10 +1,15 @@+import { type UndoState } from '../server/undo';+export interface ServerEvents {'backups-updated': unknown;'cells-changed': Array<{ name }>;'fallback-write-error': unknown;'finish-import': unknown;'finish-load': unknown;- 'orphaned-payees': unknown;+ 'orphaned-payees': {+ orphanedIds: string[];+ updatedPayeeIds: string[];+ };'prefs-updated': unknown;'schedules-offline': { payees: unknown[] };'server-error': unknown;@@ -12,5 +17,5 @@ export interface ServerEvents {'start-import': unknown;'start-load': unknown;'sync-event': { type; subtype; meta; tables; syncDisabled };- 'undo-event': unknown;+ 'undo-event': UndoState;}diff --git a/upcoming-release-notes/1942.md b/upcoming-release-notes/1942.mdnew file mode 100644index 0000000000..c2aa3e74e0--- /dev/null+++ b/upcoming-release-notes/1942.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [MatissJanis]+---++Ported `global-events` to TypeScript