Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions app/features/accounts/account-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,14 @@ export function useAccounts<T extends AccountType = AccountType>(select?: {
currency?: Currency;
type?: T;
isOnline?: boolean;
excludeStarAccounts?: boolean;
starAccountsOnly?: boolean;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having filter?: 'star' | 'non-star' or internalAccount?: boolean or something liek that makes better api. with this api caller can specify excludeStarAccounts: true and starAccountsOnly: true which doesn't make sense

}): UseSuspenseQueryResult<ExtendedAccount<T>[]> {
const user = useUser();
const accountRepository = useAccountRepository();

const { currency, type, isOnline } = select ?? {};
const { currency, type, isOnline, excludeStarAccounts, starAccountsOnly } =
select ?? {};

return useSuspenseQuery({
...accountsQueryOptions({ userId: user.id, accountRepository }),
Expand All @@ -280,13 +283,19 @@ export function useAccounts<T extends AccountType = AccountType>(select?: {
if (isOnline !== undefined && account.isOnline !== isOnline) {
return false;
}
if (excludeStarAccounts && account.isStarAccount) {
return false;
}
if (starAccountsOnly && !account.isStarAccount) {
return false;
}
return true;
},
);

return filteredData;
},
[currency, type, isOnline, user],
[currency, type, isOnline, excludeStarAccounts, starAccountsOnly, user],
),
});
}
Expand Down Expand Up @@ -409,15 +418,18 @@ export function useAddCashuAccount() {
return mutateAsync;
}

/**
* @returns the total balance of all accounts for the given currency excluding Star accounts.
*/
export function useBalance(currency: Currency) {
const { data: accounts } = useAccounts({ currency });
const balance = accounts.reduce(
(acc, account) => {
const accountBalance = getAccountBalance(account);
return acc.add(accountBalance);
},
new Money({ amount: 0, currency }),
);
const { data: accounts } = useAccounts({
currency,
excludeStarAccounts: true,
});
const balance = accounts.reduce((acc, account) => {
const accountBalance = getAccountBalance(account);
return acc.add(accountBalance);
}, Money.zero(currency));
return balance;
}

Expand Down
8 changes: 5 additions & 3 deletions app/features/accounts/account-icons.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { LandmarkIcon, Zap } from 'lucide-react';
import { LandmarkIcon, StarIcon, Zap } from 'lucide-react';
import type { ReactNode } from 'react';
import type { AccountType } from './account';

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

const iconsByAccountType: Record<AccountType, ReactNode> = {
const iconsByAccountType: Record<AccountType | 'star', ReactNode> = {
cashu: <CashuIcon />,
nwc: <NWCIcon />,
star: <StarsIcon />,
};

export function AccountTypeIcon({ type }: { type: AccountType }) {
export function AccountTypeIcon({ type }: { type: AccountType | 'star' }) {
return iconsByAccountType[type];
}
11 changes: 8 additions & 3 deletions app/features/accounts/account-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class AccountRepository {
proofs: string;
};

const { wallet, isOnline } = await this.getPreloadedWallet(
const { wallet, isOnline, isStarAccount } = await this.getPreloadedWallet(
details.mint_url,
data.currency,
);
Expand All @@ -181,6 +181,7 @@ export class AccountRepository {
keysetCounters: details.keyset_counters,
proofs: await this.encryption.decrypt<Proof[]>(details.proofs),
wallet,
isStarAccount,
} as T;
}

Expand Down Expand Up @@ -231,7 +232,7 @@ export class AccountRepository {
unit: getCashuUnit(currency),
bip39seed: seed ?? undefined,
});
return { wallet, isOnline: false };
return { wallet, isOnline: false, isStarAccount: undefined };
}
throw error;
}
Expand Down Expand Up @@ -266,7 +267,11 @@ export class AccountRepository {
// The constructor does not set the keysetId, so we need to set it manually
wallet.keysetId = activeKeyset.id;

return { wallet, isOnline: true };
return {
wallet,
isOnline: true,
isStarAccount: mintInfo.internalMeltsOnly,
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/features/accounts/account-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function AccountItem({ account }: { account: AccountSelectorOption }) {

return (
<div className="flex w-full items-center gap-4 px-3 py-4">
<AccountTypeIcon type={account.type} />
<AccountTypeIcon type={account.isStarAccount ? 'star' : account.type} />
<div className="flex w-full flex-col justify-between gap-2 text-start">
<span className="font-medium">{account.name}</span>
<div className="flex items-center justify-between text-xs">
Expand Down
1 change: 1 addition & 0 deletions app/features/accounts/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type Account = {
name: string;
type: AccountType;
isOnline: boolean;
isStarAccount?: boolean;
currency: Currency;
createdAt: string;
/**
Expand Down
2 changes: 1 addition & 1 deletion app/features/receive/receive-cashu-token-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class ReceiveCashuTokenService {
sourceAccount: CashuAccountWithTokenFlags,
otherAccounts: CashuAccountWithTokenFlags[],
): CashuAccountWithTokenFlags[] {
if (sourceAccount.isTestMint) {
if (sourceAccount.isTestMint || sourceAccount.isStarAccount) {
// Tokens sourced from test mint can only be claimed to the same mint
return sourceAccount.canReceive ? [sourceAccount] : [];
}
Expand Down
4 changes: 3 additions & 1 deletion app/features/receive/receive-cashu-token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ export default function ReceiveToken({
<AccountSelector
accounts={selectableAccounts}
selectedAccount={receiveAccount}
disabled={isCrossMintSwapDisabled}
disabled={
isCrossMintSwapDisabled || selectableAccounts.length === 1
}
onSelect={setReceiveAccount}
/>
</div>
Expand Down
5 changes: 4 additions & 1 deletion app/features/settings/accounts/all-accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import type { Currency } from '~/lib/money';
import { LinkWithViewTransition } from '~/lib/transitions';

function CurrencyAccounts({ currency }: { currency: Currency }) {
const { data: accounts } = useAccounts({ currency });
const { data: accounts } = useAccounts({
currency,
excludeStarAccounts: true,
});

return (
<div className="space-y-3">
Expand Down
4 changes: 3 additions & 1 deletion app/features/settings/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ export default function Settings() {
</SettingsNavButton>

<SettingsNavButton to="/settings/accounts">
<AccountTypeIcon type={defaultAccount.type} />
<AccountTypeIcon
type={defaultAccount.isStarAccount ? 'star' : defaultAccount.type}
/>
<span>{defaultAccount.name}</span>
</SettingsNavButton>

Expand Down