Skip to content

Commit e5fd7ec

Browse files
authored
fix: do not trigger create shared wallet more than once [LW-12780] (#1884)
1 parent f7090ea commit e5fd7ec

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

apps/browser-extension-wallet/src/views/browser-view/features/shared-wallet/SharedWallet.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,27 @@ export const SharedWallet = (): JSX.Element => {
6161
if (isSharedWallet) setBackgroundPage();
6262
}, [isSharedWallet, setBackgroundPage]);
6363

64-
const handleCreateWallet = async (data: CreateWalletParams) => {
65-
const activeWalletId = cardanoWallet.source.wallet.walletId;
64+
const handleCreateWallet = useCallback(
65+
async (data: CreateWalletParams) => {
66+
const activeWalletId = cardanoWallet.source.wallet.walletId;
6667

67-
try {
68-
await createInMemorySharedWallet({
69-
name: data.name,
70-
chainId: Wallet.Cardano.ChainIds[environmentName],
71-
ownSignerWalletId: activeWalletId,
72-
quorumRules: data.quorumRules,
73-
coSigners: data.coSigners
74-
});
75-
} catch (error: unknown) {
76-
if (error instanceof WalletConflictError) {
77-
setIsWalletConflictModalVisible(true);
68+
try {
69+
await createInMemorySharedWallet({
70+
name: data.name,
71+
chainId: Wallet.Cardano.ChainIds[environmentName],
72+
ownSignerWalletId: activeWalletId,
73+
quorumRules: data.quorumRules,
74+
coSigners: data.coSigners
75+
});
76+
} catch (error: unknown) {
77+
if (error instanceof WalletConflictError) {
78+
setIsWalletConflictModalVisible(true);
79+
}
80+
throw error;
7881
}
79-
throw error;
80-
}
81-
};
82+
},
83+
[cardanoWallet.source.wallet.walletId, environmentName, createInMemorySharedWallet]
84+
);
8285

8386
const generateKey = useCallback(
8487
async (enteredPassword?: string) => {
@@ -97,6 +100,7 @@ export const SharedWallet = (): JSX.Element => {
97100
);
98101

99102
const navigateToRoot = useCallback(() => history.push(walletRoutePaths.sharedWallet.root), [history]);
103+
const navigateToAppHome = useCallback(() => setBackgroundPage(), [setBackgroundPage]);
100104

101105
return (
102106
<>
@@ -131,7 +135,7 @@ export const SharedWallet = (): JSX.Element => {
131135
walletKind={isHardwareWallet ? 'cold' : 'hot'}
132136
activeWalletName={walletInfo?.name || ''}
133137
initialWalletName={initialWalletName}
134-
navigateToAppHome={() => setBackgroundPage()}
138+
navigateToAppHome={navigateToAppHome}
135139
exitTheFlow={navigateToRoot}
136140
sharedWalletKey={sharedWalletKey}
137141
onCreateSharedWallet={handleCreateWallet}
@@ -174,7 +178,7 @@ export const SharedWallet = (): JSX.Element => {
174178
}}
175179
sharedKeys={sharedWalletKey}
176180
exitTheFlow={navigateToRoot}
177-
navigateToAppHome={() => setBackgroundPage()}
181+
navigateToAppHome={navigateToAppHome}
178182
onImportJsonError={async () =>
179183
await analytics.sendEventToPostHog(PostHogAction.SharedWalletsLocateWalletImportJsonError)
180184
}

packages/core/src/shared-wallets/add-shared-wallet/creation-flow/SharedWalletCreationStore.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, {
33
ReactElement,
44
ReactNode,
55
createContext,
6+
useCallback,
67
useContext,
78
useEffect,
89
useMemo,
@@ -279,7 +280,7 @@ export const SharedWalletCreationStore = ({
279280
sharedWalletKey,
280281
}: SharedWalletCreationStoreProps): ReactElement => {
281282
const initialState = useInitialState(makeInitialState(activeWalletName));
282-
const [state, dispatch] = useReducer(
283+
const reducer = useCallback(
283284
(prevState: CreationFlowState, action: SharedWalletCreationAction): CreationFlowState => {
284285
const stateMachine = makeStateMachine({
285286
exitTheFlow,
@@ -290,9 +291,11 @@ export const SharedWalletCreationStore = ({
290291
const handler = stateMachine[prevState.step] as Handler<CreationFlowState>;
291292
return handler(prevState, action);
292293
},
293-
initialState,
294+
[exitTheFlow, navigateToAppHome, onCreateSharedWallet, sharedWalletKey],
294295
);
295296

297+
const [state, dispatch] = useReducer(reducer, initialState);
298+
296299
useEffect(() => {
297300
if (state.walletName !== undefined || initialWalletName === undefined) return;
298301
dispatch({ type: SharedWalletCreationActionType.CHANGE_WALLET_NAME, walletName: initialWalletName });

0 commit comments

Comments
 (0)