Skip to content

Commit

Permalink
fix(llm): improve typing of ReceiveFunds/02-SelectAccount and fix cra…
Browse files Browse the repository at this point in the history
…sh case (#7148)
  • Loading branch information
gre committed Jun 24, 2024
1 parent 231c1ca commit 33d26a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-forks-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": patch
---

bugfix crash case in ReceiveFunds/02-SelectAccount
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSelector } from "react-redux";

import { Button, Flex, Text } from "@ledgerhq/native-ui";
import { useTranslation } from "react-i18next";
import { Account, AccountLike, SubAccount, TokenAccount } from "@ledgerhq/types-live";
import { Account, SubAccount, TokenAccount } from "@ledgerhq/types-live";
import { makeEmptyTokenAccount } from "@ledgerhq/live-common/account/index";
import { flattenAccountsByCryptoCurrencyScreenSelector } from "~/reducers/accounts";
import { NavigatorName, ScreenName } from "~/const";
Expand All @@ -25,6 +25,8 @@ type SubAccountEnhanced = SubAccount & {
triggerCreateAccount: boolean;
};

type AccountLikeEnhanced = SubAccountEnhanced | Account | TokenAccount;

type NavigationProps = BaseComposite<
StackNavigatorProps<AccountsNavigatorParamList, ScreenName.ReceiveSelectAccount>
>;
Expand Down Expand Up @@ -54,21 +56,22 @@ function ReceiveSelectAccount({
const aggregatedAccounts = useMemo(
() =>
currency && currency.type === "TokenCurrency"
? parentAccounts!.reduce<AccountLike[]>((accs, pa) => {
const tokenAccounts = (pa as Account).subAccounts
? (pa as Account).subAccounts?.filter(
acc => acc.type === "TokenAccount" && acc.token.id === currency.id,
)
: [];
? parentAccounts!.reduce<AccountLikeEnhanced[]>((accs, pa) => {
const tokenAccounts =
pa.type === "Account" && pa.subAccounts
? pa.subAccounts?.filter(
acc => acc.type === "TokenAccount" && acc.token.id === currency.id,
)
: [];

if (tokenAccounts && tokenAccounts.length > 0) {
accs.push(...tokenAccounts);
} else {
const tokenAcc = makeEmptyTokenAccount(pa as Account, currency);
} else if (pa.type === "Account") {
const tokenAcc = makeEmptyTokenAccount(pa, currency);

const tokenA: SubAccountEnhanced = {
...tokenAcc,
parentAccount: pa as Account,
parentAccount: pa,
triggerCreateAccount: true,
};

Expand All @@ -82,16 +85,16 @@ function ReceiveSelectAccount({
);

const selectAccount = useCallback(
(account: AccountLike) => {
(account: AccountLikeEnhanced) => {
if (currency) {
track("account_clicked", {
asset: currency.name,
page: "Select account to deposit to",
});
navigation.navigate(ScreenName.ReceiveConfirmation, {
...route.params,
accountId: (account as SubAccountEnhanced)?.parentId || account.id,
createTokenAccount: (account as SubAccountEnhanced)?.triggerCreateAccount,
accountId: (account.type !== "Account" && account?.parentId) || account.id,
createTokenAccount: "triggerCreateAccount" in account && account?.triggerCreateAccount,
});
}
},
Expand All @@ -101,21 +104,12 @@ function ReceiveSelectAccount({
const walletState = useSelector(walletSelector);

const renderItem = useCallback(
({ item }: ListRenderItemInfo<AccountLike>) => (
({ item }: ListRenderItemInfo<AccountLikeEnhanced>) => (
<Flex px={6}>
<AccountCard
account={item}
AccountSubTitle={
(item as SubAccountEnhanced).parentAccount ||
(item as TokenAccount).token?.parentCurrency ? (
<Text color="neutral.c80">
{accountNameWithDefaultSelector(
walletState,
((item as SubAccountEnhanced).parentAccount as Account) ||
(item as TokenAccount).token.parentCurrency,
)}
</Text>
) : null
<Text color="neutral.c80">{accountNameWithDefaultSelector(walletState, item)}</Text>
}
onPress={() => selectAccount(item)}
/>
Expand Down Expand Up @@ -144,7 +138,7 @@ function ReceiveSelectAccount({
}
}, [currency, navigationAccount]);

const keyExtractor = useCallback((item: AccountLike) => item?.id, []);
const keyExtractor = useCallback((item: AccountLikeEnhanced) => item?.id, []);

return currency && aggregatedAccounts && aggregatedAccounts.length > 0 ? (
<>
Expand Down

0 comments on commit 33d26a9

Please sign in to comment.