Skip to content

Commit 57222f4

Browse files
committed
introduce stars
1 parent 0fcc1e1 commit 57222f4

File tree

8 files changed

+51
-18
lines changed

8 files changed

+51
-18
lines changed

app/features/accounts/account-hooks.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
type CashuAccount,
2020
type ExtendedAccount,
2121
getAccountBalance,
22+
isStarAccount,
2223
} from './account';
2324
import {
2425
type AccountRepository,
@@ -277,6 +278,8 @@ export const accountsQueryOptions = ({
277278
export 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+
*/
433451
export 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
}
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { LandmarkIcon, Zap } from 'lucide-react';
1+
import { LandmarkIcon, StarIcon, Zap } from 'lucide-react';
22
import type { ReactNode } from 'react';
33
import type { AccountType } from './account';
44

55
const CashuIcon = () => <LandmarkIcon className="h-4 w-4" />;
66
const NWCIcon = () => <Zap className="h-4 w-4" />;
7+
const StarsIcon = () => <StarIcon className="h-4 w-4" />;
78

8-
const iconsByAccountType: Record<AccountType, ReactNode> = {
9+
const iconsByAccountType: Record<AccountType | 'star', ReactNode> = {
910
cashu: <CashuIcon />,
1011
nwc: <NWCIcon />,
12+
star: <StarsIcon />,
1113
};
1214

13-
export function AccountTypeIcon({ type }: { type: AccountType }) {
15+
export function AccountTypeIcon({ type }: { type: AccountType | 'star' }) {
1416
return iconsByAccountType[type];
1517
}

app/features/accounts/account-selector.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ScrollArea } from '~/components/ui/scroll-area';
1212
import { cn } from '~/lib/utils';
1313
import { MoneyWithConvertedAmount } from '../shared/money-with-converted-amount';
1414
import { type Account, getAccountBalance } from './account';
15+
import { isStarAccount } from './account';
1516
import { AccountTypeIcon } from './account-icons';
1617

1718
export type AccountWithBadges<T extends Account = Account> = T & {
@@ -26,7 +27,7 @@ function AccountItem({ account }: { account: AccountWithBadges }) {
2627

2728
return (
2829
<div className="flex w-full items-center gap-4 px-3 py-4">
29-
<AccountTypeIcon type={account.type} />
30+
<AccountTypeIcon type={isStarAccount(account) ? 'star' : account.type} />
3031
<div className="flex w-full flex-col justify-between gap-2 text-start">
3132
<span className="font-medium">{account.name}</span>
3233
<div className="flex items-center justify-between text-xs">

app/features/accounts/account.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ export const getAccountBalance = (account: Account) => {
5757
// TODO: implement balance logic for other account types
5858
return new Money({ amount: 0, currency: account.currency });
5959
};
60+
61+
export const isStarAccount = (account: Account) =>
62+
account.type === 'cashu' && account.wallet.cachedMintInfo.internalMeltsOnly;

app/features/receive/receive-cashu-token-service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
getCashuUnit,
77
getCashuWallet,
88
} from '~/lib/cashu';
9-
import type { ExtendedCashuAccount } from '../accounts/account';
9+
import { type ExtendedCashuAccount, isStarAccount } from '../accounts/account';
1010
import {
1111
allMintKeysetsQueryOptions,
1212
cashuMintValidator,
@@ -198,8 +198,9 @@ export class ReceiveCashuTokenService {
198198
sourceAccount: CashuAccountWithTokenFlags,
199199
otherAccounts: CashuAccountWithTokenFlags[],
200200
): CashuAccountWithTokenFlags[] {
201-
if (sourceAccount.isTestMint) {
201+
if (sourceAccount.isTestMint || isStarAccount(sourceAccount)) {
202202
// Tokens sourced from test mint can only be claimed to the same mint
203+
// Tokens sourced from Star accounts cannot pay external invoices
203204
return sourceAccount.isSelectable ? [sourceAccount] : [];
204205
}
205206
return [sourceAccount, ...otherAccounts].filter(

app/features/receive/receive-cashu-token.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ export default function ReceiveToken({
208208
<AccountSelector
209209
accounts={selectableAccounts}
210210
selectedAccount={receiveAccount}
211-
disabled={isCrossMintSwapDisabled}
211+
disabled={
212+
isCrossMintSwapDisabled || selectableAccounts.length === 1
213+
}
212214
onSelect={setReceiveAccount}
213215
/>
214216
</div>

app/features/settings/accounts/all-accounts.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import type { Currency } from '~/lib/money';
1414
import { LinkWithViewTransition } from '~/lib/transitions';
1515

1616
function CurrencyAccounts({ currency }: { currency: Currency }) {
17-
const { data: accounts } = useAccounts({ currency });
17+
const { data: accounts } = useAccounts({
18+
currency,
19+
excludeStarAccounts: true,
20+
});
1821

1922
return (
2023
<div className="space-y-3">

app/features/settings/settings.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useToast } from '~/hooks/use-toast';
1818
import { canShare, shareContent } from '~/lib/share';
1919
import { LinkWithViewTransition } from '~/lib/transitions';
2020
import { cn } from '~/lib/utils';
21+
import { isStarAccount } from '../accounts/account';
2122
import { useDefaultAccount } from '../accounts/account-hooks';
2223
import { AccountTypeIcon } from '../accounts/account-icons';
2324
import { ColorModeToggle } from '../theme/color-mode-toggle';
@@ -111,7 +112,9 @@ export default function Settings() {
111112
</SettingsNavButton>
112113

113114
<SettingsNavButton to="/settings/accounts">
114-
<AccountTypeIcon type={defaultAccount.type} />
115+
<AccountTypeIcon
116+
type={isStarAccount(defaultAccount) ? 'star' : defaultAccount.type}
117+
/>
115118
<span>{defaultAccount.name}</span>
116119
</SettingsNavButton>
117120

0 commit comments

Comments
 (0)