Skip to content

A local-first personal finance app

Only reverse import preview transactions if they appear to be sorted by date ascending (#6543)

Asherah Connorf5e1d5eab4bd
diff --git a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-1-chromium-linux.png b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-1-chromium-linux.png
index 237fc87386..cddf544933 100644
Binary files a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-1-chromium-linux.png and b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-2-chromium-linux.png b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-2-chromium-linux.png
index 5b26d43ec6..6eba250b72 100644
Binary files a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-2-chromium-linux.png and b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-3-chromium-linux.png b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-3-chromium-linux.png
index af95a9622c..9003a5b543 100644
Binary files a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-3-chromium-linux.png and b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-import-csv-file-twice-3-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-1-chromium-linux.png b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-1-chromium-linux.png
index e4da5c8424..b20ce50478 100644
Binary files a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-1-chromium-linux.png and b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-2-chromium-linux.png b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-2-chromium-linux.png
index c6f7af6f8a..d7556a6415 100644
Binary files a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-2-chromium-linux.png and b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-3-chromium-linux.png b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-3-chromium-linux.png
index d2bb79693f..90e4aef8a6 100644
Binary files a/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-3-chromium-linux.png and b/packages/desktop-client/e2e/accounts.test.ts-snapshots/Accounts-Import-Transactions-imports-transactions-from-a-CSV-file-3-chromium-linux.png differ
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
index 962e2c272d..5767179de7 100644
--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/ImportTransactionsModal.tsx
@@ -226,18 +226,30 @@ export function ImportTransactionsModal({
const getImportPreview = useCallback(
async (
- transactions,
- filetype,
- flipAmount,
- fieldMappings,
- splitMode,
+ transactions: ImportTransaction[],
+ filetype: string,
+ flipAmount: boolean,
+ fieldMappings: FieldMapping | null,
+ splitMode: boolean,
parseDateFormat: DateFormat,
- inOutMode,
- outValue,
- multiplierAmount,
+ inOutMode: boolean,
+ outValue: string,
+ multiplierAmount: string,
) => {
const previewTransactions = [];
const inOutModeEnabled = isOfxFile(filetype) ? false : inOutMode;
+ const getTransDate: (trans: ImportTransaction) => string | null =
+ isOfxFile(filetype)
+ ? trans => trans.date ?? null
+ : trans => parseDate(trans.date, parseDateFormat);
+
+ // Note that the sort will behave unpredictably if any date fails to parse.
+ transactions.sort((a, b) => {
+ const aDate = getTransDate(a);
+ const bDate = getTransDate(b);
+
+ return aDate < bDate ? 1 : aDate === bDate ? 0 : -1;
+ });
for (let trans of transactions) {
if (trans.isMatchedTransaction) {
@@ -249,9 +261,7 @@ export function ImportTransactionsModal({
? applyFieldMappings(trans, fieldMappings)
: trans;
- const date = isOfxFile(filetype)
- ? trans.date
- : parseDate(trans.date, parseDateFormat);
+ const date = getTransDate(trans);
if (date == null) {
console.log(
`Unable to parse date ${
@@ -431,12 +441,7 @@ export function ImportTransactionsModal({
setParseDateFormat(null);
}
- // Reverse the transactions because it's very common for them to
- // be ordered ascending, but we show transactions descending by
- // date. This is purely cosmetic.
- const reversedTransactions =
- transactions.reverse() as ImportTransaction[];
- setParsedTransactions(reversedTransactions);
+ setParsedTransactions(transactions as ImportTransaction[]);
}
setLoadingState(null);
diff --git a/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts b/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts
index c3b2c72a9c..53a58bd00c 100644
--- a/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts
+++ b/packages/desktop-client/src/components/modals/ImportTransactionsModal/utils.ts
@@ -27,7 +27,7 @@ export function isDateFormat(format: string): format is DateFormat {
export function parseDate(
str: string | number | null | Array<unknown> | object,
order: DateFormat,
-) {
+): string | null {
if (typeof str !== 'string') {
return null;
}
diff --git a/upcoming-release-notes/6543.md b/upcoming-release-notes/6543.md
new file mode 100644
index 0000000000..04ba754cc2
--- /dev/null
+++ b/upcoming-release-notes/6543.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [kivikakk]
+---
+
+Sort import preview transactions by (parsed) date, descending