Skip to content

A local-first personal finance app

fix: use selected file for option-driven re-parses in import modal (#8929) (#9401)

Hrushikesh Ramilla51880c71f007
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.test.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.test.tsx
new file mode 100644
index 0000000000..e143d8b6f6
--- /dev/null
+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.test.tsx
@@ -0,0 +1,318 @@
+import { send } from '@actual-app/core/platform/client/connection';
+import type { ParseFileOptions } from '@actual-app/core/server/transactions/import/parse-file';
+import { fireEvent, render, screen, waitFor } from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { TestProviders } from '#mocks';
+
+import {
+ ImportTransactionsModal,
+ shouldPreserveImportSettingsForParse,
+} from './ImportTransactionsModal';
+
+vi.mock('@actual-app/core/platform/client/connection', () => ({
+ send: vi.fn(),
+}));
+
+const mockPrefs = {};
+vi.mock('#hooks/useSyncedPrefs', () => ({
+ useSyncedPrefs: () => [mockPrefs, vi.fn()],
+}));
+
+const mockCategories = { data: { list: [] } };
+vi.mock('#hooks/useCategories', () => ({
+ useCategories: () => mockCategories,
+}));
+
+vi.mock('#hooks/useDateFormat', () => ({
+ useDateFormat: () => 'MM/dd/yyyy',
+}));
+
+vi.mock('#accounts', () => ({
+ useImportPreviewTransactionsMutation: () => ({
+ mutate: vi.fn((_args, { onSuccess }) => onSuccess?.([])),
+ mutateAsync: vi.fn().mockResolvedValue({ added: [], updated: [] }),
+ }),
+ useImportTransactionsMutation: () => ({
+ mutate: vi.fn(),
+ mutateAsync: vi.fn().mockResolvedValue({}),
+ }),
+}));
+
+vi.mock('#payees', () => ({
+ payeeQueries: {
+ all: () => ({
+ queryKey: ['payees'],
+ queryFn: vi.fn().mockResolvedValue([]),
+ }),
+ },
+}));
+
+describe('ImportTransactionsModal', () => {
+ const mockOpenFileDialog = vi.fn();
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ window.Actual = {
+ ...window.Actual,
+ openFileDialog: mockOpenFileDialog,
+ };
+ });
+
+ it('uses the selected file for option-driven re-parses and performs only one parse on file selection', async () => {
+ vi.mocked(send).mockResolvedValueOnce({
+ errors: [{ message: 'Bad file format' }],
+ transactions: [],
+ });
+
+ render(
+ <TestProviders>
+ <ImportTransactionsModal
+ filename="initial.csv"
+ accountId="acc1"
+ onImported={vi.fn()}
+ />
+ </TestProviders>,
+ );
+
+ await waitFor(() => {
+ expect(send).toHaveBeenCalledWith(
+ 'transactions-parse-file',
+ expect.objectContaining({ filepath: 'initial.csv' }),
+ );
+ });
+ expect(send).toHaveBeenCalledTimes(1);
+
+ vi.mocked(send).mockResolvedValue({
+ errors: [],
+ transactions: [{ Date: '2026-01-01', Payee: 'Store', Amount: '10.00' }],
+ });
+
+ mockOpenFileDialog.mockResolvedValueOnce(['new-file.csv']);
+ const selectNewButton = await screen.findByRole('button', {
+ name: /select new file/i,
+ });
+ await userEvent.click(selectNewButton);
+
+ await waitFor(() => {
+ expect(send).toHaveBeenLastCalledWith(
+ 'transactions-parse-file',
+ expect.objectContaining({ filepath: 'new-file.csv' }),
+ );
+ });
+ expect(send).toHaveBeenCalledTimes(2);
+
+ // Changing skipStartLines re-parses the new file, NOT initial.csv
+ const skipStartInput = await screen.findByLabelText(/skip start lines/i);
+ fireEvent.change(skipStartInput, { target: { value: '2' } });
+
+ await waitFor(() => {
+ expect(send).toHaveBeenLastCalledWith(
+ 'transactions-parse-file',
+ expect.objectContaining({
+ filepath: 'new-file.csv',
+ options: expect.objectContaining({ skipStartLines: 2 }),
+ }),
+ );
+ });
+ expect(send).toHaveBeenCalledTimes(3);
+
+ // Changing skipEndLines re-parses the new file, NOT initial.csv
+ const skipEndInput = screen.getByLabelText(/skip end lines/i);
+ fireEvent.change(skipEndInput, { target: { value: '1' } });
+
+ await waitFor(() => {
+ expect(send).toHaveBeenLastCalledWith(
+ 'transactions-parse-file',
+ expect.objectContaining({
+ filepath: 'new-file.csv',
+ options: expect.objectContaining({ skipEndLines: 1 }),
+ }),
+ );
+ });
+ expect(send).toHaveBeenCalledTimes(4);
+
+ // Toggling header row re-parses the new file as well
+ const headerRowToggle = screen.getByLabelText(/file has header row/i);
+ fireEvent.click(headerRowToggle);
+
+ await waitFor(() => {
+ expect(send).toHaveBeenLastCalledWith(
+ 'transactions-parse-file',
+ expect.objectContaining({
+ filepath: 'new-file.csv',
+ options: expect.objectContaining({ hasHeaderRow: false }),
+ }),
+ );
+ });
+ expect(send).toHaveBeenCalledTimes(5);
+ });
+
+ it('re-parses when the user re-selects the same file path after a parse error', async () => {
+ vi.mocked(send).mockResolvedValueOnce({
+ errors: [{ parsed: true, message: 'Parse error' }],
+ transactions: [],
+ });
+
+ render(
+ <TestProviders>
+ <ImportTransactionsModal
+ filename="initial.csv"
+ accountId="acc1"
+ onImported={vi.fn()}
+ />
+ </TestProviders>,
+ );
+
+ const selectNewButton = await screen.findByRole('button', {
+ name: /select new file/i,
+ });
+
+ await waitFor(() => {
+ expect(send).toHaveBeenCalledTimes(1);
+ });
+
+ vi.mocked(send).mockResolvedValueOnce({
+ errors: [],
+ transactions: [{ Date: '2026-01-01', Payee: 'Store', Amount: '10.00' }],
+ });
+
+ mockOpenFileDialog.mockResolvedValueOnce(['initial.csv']);
+ fireEvent.click(selectNewButton);
+
+ await waitFor(() => {
+ expect(send).toHaveBeenCalledTimes(2);
+ expect(send).toHaveBeenLastCalledWith(
+ 'transactions-parse-file',
+ expect.objectContaining({
+ filepath: 'initial.csv',
+ options: expect.objectContaining({
+ delimiter: ',',
+ hasHeaderRow: true,
+ }),
+ }),
+ );
+ });
+ });
+
+ it('re-parses with non-CSV options when re-selecting the same QIF file after error', async () => {
+ vi.mocked(send).mockResolvedValueOnce({
+ errors: [{ parsed: true, message: 'Parse error' }],
+ transactions: [],
+ });
+
+ render(
+ <TestProviders>
+ <ImportTransactionsModal
+ filename="initial.qif"
+ accountId="acc1"
+ onImported={vi.fn()}
+ />
+ </TestProviders>,
+ );
+
+ const selectNewButton = await screen.findByRole('button', {
+ name: /select new file/i,
+ });
+
+ await waitFor(() => {
+ expect(send).toHaveBeenCalledTimes(1);
+ });
+
+ vi.mocked(send).mockResolvedValueOnce({
+ errors: [],
+ transactions: [{ Date: '2026-01-01', Payee: 'Store', Amount: '10.00' }],
+ });
+
+ mockOpenFileDialog.mockResolvedValueOnce(['initial.qif']);
+ fireEvent.click(selectNewButton);
+
+ await waitFor(() => {
+ expect(send).toHaveBeenCalledTimes(2);
+ expect(send).toHaveBeenLastCalledWith(
+ 'transactions-parse-file',
+ expect.objectContaining({
+ filepath: 'initial.qif',
+ options: {
+ importNotes: true,
+ swapPayeeAndMemo: false,
+ },
+ }),
+ );
+ });
+ });
+
+ describe('shouldPreserveImportSettingsForParse', () => {
+ const baseOptions: ParseFileOptions = {
+ delimiter: ',',
+ hasHeaderRow: true,
+ skipStartLines: 0,
+ skipEndLines: 0,
+ fallbackMissingPayeeToMemo: false,
+ importNotes: true,
+ };
+
+ it('returns false when a new file is selected', () => {
+ const lastParse = {
+ filename: 'file1.csv',
+ fileType: 'csv',
+ options: baseOptions,
+ };
+
+ expect(
+ shouldPreserveImportSettingsForParse(
+ lastParse,
+ 'file2.csv',
+ 'csv',
+ baseOptions,
+ ),
+ ).toBe(false);
+ });
+
+ it('returns true when the same file only changes skipEndLines', () => {
+ const lastParse = {
+ filename: 'file1.csv',
+ fileType: 'csv',
+ options: baseOptions,
+ };
+
+ expect(
+ shouldPreserveImportSettingsForParse(lastParse, 'file1.csv', 'csv', {
+ ...baseOptions,
+ skipEndLines: 2,
+ }),
+ ).toBe(true);
+ });
+
+ it('returns false when other options change on the same file', () => {
+ const lastParse = {
+ filename: 'file1.csv',
+ fileType: 'csv',
+ options: baseOptions,
+ };
+
+ expect(
+ shouldPreserveImportSettingsForParse(lastParse, 'file1.csv', 'csv', {
+ ...baseOptions,
+ delimiter: ';',
+ }),
+ ).toBe(false);
+ });
+
+ it('returns false for non-csv file types', () => {
+ const lastParse = {
+ filename: 'file1.qif',
+ fileType: 'qif',
+ options: baseOptions,
+ };
+
+ expect(
+ shouldPreserveImportSettingsForParse(lastParse, 'file1.qif', 'qif', {
+ ...baseOptions,
+ skipEndLines: 1,
+ }),
+ ).toBe(false);
+ });
+ });
+});
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
index b1d59d279f..cd2dd3b137 100644
--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
@@ -184,7 +184,7 @@ const parseOptionKeys = [
'importNotes',
] satisfies Array<keyof ParseFileOptions>;
-function shouldPreserveImportSettingsForParse(
+export function shouldPreserveImportSettingsForParse(
lastParse: LastParse | null,
filename: string,
fileType: string,
@@ -478,7 +478,7 @@ export function ImportTransactionsModal({
}
useEffect(() => {
- const fileType = getFileType(originalFileName);
+ const fileType = getFileType(filename);
const parseOptions = getParseOptions(fileType, {
delimiter,
encoding: csvEncoding,
@@ -497,22 +497,22 @@ export function ImportTransactionsModal({
const lastParse = lastParseRef.current;
const shouldPreserveImportSettings = shouldPreserveImportSettingsForParse(
lastParse,
- originalFileName,
+ filename,
fileType,
parseOptions,
);
lastParseRef.current = {
- filename: originalFileName,
+ filename,
fileType,
options: parseOptions,
};
- void parse(originalFileName, parseOptions, {
+ void parse(filename, parseOptions, {
preserveImportSettings: shouldPreserveImportSettings,
});
}, [
- originalFileName,
+ filename,
delimiter,
csvEncoding,
hasHeaderRow,
@@ -562,24 +562,32 @@ export function ImportTransactionsModal({
],
});
- const fileType = getFileType(res[0]);
- const parseOptions = getParseOptions(fileType, {
- delimiter,
- encoding: csvEncoding,
- hasHeaderRow,
- skipStartLines,
- skipEndLines,
- fallbackMissingPayeeToMemo,
- importNotes,
- swapPayeeAndMemo: getSwapOption(
- fileType,
- ofxSwapPayeeAndMemo,
- qifSwapPayeeAndMemo,
- camtSwapPayeeAndMemo,
- ),
- });
-
- void parse(res[0], parseOptions);
+ if (res?.[0]) {
+ const nextFilename = res[0];
+ if (nextFilename === filename) {
+ const fileType = getFileType(nextFilename);
+ const options = getParseOptions(fileType, {
+ delimiter,
+ encoding: csvEncoding,
+ hasHeaderRow,
+ skipStartLines,
+ skipEndLines,
+ fallbackMissingPayeeToMemo,
+ importNotes,
+ swapPayeeAndMemo: getSwapOption(
+ fileType,
+ ofxSwapPayeeAndMemo,
+ qifSwapPayeeAndMemo,
+ camtSwapPayeeAndMemo,
+ ),
+ });
+ void parse(nextFilename, options, {
+ preserveImportSettings: false,
+ });
+ } else {
+ setFilename(nextFilename);
+ }
+ }
}
function onUpdateFields(field, name) {
diff --git a/upcoming-release-notes/import-modal-reparse-selected-file.md b/upcoming-release-notes/import-modal-reparse-selected-file.md
new file mode 100644
index 0000000000..4c6e3a54c4
--- /dev/null
+++ b/upcoming-release-notes/import-modal-reparse-selected-file.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [Hrushikesh-ramilla]
+---
+
+Use the current selected file for option-driven re-parses in the transaction import modal.