@@ -19,6 +19,7 @@ import {
1919 type CashuAccount ,
2020 type ExtendedAccount ,
2121 getAccountBalance ,
22+ isStarAccount ,
2223} from './account' ;
2324import {
2425 type AccountRepository ,
@@ -277,6 +278,8 @@ export const accountsQueryOptions = ({
277278export function useAccounts < T extends AccountType = AccountType > ( select ?: {
278279 currency ?: Currency ;
279280 type ?: T ;
281+ excludeStarAccounts ?: boolean ;
282+ starAccountsOnly ?: boolean ;
280283} ) : UseSuspenseQueryResult < ExtendedAccount < T > [ ] > {
281284 const user = useUser ( ) ;
282285 const accountRepository = useAccountRepository ( ) ;
@@ -301,13 +304,25 @@ export function useAccounts<T extends AccountType = AccountType>(select?: {
301304 if ( select . type && account . type !== select . type ) {
302305 return false ;
303306 }
307+ if ( select . excludeStarAccounts && isStarAccount ( account ) ) {
308+ return false ;
309+ }
310+ if ( select . starAccountsOnly && ! isStarAccount ( account ) ) {
311+ return false ;
312+ }
304313 return true ;
305314 } ,
306315 ) ;
307316
308317 return filteredData ;
309318 } ,
310- [ select ?. currency , select ?. type , user ] ,
319+ [
320+ select ?. currency ,
321+ select ?. type ,
322+ select ?. excludeStarAccounts ,
323+ select ?. starAccountsOnly ,
324+ user ,
325+ ] ,
311326 ) ,
312327 } ) ;
313328}
@@ -430,14 +445,17 @@ export function useAddCashuAccount() {
430445 return mutateAsync ;
431446}
432447
448+ /**
449+ * @returns the total balance of all accounts for the given currency excluding Star accounts.
450+ */
433451export function useBalance ( currency : Currency ) {
434- const { data : accounts } = useAccounts ( { currency } ) ;
435- const balance = accounts . reduce (
436- ( acc , account ) => {
437- const accountBalance = getAccountBalance ( account ) ;
438- return acc . add ( accountBalance ) ;
439- } ,
440- new Money ( { amount : 0 , currency } ) ,
441- ) ;
452+ const { data : accounts } = useAccounts ( {
453+ currency ,
454+ excludeStarAccounts : true ,
455+ } ) ;
456+ const balance = accounts . reduce ( ( acc , account ) => {
457+ const accountBalance = getAccountBalance ( account ) ;
458+ return acc . add ( accountBalance ) ;
459+ } , Money . zero ( currency ) ) ;
442460 return balance ;
443461}
0 commit comments