A local-first personal finance app
[AI] fix transaction search treating digits inside a word as an amount (#9232)
abakarovya6a989823b65
diff --git a/packages/desktop-client/src/components/accounts/Account.tsx b/packages/desktop-client/src/components/accounts/Account.tsxindex 0a740e40c9..a249583808 100644--- a/packages/desktop-client/src/components/accounts/Account.tsx+++ b/packages/desktop-client/src/components/accounts/Account.tsx@@ -60,6 +60,7 @@ import { useAccounts } from '#hooks/useAccounts';import { SchedulesProvider } from '#hooks/useCachedSchedules';import { useCategories } from '#hooks/useCategories';import { useDateFormat } from '#hooks/useDateFormat';+import { useFormat } from '#hooks/useFormat';import { useLocalPref } from '#hooks/useLocalPref';import { usePayees } from '#hooks/usePayees';import { getSchedulesQuery } from '#hooks/useSchedules';@@ -261,6 +262,7 @@ type AccountInternalProps = {categoryId?: string;location: ReturnType<typeof useLocation>;dateFormat: ReturnType<typeof useDateFormat>;+ currencySymbol: string;payees: PayeeEntity[];categoryGroups: CategoryGroupEntity[];hideFraction: boolean;@@ -660,6 +662,7 @@ class AccountInternal extends PureComponent<this.currentQuery,this.state.search,this.props.dateFormat,+ this.props.currencySymbol,),true,);@@ -2127,6 +2130,7 @@ export function Account() {const { data: accounts = [] } = useAccounts();const { data: payees = [] } = usePayees();const dateFormat = useDateFormat() || 'MM/dd/yyyy';+ const currencySymbol = useFormat().currency.symbol;const [hideFraction] = useSyncedPref('hideFraction');const [expandSplits] = useLocalPref('expand-splits');const [showNetWorthChart, setShowNetWorthChart] = useSyncedPref(@@ -2187,6 +2191,7 @@ export function Account() {matchedTransactions={matchedTransactions}accounts={accounts}dateFormat={dateFormat}+ currencySymbol={currencySymbol}hideFraction={String(hideFraction) === 'true'}expandSplits={expandSplits}showBalances={showBalances}diff --git a/packages/desktop-client/src/components/mobile/accounts/AllAccountTransactions.tsx b/packages/desktop-client/src/components/mobile/accounts/AllAccountTransactions.tsxindex f69b6d9ca7..f64a28569f 100644--- a/packages/desktop-client/src/components/mobile/accounts/AllAccountTransactions.tsx+++ b/packages/desktop-client/src/components/mobile/accounts/AllAccountTransactions.tsx@@ -13,6 +13,7 @@ import type {import { TransactionListWithBalances } from '#components/mobile/transactions/TransactionListWithBalances';import { SchedulesProvider } from '#hooks/useCachedSchedules';import { useDateFormat } from '#hooks/useDateFormat';+import { useFormat } from '#hooks/useFormat';import { useNavigate } from '#hooks/useNavigate';import { usePreviewTransactions } from '#hooks/usePreviewTransactions';import { getSchedulesQuery } from '#hooks/useSchedules';@@ -43,6 +44,7 @@ function TransactionListWithPreviews() {const [hideReconciled] = useSyncedPref('hide-reconciled-all');const dateFormat = useDateFormat() || 'MM/dd/yyyy';+ const currencySymbol = useFormat().currency.symbol;const makeRootTransactionsQuery = useCallback(() => {let query = queries.transactions().options({ splits: 'all' }).select('*');@@ -83,7 +85,12 @@ function TransactionListWithPreviews() {setCurrentQuery(query);setTransactionsQuery(searchText !== ''- ? queries.transactionsSearch(query, searchText, dateFormat)+ ? queries.transactionsSearch(+ query,+ searchText,+ dateFormat,+ currencySymbol,+ ): query,);}diff --git a/packages/desktop-client/src/hooks/useTransactionsSearch.ts b/packages/desktop-client/src/hooks/useTransactionsSearch.tsindex ab207b070e..932969aab4 100644--- a/packages/desktop-client/src/hooks/useTransactionsSearch.ts+++ b/packages/desktop-client/src/hooks/useTransactionsSearch.ts@@ -3,6 +3,7 @@ import { useEffect, useMemo, useRef, useState } from 'react';import type { Query } from '@actual-app/core/shared/query';import { debounce } from 'es-toolkit/compat';+import { useFormat } from '#hooks/useFormat';import * as queries from '#queries';type UseTransactionsSearchProps = {@@ -23,6 +24,7 @@ export function useTransactionsSearch({delayMs = 150,}: UseTransactionsSearchProps): UseTransactionsSearchResult {const [isSearching, setIsSearching] = useState(false);+ const currencySymbol = useFormat().currency.symbol;const updateQueryRef = useRef(updateQuery);updateQueryRef.current = updateQuery;@@ -39,12 +41,17 @@ export function useTransactionsSearch({} else if (searchText) {resetQueryRef.current?.();updateQueryRef.current(previousQuery =>- queries.transactionsSearch(previousQuery, searchText, dateFormat),+ queries.transactionsSearch(+ previousQuery,+ searchText,+ dateFormat,+ currencySymbol,+ ),);setIsSearching(true);}}, delayMs),- [dateFormat, delayMs, resetQueryRef, updateQueryRef],+ [currencySymbol, dateFormat, delayMs, resetQueryRef, updateQueryRef],);useEffect(() => {diff --git a/packages/desktop-client/src/queries/index.test.ts b/packages/desktop-client/src/queries/index.test.tsnew file mode 100644index 0000000000..07e2042cec--- /dev/null+++ b/packages/desktop-client/src/queries/index.test.ts@@ -0,0 +1,51 @@+import { q } from '@actual-app/core/shared/query';++import { transactionsSearch } from './index';++describe('transactionsSearch', () => {+ it.each(['XF 35mm', '12kr34'])(+ 'searches %s as text without matching amounts',+ search => {+ const query = transactionsSearch(+ q('transactions'),+ search,+ 'yyyy-MM-dd',+ 'kr',+ );++ expect(query.serialize().filterExpressions).toMatchObject([+ {+ $or: {+ notes: { $like: `%${search}%` },+ 'payee.name': { $like: `%${search}%` },+ $or: [],+ },+ },+ ]);+ },+ );++ it.each([+ ['35', '', 3500],+ ['$12.50', '$', 1250],+ ['12,50 kr', 'kr', 1250],+ ['chf 12.50', 'CHF', 1250],+ ])('still matches the amount for %s', (search, currencySymbol, amount) => {+ const query = transactionsSearch(+ q('transactions'),+ search,+ 'yyyy-MM-dd',+ currencySymbol,+ );++ expect(query.serialize().filterExpressions).toMatchObject([+ {+ $or: {+ $or: expect.arrayContaining([+ { amount: { $transform: '$abs', $eq: amount } },+ ]),+ },+ },+ ]);+ });+});diff --git a/packages/desktop-client/src/queries/index.ts b/packages/desktop-client/src/queries/index.tsindex 63d8e1b58b..7d35efd8fb 100644--- a/packages/desktop-client/src/queries/index.ts+++ b/packages/desktop-client/src/queries/index.ts@@ -15,6 +15,7 @@ import type { AccountEntity } from '@actual-app/core/types/models';import type { SyncedPrefs } from '@actual-app/core/types/prefs';// @ts-strict-ignoreimport { isValid as isDateValid, parse as parseDate } from 'date-fns';+import { escapeRegExp } from 'es-toolkit';export function accountFilter(accountId?:@@ -84,8 +85,24 @@ export function transactionsSearch(currentQuery: Query,search: string,dateFormat: SyncedPrefs['dateFormat'],+ currencySymbol: string,) {- const amount = currencyToAmount(search);+ // currencyToAmount ignores letters, so "35mm" would match the amount 35.+ // Letters make a search text-only, except a leading or trailing currency+ // symbol ("120 kr", "-CHF 12").+ const symbol = escapeRegExp(currencySymbol);+ const amountSearch = currencySymbol+ ? search.replace(+ new RegExp(+ `^([^\\p{L}\\d]*)${symbol}|${symbol}(?=[^\\p{L}\\d]*$)`,+ 'iu',+ ),+ '$1',+ )+ : search;+ const amount = /\p{L}/u.test(amountSearch)+ ? null+ : currencyToAmount(amountSearch.trim());const escapedSearch = search.replace(/[\\%?]/g, '\\$&');// Support various date formatsdiff --git a/upcoming-release-notes/transaction-search-word-digits.md b/upcoming-release-notes/transaction-search-word-digits.mdnew file mode 100644index 0000000000..f58fe6a93e--- /dev/null+++ b/upcoming-release-notes/transaction-search-word-digits.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [abakarovy]+---++Fix transaction search treating digits inside words like `35mm` as an amount