Skip to content

A local-first personal finance app

[AI] Show a progress bar while importing a YNAB budget (#8956)

Stephen Brown II9788bb123a42
diff --git a/packages/desktop-client/src/components/modals/manager/ImportProgress.tsx b/packages/desktop-client/src/components/modals/manager/ImportProgress.tsx
new file mode 100644
index 0000000000..333583a902
--- /dev/null
+++ b/packages/desktop-client/src/components/modals/manager/ImportProgress.tsx
@@ -0,0 +1,113 @@
+import { useEffect, useState } from 'react';
+import { ProgressBar } from 'react-aria-components';
+import { useTranslation } from 'react-i18next';
+
+import { styles } from '@actual-app/components/styles';
+import { Text } from '@actual-app/components/text';
+import { theme } from '@actual-app/components/theme';
+import { View } from '@actual-app/components/view';
+import { listen } from '@actual-app/core/platform/client/connection';
+import type {
+ ImportStep,
+ ServerEvents,
+} from '@actual-app/core/types/server-events';
+import { css } from '@emotion/css';
+
+/**
+ * Shows which stage a budget import is on and how far along it is. Renders
+ * nothing until the import reports its first step.
+ */
+export function ImportProgress() {
+ const { t } = useTranslation();
+ const [progress, setProgress] = useState<
+ ServerEvents['import-progress'] | null
+ >(null);
+
+ useEffect(() => listen('import-progress', setProgress), []);
+
+ if (!progress) {
+ return null;
+ }
+
+ const { step, current, total, overallCurrent, overallTotal, batch } =
+ progress;
+ const percentage = overallTotal ? (overallCurrent / overallTotal) * 100 : 0;
+ const stepProgress = total > 0 ? ` (${current}/${total})` : '';
+
+ return (
+ <View
+ style={{
+ alignItems: 'center',
+ alignSelf: 'stretch',
+ gap: 10,
+ marginTop: 20,
+ }}
+ >
+ <Text style={styles.tnum}>
+ {getStepText(t, step, stepProgress, batch)}
+ </Text>
+ <ProgressBar
+ value={percentage}
+ aria-label={t('Import progress')}
+ style={{ width: '100%' }}
+ >
+ <View
+ className={css({
+ height: 6,
+ borderRadius: 3,
+ backgroundColor: theme.pillBackground,
+ overflow: 'hidden',
+ })}
+ >
+ <View
+ className={css({
+ height: '100%',
+ width: `${percentage}%`,
+ backgroundColor: theme.pageTextPositive,
+ transition: 'width 150ms ease-out',
+ })}
+ />
+ </View>
+ </ProgressBar>
+ </View>
+ );
+}
+
+function getStepText(
+ t: ReturnType<typeof useTranslation>['t'],
+ step: ImportStep,
+ progress: string,
+ batch?: { amount: number; account: string },
+): string {
+ switch (step) {
+ case 'accounts':
+ return t('Importing accounts{{progress}}...', { progress });
+ case 'categories':
+ return t('Importing categories{{progress}}...', { progress });
+ case 'payees':
+ return t('Importing payees{{progress}}...', { progress });
+ case 'payee-locations':
+ return t('Importing payee locations{{progress}}...', { progress });
+ case 'tags':
+ return t('Importing tags{{progress}}...', { progress });
+ case 'transactions':
+ // Transactions go in one batch per account, so the tick reports an
+ // account that has already landed rather than one in flight.
+ return batch
+ ? t('Imported {{count}} transactions for {{account}}{{progress}}...', {
+ count: batch.amount,
+ account: batch.account,
+ progress,
+ })
+ : t('Importing transactions{{progress}}...', { progress });
+ case 'scheduled-transactions':
+ return t('Importing scheduled transactions{{progress}}...', { progress });
+ case 'budgets':
+ // The count is months, not individual budgeted amounts.
+ return t('Importing monthly budgets{{progress}}...', { progress });
+ // The union is exhaustive above; `default` is here for the lint rule.
+ case 'finishing':
+ default:
+ return t('Setting up...');
+ }
+}
diff --git a/packages/desktop-client/src/components/modals/manager/ImportYNAB4Modal.tsx b/packages/desktop-client/src/components/modals/manager/ImportYNAB4Modal.tsx
index c794df3565..76b2bd95d6 100644
--- a/packages/desktop-client/src/components/modals/manager/ImportYNAB4Modal.tsx
+++ b/packages/desktop-client/src/components/modals/manager/ImportYNAB4Modal.tsx
@@ -11,6 +11,7 @@ import { View } from '@actual-app/components/view';
import { importBudget } from '#budgetfiles/budgetfilesSlice';
import { Modal, ModalCloseButton, ModalHeader } from '#components/common/Modal';
+import { ImportProgress } from '#components/modals/manager/ImportProgress';
import { useNavigate } from '#hooks/useNavigate';
import { useDispatch } from '#redux';
@@ -50,12 +51,18 @@ export function ImportYNAB4Modal() {
}
return (
- <Modal name="import-ynab4" containerProps={{ style: { width: 400 } }}>
+ <Modal
+ name="import-ynab4"
+ isDismissable={!importing}
+ containerProps={{ style: { width: 400 } }}
+ >
{({ state }) => (
<>
<ModalHeader
title={t('Import from YNAB4')}
- rightContent={<ModalCloseButton onPress={() => state.close()} />}
+ rightContent={
+ !importing && <ModalCloseButton onPress={() => state.close()} />
+ }
/>
<View style={{ ...styles.smallText, lineHeight: 1.5, marginTop: 20 }}>
{error && (
@@ -82,16 +89,18 @@ export function ImportYNAB4Modal() {
folder". Upload the zipped folder for importing.
</Trans>
</Paragraph>
- <View>
- <ButtonWithLoading
- variant="primary"
- autoFocus
- isLoading={importing}
- onPress={onImport}
- >
- <Trans>Select zip file...</Trans>
- </ButtonWithLoading>
- </View>
+ {!importing && (
+ <View>
+ <ButtonWithLoading
+ variant="primary"
+ autoFocus
+ onPress={onImport}
+ >
+ <Trans>Select zip file...</Trans>
+ </ButtonWithLoading>
+ </View>
+ )}
+ <ImportProgress />
</View>
</View>
</>
diff --git a/packages/desktop-client/src/components/modals/manager/ImportYNAB5Modal.tsx b/packages/desktop-client/src/components/modals/manager/ImportYNAB5Modal.tsx
index 87a43da7db..0bdf3d1d90 100644
--- a/packages/desktop-client/src/components/modals/manager/ImportYNAB5Modal.tsx
+++ b/packages/desktop-client/src/components/modals/manager/ImportYNAB5Modal.tsx
@@ -12,6 +12,7 @@ import { View } from '@actual-app/components/view';
import { importBudget } from '#budgetfiles/budgetfilesSlice';
import { Link } from '#components/common/Link';
import { Modal, ModalCloseButton, ModalHeader } from '#components/common/Modal';
+import { ImportProgress } from '#components/modals/manager/ImportProgress';
import { useNavigate } from '#hooks/useNavigate';
import { useDispatch } from '#redux';
@@ -60,12 +61,18 @@ export function ImportYNAB5Modal() {
}
return (
- <Modal name="import-ynab5" containerProps={{ style: { width: 400 } }}>
+ <Modal
+ name="import-ynab5"
+ isDismissable={!importing}
+ containerProps={{ style: { width: 400 } }}
+ >
{({ state }) => (
<>
<ModalHeader
title={t('Import from nYNAB')}
- rightContent={<ModalCloseButton onPress={() => state.close()} />}
+ rightContent={
+ !importing && <ModalCloseButton onPress={() => state.close()} />
+ }
/>
<View style={{ ...styles.smallText, lineHeight: 1.5, marginTop: 20 }}>
{error && (
@@ -101,16 +108,18 @@ export function ImportYNAB5Modal() {
fix up any problems.
</Trans>
</Paragraph>
- <View>
- <ButtonWithLoading
- variant="primary"
- autoFocus
- isLoading={importing}
- onPress={onImport}
- >
- <Trans>Select file...</Trans>
- </ButtonWithLoading>
- </View>
+ {!importing && (
+ <View>
+ <ButtonWithLoading
+ variant="primary"
+ autoFocus
+ onPress={onImport}
+ >
+ <Trans>Select file...</Trans>
+ </ButtonWithLoading>
+ </View>
+ )}
+ <ImportProgress />
</View>
</View>
</>
diff --git a/packages/loot-core/src/server/importers/progress.test.ts b/packages/loot-core/src/server/importers/progress.test.ts
new file mode 100644
index 0000000000..b71e9312e6
--- /dev/null
+++ b/packages/loot-core/src/server/importers/progress.test.ts
@@ -0,0 +1,126 @@
+import * as connection from '#platform/server/connection';
+import type { ServerEvents } from '#types/server-events';
+
+import { runImportSteps } from './progress';
+
+vi.mock('#platform/server/connection', () => ({ send: vi.fn() }));
+
+function sentProgress() {
+ return vi
+ .mocked(connection.send)
+ .mock.calls.map(
+ ([, payload]) => payload as ServerEvents['import-progress'],
+ );
+}
+
+describe('runImportSteps', () => {
+ beforeEach(() => {
+ vi.mocked(connection.send).mockClear();
+ // Pin the clock so the throttle can't emit an extra event under a slow run.
+ vi.spyOn(Date, 'now').mockReturnValue(0);
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it('reports per-step and overall counts as items are imported', async () => {
+ const ran: string[] = [];
+
+ await runImportSteps([
+ {
+ step: 'accounts',
+ total: 2,
+ run: async tick => {
+ ran.push('accounts');
+ tick();
+ tick();
+ },
+ },
+ {
+ step: 'transactions',
+ total: 10,
+ run: async tick => {
+ ran.push('transactions');
+ tick(10);
+ },
+ },
+ ]);
+
+ expect(ran).toEqual(['accounts', 'transactions']);
+ expect(sentProgress()).toEqual([
+ {
+ step: 'accounts',
+ current: 0,
+ total: 2,
+ overallCurrent: 0,
+ overallTotal: 12,
+ },
+ {
+ step: 'accounts',
+ current: 2,
+ total: 2,
+ overallCurrent: 2,
+ overallTotal: 12,
+ },
+ {
+ step: 'transactions',
+ current: 0,
+ total: 10,
+ overallCurrent: 2,
+ overallTotal: 12,
+ },
+ {
+ step: 'transactions',
+ current: 10,
+ total: 10,
+ overallCurrent: 12,
+ overallTotal: 12,
+ },
+ {
+ step: 'finishing',
+ current: 0,
+ total: 0,
+ overallCurrent: 12,
+ overallTotal: 12,
+ },
+ ]);
+ });
+
+ it('reports a named batch immediately, without waiting out the throttle', async () => {
+ await runImportSteps([
+ {
+ step: 'transactions',
+ total: 30,
+ run: async tick => {
+ tick(10, 'Checking');
+ tick(20, 'Savings');
+ },
+ },
+ ]);
+
+ expect(sentProgress().map(payload => payload.batch)).toEqual([
+ undefined,
+ { amount: 10, account: 'Checking' },
+ { amount: 20, account: 'Savings' },
+ undefined,
+ undefined,
+ ]);
+ });
+
+ it('snaps a step to its total when the estimate was off', async () => {
+ // The step only imports 1 of the 5 items it estimated, but progress still
+ // has to end up complete.
+ await runImportSteps([
+ { step: 'payees', total: 5, run: async tick => tick() },
+ ]);
+
+ expect(sentProgress().at(-2)).toEqual({
+ step: 'payees',
+ current: 5,
+ total: 5,
+ overallCurrent: 5,
+ overallTotal: 5,
+ });
+ });
+});
diff --git a/packages/loot-core/src/server/importers/progress.ts b/packages/loot-core/src/server/importers/progress.ts
new file mode 100644
index 0000000000..b37f432b37
--- /dev/null
+++ b/packages/loot-core/src/server/importers/progress.ts
@@ -0,0 +1,81 @@
+import * as connection from '#platform/server/connection';
+import { logger } from '#platform/server/log';
+import type { ImportStep } from '#types/server-events';
+
+/**
+ * Reports that `count` more items of the current step have been imported.
+ * Steps that import a batch at a time can name the account it belonged to, so
+ * the client can say which one it just finished.
+ */
+export type ImportTick = (count?: number, account?: string) => void;
+
+type ImportStepDefinition = {
+ step: ImportStep;
+ /**
+ * How many items this step expects to import. Only used to show progress, so
+ * an estimate is fine — the step is snapped to its total once it finishes.
+ */
+ total: number;
+ run: (tick: ImportTick) => Promise<unknown>;
+};
+
+// Importing tens of thousands of items would otherwise flood the client with
+// an event per item.
+const MIN_EVENT_INTERVAL_MS = 100;
+
+/**
+ * Runs each import step in order, reporting progress to the client so it can
+ * show which stage the import is on and how far along it is.
+ */
+export async function runImportSteps(steps: ImportStepDefinition[]) {
+ const overallTotal = steps.reduce((sum, { total }) => sum + total, 0);
+ let overallCurrent = 0;
+ let lastSentAt = 0;
+
+ for (const { step, total, run } of steps) {
+ logger.log(`Importing ${step}...`);
+ let current = 0;
+
+ const emit = (batch?: { amount: number; account: string }) => {
+ lastSentAt = Date.now();
+ connection.send('import-progress', {
+ step,
+ current,
+ total,
+ overallCurrent,
+ overallTotal,
+ batch,
+ });
+ };
+
+ emit();
+
+ await run((count = 1, account) => {
+ current += count;
+ overallCurrent += count;
+ // Always report a named batch: they are rare, and the account name is
+ // the whole point of the message.
+ if (account !== undefined) {
+ emit({ amount: count, account });
+ } else if (Date.now() - lastSentAt >= MIN_EVENT_INTERVAL_MS) {
+ emit();
+ }
+ });
+
+ // Totals are estimated up front, so snap the step up to its total instead
+ // of leaving the bar short. Never snap back: an overshooting estimate must
+ // not make the overall bar run backwards.
+ overallCurrent += Math.max(0, total - current);
+ current = total;
+ emit();
+ }
+
+ logger.log('Setting up...');
+ connection.send('import-progress', {
+ step: 'finishing',
+ current: 0,
+ total: 0,
+ overallCurrent: overallTotal,
+ overallTotal,
+ });
+}
diff --git a/packages/loot-core/src/server/importers/ynab4.ts b/packages/loot-core/src/server/importers/ynab4.ts
index f8dbdf2788..9809d40533 100644
--- a/packages/loot-core/src/server/importers/ynab4.ts
+++ b/packages/loot-core/src/server/importers/ynab4.ts
@@ -7,6 +7,8 @@ import { safeUnzip } from '#server/util/zip';
import * as monthUtils from '#shared/months';
import { amountToInteger, groupBy, sortByKey } from '#shared/util';
+import { runImportSteps } from './progress';
+import type { ImportTick } from './progress';
import type * as YNAB4 from './ynab4-types';
// Importer
@@ -14,6 +16,7 @@ import type * as YNAB4 from './ynab4-types';
async function importAccounts(
data: YNAB4.YFull,
entityIdMap: Map<string, string>,
+ tick: ImportTick,
) {
const accounts = sortByKey(data.accounts, 'sortableIndex');
@@ -28,6 +31,7 @@ async function importAccounts(
},
});
entityIdMap.set(account.entityId, id);
+ tick();
}
}),
);
@@ -36,6 +40,7 @@ async function importAccounts(
async function importCategories(
data: YNAB4.YFull,
entityIdMap: Map<string, string>,
+ tick: ImportTick,
) {
const masterCategories = sortByKey(data.masterCategories, 'sortableIndex');
@@ -54,6 +59,7 @@ async function importCategories(
},
});
entityIdMap.set(masterCategory.entityId, id);
+ tick();
if (masterCategory.note) {
void send('notes-save', {
id,
@@ -95,6 +101,7 @@ async function importCategories(
},
});
entityIdMap.set(category.entityId, id);
+ tick();
if (category.note) {
void send('notes-save', {
id,
@@ -112,6 +119,7 @@ async function importCategories(
async function importPayees(
data: YNAB4.YFull,
entityIdMap: Map<string, string>,
+ tick: ImportTick,
) {
for (const payee of data.payees) {
if (!payee.isTombstone) {
@@ -125,6 +133,7 @@ async function importPayees(
// TODO: import payee rules
entityIdMap.set(payee.entityId, id);
+ tick();
}
}
}
@@ -132,6 +141,7 @@ async function importPayees(
async function importTransactions(
data: YNAB4.YFull,
entityIdMap: Map<string, string>,
+ tick: ImportTick,
) {
const categories = await send('api/categories-get');
const incomeCategoryId: string = categories.find(
@@ -248,6 +258,11 @@ async function importTransactions(
learnCategories: true,
runTransfers: false,
});
+ tick(
+ toImport.length,
+ data.accounts.find(account => account.entityId === accountId)
+ ?.accountName,
+ );
}),
);
}
@@ -285,6 +300,7 @@ function fillInBudgets(
async function importBudgets(
data: YNAB4.YFull,
entityIdMap: Map<string, string>,
+ tick: ImportTick,
) {
const budgets = sortByKey(data.monthlyBudgets, 'month');
@@ -326,6 +342,7 @@ async function importBudgets(
}
}),
);
+ tick();
}
} finally {
await send('api/batch-budget-end');
@@ -377,22 +394,38 @@ function findLatestDevice(
export async function doImport(data: YNAB4.YFull) {
const entityIdMap = new Map<string, string>();
- logger.log('Importing Accounts...');
- await importAccounts(data, entityIdMap);
-
- logger.log('Importing Categories...');
- await importCategories(data, entityIdMap);
-
- logger.log('Importing Payees...');
- await importPayees(data, entityIdMap);
-
- logger.log('Importing Transactions...');
- await importTransactions(data, entityIdMap);
-
- logger.log('Importing Budgets...');
- await importBudgets(data, entityIdMap);
-
- logger.log('Setting up...');
+ await runImportSteps([
+ {
+ step: 'accounts',
+ total: countLive(data.accounts),
+ run: tick => importAccounts(data, entityIdMap, tick),
+ },
+ {
+ step: 'categories',
+ total:
+ countLive(data.masterCategories) +
+ data.masterCategories.reduce(
+ (count, group) => count + countLive(group.subCategories ?? []),
+ 0,
+ ),
+ run: tick => importCategories(data, entityIdMap, tick),
+ },
+ {
+ step: 'payees',
+ total: countLive(data.payees),
+ run: tick => importPayees(data, entityIdMap, tick),
+ },
+ {
+ step: 'transactions',
+ total: data.transactions.length,
+ run: tick => importTransactions(data, entityIdMap, tick),
+ },
+ {
+ step: 'budgets',
+ total: data.monthlyBudgets.length,
+ run: tick => importBudgets(data, entityIdMap, tick),
+ },
+ ]);
}
export function getBudgetName(filepath) {
@@ -476,3 +509,7 @@ export function parseFile(buffer: Buffer): YNAB4.YFull {
throw new Error('Error parsing Budget.yfull file');
}
}
+
+function countLive(entities: { isTombstone?: boolean }[]) {
+ return entities.filter(entity => !entity.isTombstone).length;
+}
diff --git a/packages/loot-core/src/server/importers/ynab5.test.ts b/packages/loot-core/src/server/importers/ynab5.test.ts
index 8867982b1c..050af5f2db 100644
--- a/packages/loot-core/src/server/importers/ynab5.test.ts
+++ b/packages/loot-core/src/server/importers/ynab5.test.ts
@@ -5,6 +5,7 @@ import { send } from '#server/main-app';
import {
getBudgetName,
importPayees,
+ importScheduledTransactions,
importTransactions,
parseFile,
} from './ynab5';
@@ -169,6 +170,90 @@ describe('importTransactions', () => {
});
});
+describe('importScheduledTransactions', () => {
+ function makeScheduledTransaction() {
+ return {
+ id: 'scheduled-1',
+ account_id: 'ynab-account-1',
+ payee_id: 'ynab-payee-1',
+ category_id: 'ynab-category-1',
+ amount: -1000,
+ memo: '',
+ frequency: 'never',
+ date_next: '2026-02-01',
+ date_first: '2026-02-01',
+ deleted: false,
+ };
+ }
+
+ const entityIdMap = new Map<string, string>([
+ ['ynab-account-1', 'actual-account-1'],
+ ['ynab-payee-1', 'actual-payee-1'],
+ ['ynab-category-1', 'actual-category-1'],
+ ]);
+
+ it('advances progress after its category rule update completes', async () => {
+ const tick = vi.fn();
+ const rule = {
+ id: 'rule-1',
+ stage: null,
+ conditions_op: 'and',
+ conditions: '[]',
+ actions: '[]',
+ };
+ let queryCount = 0;
+
+ vi.mocked(send).mockImplementation(async name => {
+ if (name === 'api/payees-get') return [];
+ if (name === 'api/schedule-create') return 'schedule-1';
+ if (name === 'api/query') {
+ queryCount += 1;
+ return queryCount === 1 ? { data: 'rule-1' } : { data: [rule] };
+ }
+ if (name === 'api/rule-update') {
+ expect(tick).not.toHaveBeenCalled();
+ return null;
+ }
+ throw new Error(`Unexpected send call: ${name}`);
+ });
+
+ await importScheduledTransactions(
+ {
+ scheduled_transactions: [makeScheduledTransaction()],
+ scheduled_subtransactions: [],
+ } as unknown as Parameters<typeof importScheduledTransactions>[0],
+ entityIdMap,
+ new Set(),
+ tick,
+ );
+
+ expect(tick).toHaveBeenCalledOnce();
+ });
+
+ it('does not advance progress when schedule creation fails', async () => {
+ const tick = vi.fn();
+ vi.mocked(send).mockImplementation(async name => {
+ if (name === 'api/payees-get') return [];
+ if (name === 'api/schedule-create') throw new Error('create failed');
+ throw new Error(`Unexpected send call: ${name}`);
+ });
+
+ await expect(
+ importScheduledTransactions(
+ {
+ scheduled_transactions: [makeScheduledTransaction()],
+ scheduled_subtransactions: [],
+ } as unknown as Parameters<typeof importScheduledTransactions>[0],
+ entityIdMap,
+ new Set(),
+ tick,
+ ),
+ ).rejects.toThrow('create failed');
+
+ expect(tick).not.toHaveBeenCalled();
+ });
+});
+
describe('ynab5 parseFile', () => {
it('unwraps the legacy `budget` wrapper', () => {
const data = parseFile(
diff --git a/packages/loot-core/src/server/importers/ynab5.ts b/packages/loot-core/src/server/importers/ynab5.ts
index 2db585b135..6f8ab84d07 100644
--- a/packages/loot-core/src/server/importers/ynab5.ts
+++ b/packages/loot-core/src/server/importers/ynab5.ts
@@ -9,6 +9,8 @@ import { q } from '#shared/query';
import { groupBy, sortByKey } from '#shared/util';
import type { RecurConfig, RecurPattern, RuleEntity } from '#types/models';
+import { runImportSteps } from './progress';
+import type { ImportTick } from './progress';
import type {
Budget,
Payee,
@@ -267,7 +269,11 @@ function buildRuleUpdate(
};
}
-function importAccounts(data: Budget, entityIdMap: Map<string, string>) {
+function importAccounts(
+ data: Budget,
+ entityIdMap: Map<string, string>,
+ tick: ImportTick,
+) {
return Promise.all(
data.accounts.map(async account => {
if (!account.deleted) {
@@ -279,6 +285,7 @@ function importAccounts(data: Budget, entityIdMap: Map<string, string>) {
},
});
entityIdMap.set(account.id, id);
+ tick();
}
}),
);
@@ -287,6 +294,7 @@ function importAccounts(data: Budget, entityIdMap: Map<string, string>) {
async function importCategories(
data: Budget,
entityIdMap: Map<string, string>,
+ tick: ImportTick,
) {
// Hidden categories are put in its own group by YNAB,
// so it's already handled.
@@ -390,6 +398,7 @@ async function importCategories(
});
groupId = createdGroup.id;
entityIdMap.set(group.id, groupId);
+ tick();
if (group.note) {
void send('notes-save', {
id: groupId,
@@ -431,6 +440,7 @@ async function importCategories(
hidden: cat.hidden,
});
entityIdMap.set(cat.id, createdCategory.id);
+ tick();
if (cat.note) {
void send('notes-save', {
id: createdCategory.id,
@@ -445,7 +455,11 @@ async function importCategories(
}
}
-export function importPayees(data: Budget, entityIdMap: Map<string, string>) {
+export function importPayees(
+ data: Budget,
+ entityIdMap: Map<string, string>,
+ tick?: ImportTick,
+) {
return Promise.all(
data.payees.map(async payee => {
if (!payee.deleted && !payee.transfer_account_id) {
@@ -453,6 +467,7 @@ export function importPayees(data: Budget, entityIdMap: Map<string, string>) {
payee: { name: payee.name },
});
entityIdMap.set(payee.id, id);
+ tick?.();
}
}),
);
@@ -461,6 +476,7 @@ export function importPayees(data: Budget, entityIdMap: Map<string, string>) {
async function importPayeeLocations(
data: Budget,
entityIdMap: Map<string, string>,
+ tick: ImportTick,
) {
// If no payee locations data provided, skip import
if (!data?.payee_locations) {
@@ -501,6 +517,7 @@ async function importPayeeLocations(
latitude,
longitude,
});
+ tick();
} catch (error) {
const errorMessage =
error instanceof Error
@@ -513,10 +530,7 @@ async function importPayeeLocations(
}
}
-async function importFlagsAsTags(
- data: Budget,
- flagNameConflicts: Set<string>,
-): Promise<void> {
+function getTagsToCreate(data: Budget, flagNameConflicts: Set<string>) {
const tagsToCreate = new Map<string, string | null>();
const flaggedTransactions = getFlaggedTransactions(data);
@@ -549,9 +563,15 @@ async function importFlagsAsTags(
}
}
- if (tagsToCreate.size === 0) {
- return;
- }
+ return tagsToCreate;
+}
+
+async function importFlagsAsTags(
+ data: Budget,
+ flagNameConflicts: Set<string>,
+ tick: ImportTick,
+): Promise<void> {
+ const tagsToCreate = getTagsToCreate(data, flagNameConflicts);
await Promise.all(
[...tagsToCreate.entries()].map(async ([tag, color]) => {
@@ -560,6 +580,7 @@ async function importFlagsAsTags(
color,
description: 'Imported from YNAB',
});
+ tick();
}),
);
}
@@ -568,6 +589,7 @@ export async function importTransactions(
data: Budget,
entityIdMap: Map<string, string>,
flagNameConflicts: Set<string>,
+ tick?: ImportTick,
) {
const payees = await send('api/payees-get');
const categories = await send('api/categories-get');
@@ -839,14 +861,19 @@ export async function importTransactions(
learnCategories: true,
runTransfers: false,
});
+ tick?.(
+ toImport.length,
+ data.accounts.find(account => account.id === accountId)?.name,
+ );
}),
);
}
-async function importScheduledTransactions(
+export async function importScheduledTransactions(
data: Budget,
entityIdMap: Map<string, string>,
flagNameConflicts: Set<string>,
+ tick: ImportTick,
) {
const scheduledTransactions = data.scheduled_transactions;
const scheduledSubtransactionsGrouped = groupBy(
@@ -865,6 +892,7 @@ async function importScheduledTransactions(
const scheduleCategoryMap = new Map<string, string>();
const scheduleSplitsMap = new Map<string, ScheduledSubtransaction[]>();
const schedulePayeeMap = new Map<string, string>();
+ const deferredTicks = new Map<string, ImportTick>();
async function createScheduleWithUniqueName(params: {
name: string;
@@ -982,18 +1010,25 @@ async function importScheduledTransactions(
if (scheduledSubtransactions.length > 0) {
scheduleSplitsMap.set(scheduleId, scheduledSubtransactions);
+ deferredTicks.set(scheduleId, tick);
} else if (!scheduled.transfer_account_id && scheduled.category_id) {
const mappedCategoryId = entityIdMap.get(scheduled.category_id);
if (mappedCategoryId) {
scheduleCategoryMap.set(scheduleId, mappedCategoryId);
+ deferredTicks.set(scheduleId, tick);
}
}
+
+ if (!deferredTicks.has(scheduleId)) {
+ tick();
+ }
}
if (scheduleCategoryMap.size > 0 || scheduleSplitsMap.size > 0) {
for (const [scheduleId, categoryId] of scheduleCategoryMap.entries()) {
const rule = await getRuleForSchedule(scheduleId);
if (!rule) {
+ deferredTicks.get(scheduleId)?.();
continue;
}
@@ -1007,11 +1042,13 @@ async function importScheduledTransactions(
await send('api/rule-update', {
rule: buildRuleUpdate(rule, actions),
});
+ deferredTicks.get(scheduleId)?.();
}
for (const [scheduleId, subtransactions] of scheduleSplitsMap.entries()) {
const rule = await getRuleForSchedule(scheduleId);
if (!rule) {
+ deferredTicks.get(scheduleId)?.();
continue;
}
@@ -1086,11 +1123,16 @@ async function importScheduledTransactions(
await send('api/rule-update', {
rule: buildRuleUpdate(rule, actions),
});
+ deferredTicks.get(scheduleId)?.();
}
}
}
-async function importBudgets(data: Budget, entityIdMap: Map<string, string>) {
+async function importBudgets(
+ data: Budget,
+ entityIdMap: Map<string, string>,
+ tick: ImportTick,
+) {
// There should be info in the docs to deal with
// no credit card category and how YNAB and Actual
// handle differently the amount To be Budgeted
@@ -1135,6 +1177,7 @@ async function importBudgets(data: Budget, entityIdMap: Map<string, string>) {
});
}),
);
+ tick();
}
} finally {
await send('api/batch-budget-end');
@@ -1165,29 +1208,54 @@ export async function doImport(data: Budget) {
const entityIdMap = new Map<string, string>();
const flagNameConflicts = getFlagNameConflicts(data);
- logger.log('Importing Accounts...');
- await importAccounts(data, entityIdMap);
-
- logger.log('Importing Categories...');
- await importCategories(data, entityIdMap);
-
- logger.log('Importing Payees...');
- await importPayees(data, entityIdMap);
-
- logger.log('Importing Payee Locations...');
- await importPayeeLocations(data, entityIdMap);
-
- logger.log('Importing Tags...');
- await importFlagsAsTags(data, flagNameConflicts);
-
- logger.log('Importing Transactions...');
- await importTransactions(data, entityIdMap, flagNameConflicts);
-
- logger.log('Importing Scheduled Transactions...');
- await importScheduledTransactions(data, entityIdMap, flagNameConflicts);
-
- logger.log('Importing Budgets...');
- await importBudgets(data, entityIdMap);
+ await runImportSteps([
+ {
+ step: 'accounts',
+ total: countLive(data.accounts),
+ run: tick => importAccounts(data, entityIdMap, tick),
+ },
+ {
+ step: 'categories',
+ total: countLive(data.category_groups) + countLive(data.categories),
+ run: tick => importCategories(data, entityIdMap, tick),
+ },
+ {
+ step: 'payees',
+ total: data.payees.filter(
+ payee => !payee.deleted && !payee.transfer_account_id,
+ ).length,
+ run: tick => importPayees(data, entityIdMap, tick),
+ },
+ {
+ step: 'payee-locations',
+ total: countLive(data.payee_locations ?? []),
+ run: tick => importPayeeLocations(data, entityIdMap, tick),
+ },
+ {
+ step: 'tags',
+ total: getTagsToCreate(data, flagNameConflicts).size,
+ run: tick => importFlagsAsTags(data, flagNameConflicts, tick),
+ },
+ {
+ step: 'transactions',
+ total: data.transactions.length,
+ run: tick =>
+ importTransactions(data, entityIdMap, flagNameConflicts, tick),
+ },
+ {
+ step: 'scheduled-transactions',
+ total: data.scheduled_transactions.length,
+ run: tick =>
+ importScheduledTransactions(data, entityIdMap, flagNameConflicts, tick),
+ },
+ {
+ step: 'budgets',
+ total: data.months.length,
+ run: tick => importBudgets(data, entityIdMap, tick),
+ },
+ ]);
+}
- logger.log('Setting up...');
+function countLive(entities: { deleted?: boolean }[]) {
+ return entities.filter(entity => !entity.deleted).length;
}
diff --git a/packages/loot-core/src/types/server-events.ts b/packages/loot-core/src/types/server-events.ts
index b80534606c..1a5d94e391 100644
--- a/packages/loot-core/src/types/server-events.ts
+++ b/packages/loot-core/src/types/server-events.ts
@@ -71,6 +71,32 @@ type OrphanedPayeesEvent = {
updatedPayeeIds: string[];
};
+export type ImportStep =
+ | 'accounts'
+ | 'categories'
+ | 'payees'
+ | 'payee-locations'
+ | 'tags'
+ | 'transactions'
+ | 'scheduled-transactions'
+ | 'budgets'
+ | 'finishing';
+
+type ImportProgressEvent = {
+ step: ImportStep;
+ /** Items imported so far in this step. `total` is 0 when it has no count. */
+ current: number;
+ total: number;
+ /** Items imported so far across every step, for an overall progress bar. */
+ overallCurrent: number;
+ overallTotal: number;
+ /**
+ * The batch that was just imported, when a step works in batches rather than
+ * one item at a time.
+ */
+ batch?: { amount: number; account: string };
+};
+
type PrefsUpdatedEvent = undefined;
type SchedulesOfflineEvent = undefined;
type ServerErrorEvent = undefined;
@@ -85,6 +111,7 @@ export type ServerEvents = {
'fallback-write-error': FallbackWriteErrorEvent;
'finish-import': FinishImportEvent;
'finish-load': FinishLoadEvent;
+ 'import-progress': ImportProgressEvent;
'indexeddb-quota-error': IndexeddbQuotaErrorEvent;
'orphaned-payees': OrphanedPayeesEvent;
'prefs-updated': PrefsUpdatedEvent;
diff --git a/upcoming-release-notes/budget-import-progress.md b/upcoming-release-notes/budget-import-progress.md
new file mode 100644
index 0000000000..337bf7bef8
--- /dev/null
+++ b/upcoming-release-notes/budget-import-progress.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [StephenBrown2]
+---
+
+Show a progress bar and the current stage while importing a budget from YNAB4 or nYNAB