1
1
import { useMemo } from 'react' ;
2
2
3
3
import { useTransactions } from 'loot-core/client/data-hooks/transactions' ;
4
- import { getPayeesById } from 'loot-core/client/queries/queriesSlice' ;
5
4
import { q } from 'loot-core/shared/query' ;
6
5
import {
7
6
type AccountEntity ,
@@ -20,7 +19,7 @@ type Counts = {
20
19
} ;
21
20
22
21
type UseDisplayPayeeProps = {
23
- transaction : TransactionEntity ;
22
+ transaction ? : TransactionEntity | undefined ;
24
23
} ;
25
24
26
25
export function useDisplayPayee ( { transaction } : UseDisplayPayeeProps ) {
@@ -44,7 +43,7 @@ export function useDisplayPayee({ transaction }: UseDisplayPayeeProps) {
44
43
transferAccount : accounts . find (
45
44
a =>
46
45
a . id ===
47
- payees . find ( p => p . id === transaction . payee ) ?. transfer_acct ,
46
+ payees . find ( p => p . id === transaction ? .payee ) ?. transfer_acct ,
48
47
) ,
49
48
} ) ;
50
49
}
@@ -71,8 +70,13 @@ export function useDisplayPayee({ transaction }: UseDisplayPayeeProps) {
71
70
return 'Split (no payee)' ;
72
71
}
73
72
74
- const mostCommonPayee =
75
- getPayeesById ( payees ) [ mostCommonPayeeTransaction . payee ] ;
73
+ const mostCommonPayee = payees . find (
74
+ p => p . id === mostCommonPayeeTransaction . payee ,
75
+ ) ;
76
+
77
+ if ( ! mostCommonPayee ) {
78
+ return 'Split (no payee)' ;
79
+ }
76
80
77
81
const numDistinctPayees = Object . keys ( counts ) . length ;
78
82
@@ -87,14 +91,14 @@ export function useDisplayPayee({ transaction }: UseDisplayPayeeProps) {
87
91
) ,
88
92
numHiddenPayees : numDistinctPayees - 1 ,
89
93
} ) ;
90
- } , [ subtransactions , payees ] ) ;
94
+ } , [ subtransactions , payees , accounts , transaction , payee ] ) ;
91
95
}
92
96
93
97
type GetPrettyPayeeProps = {
94
- transaction : TransactionEntity ;
95
- payee : PayeeEntity ;
96
- transferAccount : AccountEntity | null ;
97
- numHiddenPayees ?: number ;
98
+ transaction ? : TransactionEntity | undefined ;
99
+ payee ? : PayeeEntity | undefined ;
100
+ transferAccount ? : AccountEntity | undefined ;
101
+ numHiddenPayees ?: number | undefined ;
98
102
} ;
99
103
100
104
function getPrettyPayee ( {
@@ -103,6 +107,10 @@ function getPrettyPayee({
103
107
transferAccount,
104
108
numHiddenPayees = 0 ,
105
109
} : GetPrettyPayeeProps ) {
110
+ if ( ! transaction ) {
111
+ return '' ;
112
+ }
113
+
106
114
const formatPayeeName = ( payeeName : string ) =>
107
115
numHiddenPayees > 0 ? `${ payeeName } (+${ numHiddenPayees } more)` : payeeName ;
108
116
0 commit comments