Skip to content

Commit f740132

Browse files
fix(extension): loading wallet during app init + switching [LW-13016] (#1902)
* fix(extension): loading wallet during app init + switching * feat(extension): add toast when switching wallets after removing a wallet * fix(extension): hide cardano accounts menu when bitcoin wallet is active
1 parent a316b60 commit f740132

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

apps/browser-extension-wallet/src/components/MainMenu/DropdownMenuOverlay/components/UserInfo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export const UserInfo = ({
173173
}
174174
{...(wallet.type !== WalletType.Script &&
175175
wallet.blockchainName !== 'Bitcoin' && {
176-
onOpenAccountsMenu: () => onOpenWalletAccounts(wallet),
176+
...(blockchain !== 'bitcoin' ? { onOpenAccountsMenu: () => onOpenWalletAccounts(wallet) } : {}),
177177
onOpenEditWallet: () => onOpenEditWallet(wallet)
178178
})}
179179
/>

apps/browser-extension-wallet/src/hooks/useAppInit.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { useObservable } from '@lace/common';
22
import { useWalletStore } from '@stores';
3-
import { useEffect } from 'react';
3+
import { useEffect, useMemo } from 'react';
44
import { isKeyHashAddress } from '@cardano-sdk/wallet';
55
import { AddressesDiscoveryStatus } from '@lib/communication/addresses-discoverer';
66
import { useWalletManager } from './useWalletManager';
77
import { useWalletState } from './useWalletState';
88
import { setBackgroundStorage } from '@lib/scripts/background/storage';
99
import { useCustomSubmitApi } from '@hooks/useCustomSubmitApi';
10+
import { bitcoinWalletManager } from '@lib/wallet-api-ui';
11+
import { useCurrentBlockchain } from '@src/multichain';
1012

1113
export const useAppInit = (): void => {
1214
const {
@@ -19,9 +21,10 @@ export const useAppInit = (): void => {
1921
setHdDiscoveryStatus,
2022
deletingWallet
2123
} = useWalletStore();
24+
const { blockchain } = useCurrentBlockchain();
2225
const { loadWallet, walletManager, walletRepository } = useWalletManager();
2326
const walletState = useWalletState();
24-
const { environmentName } = useWalletStore();
27+
const { environmentName, currentChain } = useWalletStore();
2528
const { getCustomSubmitApiForNetwork } = useCustomSubmitApi();
2629

2730
useEffect(() => {
@@ -56,7 +59,16 @@ export const useAppInit = (): void => {
5659
}, [environmentName, getCustomSubmitApiForNetwork]);
5760

5861
const wallets = useObservable(walletRepository.wallets$);
59-
const activeWalletProps = useObservable(walletManager.activeWalletId$);
62+
const activeCardanoWalletProps = useObservable(walletManager.activeWalletId$);
63+
const activeBitcoinWalletProps = useObservable(bitcoinWalletManager.activeWalletId$);
64+
const activeWalletProps = useMemo(
65+
() =>
66+
blockchain === 'bitcoin'
67+
? activeBitcoinWalletProps && currentChain && { ...activeBitcoinWalletProps, chainId: currentChain }
68+
: activeCardanoWalletProps,
69+
[blockchain, activeCardanoWalletProps, activeBitcoinWalletProps, currentChain]
70+
);
71+
6072
useEffect(() => {
6173
if (deletingWallet || typeof wallets === 'undefined' || typeof activeWalletProps === 'undefined') return;
6274
void loadWallet(wallets, activeWalletProps);

apps/browser-extension-wallet/src/hooks/useWalletManager.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ export const useWalletManager = (): UseWalletManager => {
669669
setWalletDisplayInfo(await getWalletInfo());
670670
}
671671
},
672-
[getCurrentChainId, backgroundService, setIsBitcoinWallet, setWalletDisplayInfo, getWalletInfo]
672+
[backgroundService, getCurrentChainId, setWalletDisplayInfo, getWalletInfo, setIsBitcoinWallet]
673673
);
674674

675675
const activateAnyWallet = useCallback(
@@ -688,11 +688,12 @@ export const useWalletManager = (): UseWalletManager => {
688688
* @returns resolves with wallet information or null when no wallet is found
689689
*/
690690
const loadWallet = useCallback(
691-
// eslint-disable-next-line complexity
691+
// eslint-disable-next-line complexity, max-statements
692692
async (
693693
wallets: AnyWallet<Wallet.WalletMetadata, Wallet.AccountMetadata>[],
694694
activeWalletProps: WalletManagerActivateProps | null
695695
): Promise<Wallet.CardanoWallet | undefined> => {
696+
const { activeBlockchain } = await backgroundService.getBackgroundStorage();
696697
// If there are no wallets, attempt to migrate local storage data to wallet repository
697698
if (wallets.length === 0) {
698699
if (!(await tryMigrateToWalletRepository())) {
@@ -727,6 +728,13 @@ export const useWalletManager = (): UseWalletManager => {
727728
activeWallet.type !== WalletType.Script
728729
? activeWallet.accounts.find((a) => a.accountIndex === activeWalletProps.accountIndex)
729730
: undefined;
731+
732+
if (activeBlockchain === 'bitcoin' && activeWallet) {
733+
setIsBitcoinWallet(true);
734+
setWalletDisplayInfo(await getWalletInfo());
735+
return;
736+
}
737+
730738
const newCardanoWallet = {
731739
name: activeWallet.metadata.name,
732740
signingCoordinator,
@@ -760,17 +768,19 @@ export const useWalletManager = (): UseWalletManager => {
760768
return newCardanoWallet;
761769
},
762770
[
763-
getWalletInfo,
764-
setCardanoWallet,
765-
setWalletDisplayInfo,
766-
setCurrentChain,
767-
tryMigrateToWalletRepository,
771+
backgroundService,
772+
currentChain?.networkMagic,
768773
cardanoWallet,
769-
currentChain,
770774
manageAccountsWallet,
771-
activateAnyWallet,
775+
tryMigrateToWalletRepository,
776+
setCardanoWallet,
777+
setWalletDisplayInfo,
772778
deletingWallet,
779+
activateAnyWallet,
780+
setCurrentChain,
773781
setCardanoCoin,
782+
setIsBitcoinWallet,
783+
getWalletInfo,
774784
setManageAccountsWallet
775785
]
776786
);

apps/browser-extension-wallet/src/stores/slices/wallet-info-slice.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { Wallet } from '@lace/cardano';
44
import { ObservableWalletState } from '@hooks/useWalletState';
55
import { isSharedWallet } from '@lace/core';
66
import { isNamiWallet } from '@src/views/nami-mode/utils';
7-
import { Bitcoin } from '@lace/bitcoin/';
87

98
/**
109
* has all wallet info related actions and states
@@ -52,11 +51,6 @@ export const walletInfoSlice: SliceCreator<WalletInfoSlice & BlockchainProviderS
5251
set({
5352
walletDisplayInfo: info
5453
}),
55-
setBitcoinWallet: (wallet?: Bitcoin.BitcoinWallet) =>
56-
set({
57-
bitcoinWallet: wallet,
58-
isBitcoinWallet: !!wallet
59-
}),
6054
setIsBitcoinWallet: (isBitcoinWallet: boolean) =>
6155
set({
6256
isBitcoinWallet

apps/browser-extension-wallet/src/views/browser-view/features/settings/components/SettingsRemoveWallet.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import React, { useEffect, useState } from 'react';
33
import { SettingsCard } from './';
44
import { useTranslation } from 'react-i18next';
55
import { Typography } from 'antd';
6-
import { Button, useObservable } from '@lace/common';
6+
import { Button, toast, useObservable } from '@lace/common';
77
import { WarningModal } from '@views/browser/components/WarningModal';
88
import styles from './SettingsLayout.module.scss';
9-
import { useWalletManager } from '@hooks';
9+
import { useWalletManager, TOAST_DEFAULT_DURATION } from '@hooks';
1010
import { useWalletStore } from '@src/stores';
1111
import { useBackgroundServiceAPIContext } from '@providers/BackgroundServiceAPI';
1212
import { BrowserViewSections } from '@lib/scripts/types';
@@ -59,8 +59,13 @@ export const SettingsRemoveWallet = ({ popupView }: { popupView?: boolean }): Re
5959
// eslint-disable-next-line camelcase
6060
$set: { wallet_accounts_quantity: await getWalletAccountsQtyString(walletRepository) }
6161
});
62-
const nextActiveWallet = await deleteWallet();
62+
const { walletId: nextActiveWalletId } = (await deleteWallet()) || {};
63+
const nextActiveWallet = wallets?.find(({ walletId }) => walletId === nextActiveWalletId);
6364
setDeletingWallet(false);
65+
toast.notify({
66+
duration: TOAST_DEFAULT_DURATION,
67+
text: t('multiWallet.activated.wallet', { walletName: nextActiveWallet.metadata.name })
68+
});
6469
if (nextActiveWallet) return;
6570
if (popupView) await backgroundServices.handleOpenBrowser({ section: BrowserViewSections.HOME });
6671
// force reload to ensure all stores are cleaned up

0 commit comments

Comments
 (0)