Skip to content

A local-first personal finance app

Avoid repeated calls to payee.find in useDisplayPayee.ts (#5761)

thromer7a4799de9494
diff --git a/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx b/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx
index 222d646a92..643128ff80 100644
--- a/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx
+++ b/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx
@@ -75,6 +75,13 @@ vi.mock('../../hooks/usePayees', async importOriginal => {
return {
...actual,
usePayees: () => payees,
+ usePayeesById: () => {
+ const payeesById: Record<string, PayeeEntity> = {};
+ payees.forEach(payee => {
+ payeesById[payee.id] = payee;
+ });
+ return payeesById;
+ },
};
});
diff --git a/packages/desktop-client/src/hooks/useDisplayPayee.ts b/packages/desktop-client/src/hooks/useDisplayPayee.ts
index d412ab1094..5347520299 100644
--- a/packages/desktop-client/src/hooks/useDisplayPayee.ts
+++ b/packages/desktop-client/src/hooks/useDisplayPayee.ts
@@ -9,8 +9,7 @@ import {
} from 'loot-core/types/models';
import { useAccounts } from './useAccounts';
-import { usePayee } from './usePayee';
-import { usePayees } from './usePayees';
+import { usePayeesById } from './usePayees';
import { useTransactions } from './useTransactions';
type Counts = {
@@ -34,8 +33,8 @@ export function useDisplayPayee({ transaction }: UseDisplayPayeeProps) {
});
const accounts = useAccounts();
- const payees = usePayees();
- const payee = usePayee(transaction?.payee || '');
+ const payeesById = usePayeesById();
+ const payee = payeesById[transaction?.payee || ''];
return useMemo(() => {
if (subtransactions.length === 0) {
@@ -44,9 +43,7 @@ export function useDisplayPayee({ transaction }: UseDisplayPayeeProps) {
transaction,
payee,
transferAccount: accounts.find(
- a =>
- a.id ===
- payees.find(p => p.id === transaction?.payee)?.transfer_acct,
+ a => a.id === payeesById[transaction?.payee || '']?.transfer_acct,
),
});
}
@@ -73,9 +70,7 @@ export function useDisplayPayee({ transaction }: UseDisplayPayeeProps) {
return t('Split (no payee)');
}
- const mostCommonPayee = payees.find(
- p => p.id === mostCommonPayeeTransaction.payee,
- );
+ const mostCommonPayee = payeesById[mostCommonPayeeTransaction.payee || ''];
if (!mostCommonPayee) {
return t('Split (no payee)');
@@ -90,12 +85,11 @@ export function useDisplayPayee({ transaction }: UseDisplayPayeeProps) {
transferAccount: accounts.find(
a =>
a.id ===
- payees.find(p => p.id === mostCommonPayeeTransaction.payee)
- ?.transfer_acct,
+ payeesById[mostCommonPayeeTransaction.payee || '']?.transfer_acct,
),
numHiddenPayees: numDistinctPayees - 1,
});
- }, [subtransactions, payees, accounts, transaction, payee, t]);
+ }, [subtransactions, payeesById, accounts, transaction, payee, t]);
}
type GetPrettyPayeeProps = {
diff --git a/packages/desktop-client/src/hooks/usePayees.ts b/packages/desktop-client/src/hooks/usePayees.ts
index ddb3e7e47a..ba584d0148 100644
--- a/packages/desktop-client/src/hooks/usePayees.ts
+++ b/packages/desktop-client/src/hooks/usePayees.ts
@@ -2,7 +2,11 @@ import { useEffect } from 'react';
import { useInitialMount } from './useInitialMount';
-import { getCommonPayees, getPayees } from '@desktop-client/payees/payeesSlice';
+import {
+ getCommonPayees,
+ getPayees,
+ getPayeesById,
+} from '@desktop-client/payees/payeesSlice';
import { useSelector, useDispatch } from '@desktop-client/redux';
export function useCommonPayees() {
@@ -34,3 +38,8 @@ export function usePayees() {
return useSelector(state => state.payees.payees);
}
+
+export function usePayeesById() {
+ const payees = usePayees();
+ return getPayeesById(payees);
+}
diff --git a/upcoming-release-notes/5761.md b/upcoming-release-notes/5761.md
new file mode 100644
index 0000000000..955ebc6398
--- /dev/null
+++ b/upcoming-release-notes/5761.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [thromer]
+---
+Optimise the way payee information is fetched.
+