A local-first personal finance app
Fix CSV import settings reset when skipping end lines (#7827)
Henghui Zhu45ac4cb98bb5
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsxindex 8d3422b309..d4bba99d7a 100644--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx@@ -5,7 +5,13 @@ import type {ReactNode,SetStateAction,} from 'react';-import { useCallback, useEffect, useEffectEvent, useState } from 'react';+import {+ useCallback,+ useEffect,+ useEffectEvent,+ useRef,+ useState,+} from 'react';import { Trans, useTranslation } from 'react-i18next';import { Button, ButtonWithLoading } from '@actual-app/components/button';@@ -161,6 +167,40 @@ function getInitialMappings(transactions) {};}+type LastParse = {+ filename: string;+ fileType: string;+ options: ParseFileOptions;+};++const parseOptionKeys = [+ 'hasHeaderRow',+ 'delimiter',+ 'fallbackMissingPayeeToMemo',+ 'swapPayeeAndMemo',+ 'skipStartLines',+ 'skipEndLines',+ 'importNotes',+] satisfies Array<keyof ParseFileOptions>;++function shouldPreserveImportSettingsForParse(+ lastParse: LastParse | null,+ filename: string,+ fileType: string,+ options: ParseFileOptions,+) {+ return (+ fileType === 'csv' &&+ lastParse?.filename === filename &&+ lastParse.fileType === fileType &&+ parseOptionKeys.every(key =>+ key === 'skipEndLines'+ ? lastParse.options[key] !== options[key]+ : lastParse.options[key] === options[key],+ )+ );+}+export function ImportTransactionsModal({filename: originalFileName,accountId,@@ -239,6 +279,7 @@ export function ImportTransactionsModal({const [clearOnImport, setClearOnImport] = useState(true);const [startDate, setStartDate] = useState('');+ const lastParseRef = useRef<LastParse | null>(null);const getImportPreview = useCallback(async (@@ -332,7 +373,11 @@ export function ImportTransactionsModal({);const parse = useCallback(- async (filename: string, options: ParseFileOptions) => {+ async (+ filename: string,+ options: ParseFileOptions,+ { preserveImportSettings = false } = {},+ ) => {setLoadingState('parsing');const filetype = getFileType(filename);@@ -367,39 +412,46 @@ export function ImportTransactionsModal({message: errors[0].message || 'Internal error',});} else {- if (filetype === 'csv' || filetype === 'qif') {+ if (+ !preserveImportSettings &&+ (filetype === 'csv' || filetype === 'qif')+ ) {const flipAmount =String(prefs[`flip-amount-${accountId}-${filetype}`]) === 'true';setFlipAmount(flipAmount);}if (filetype === 'csv') {- let mappings = prefs[`csv-mappings-${accountId}`];- mappings = mappings- ? JSON.parse(mappings)- : getInitialMappings(transactions);-- // @ts-expect-error - mappings might not have outflow/inflow properties- setFieldMappings(mappings);-- // Set initial split mode based on any saved mapping- // @ts-expect-error - mappings might not have outflow/inflow properties- const splitMode = !!(mappings.outflow || mappings.inflow);- setSplitMode(splitMode);-- const parseDateFormat =- prefs[`parse-date-${accountId}-${filetype}`] ||- getInitialDateFormat(transactions, mappings);- setParseDateFormat(- isDateFormat(parseDateFormat) ? parseDateFormat : null,- );+ if (!preserveImportSettings) {+ let mappings = prefs[`csv-mappings-${accountId}`];+ mappings = mappings+ ? JSON.parse(mappings)+ : getInitialMappings(transactions);++ // @ts-expect-error - mappings might not have outflow/inflow properties+ setFieldMappings(mappings);++ // Set initial split mode based on any saved mapping+ // @ts-expect-error - mappings might not have outflow/inflow properties+ const splitMode = !!(mappings.outflow || mappings.inflow);+ setSplitMode(splitMode);++ const parseDateFormat =+ prefs[`parse-date-${accountId}-${filetype}`] ||+ getInitialDateFormat(transactions, mappings);+ setParseDateFormat(+ isDateFormat(parseDateFormat) ? parseDateFormat : null,+ );+ }} else if (filetype === 'qif') {- const parseDateFormat =- prefs[`parse-date-${accountId}-${filetype}`] ||- getInitialDateFormat(transactions, { date: 'date' });- setParseDateFormat(- isDateFormat(parseDateFormat) ? parseDateFormat : null,- );+ if (!preserveImportSettings) {+ const parseDateFormat =+ prefs[`parse-date-${accountId}-${filetype}`] ||+ getInitialDateFormat(transactions, { date: 'date' });+ setParseDateFormat(+ isDateFormat(parseDateFormat) ? parseDateFormat : null,+ );+ }} else {setFieldMappings(null);setParseDateFormat(null);@@ -437,8 +489,23 @@ export function ImportTransactionsModal({camtSwapPayeeAndMemo,),});+ const lastParse = lastParseRef.current;+ const shouldPreserveImportSettings = shouldPreserveImportSettingsForParse(+ lastParse,+ originalFileName,+ fileType,+ parseOptions,+ );++ lastParseRef.current = {+ filename: originalFileName,+ fileType,+ options: parseOptions,+ };- void parse(originalFileName, parseOptions);+ void parse(originalFileName, parseOptions, {+ preserveImportSettings: shouldPreserveImportSettings,+ });}, [originalFileName,delimiter,diff --git a/upcoming-release-notes/7827.md b/upcoming-release-notes/7827.mdnew file mode 100644index 0000000000..002a24e0c6--- /dev/null+++ b/upcoming-release-notes/7827.md@@ -0,0 +1,6 @@+---+category: Bugfixes+authors: [Hui66cs]+---++Fixed CSV import settings being reset when changing “Skip end lines”.