Skip to content

Commit ac837b3

Browse files
chore(runway): cherry-pick fix: cp-7.65.0 hardened bottomsheet component to prevent duplicate onClose and goBack calls from firing (#25922)
- fix: cp-7.65.0 hardened bottomsheet component to prevent duplicate onClose and goBack calls from firing (#25862) <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** Fixes bug where duplicate mUSD conversion transactions could be created by spam pressing the "Pay with" menu items. The proposed fix is to harden the `BottomSheet` component to dedupe duplicate events. Bottomsheet changes: - Deduped navigation: `navigation.goBack()` now runs at most once per sheet lifecycle (guards duplicate close events). - Hardened close requests: `onCloseBottomSheet(callback)` is now first-wins while closing (ignores subsequent close requests so the callback can’t be overwritten). - Single-fire post callback: the `postCallback` now runs at most once on close (even if multiple close events fire), and is cleared after running. - Resets on open: on open we reset the close-state refs so the sheet can be reused safely. <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: updated bottomsheet component to dedupe events ## **Related issues** Fixes: [MUSD-306: Duplicate mUSD transactions created when spam clicking token in the "Pay with" menu](https://consensyssoftware.atlassian.net/browse/MUSD-306) ## **Manual testing steps** ```gherkin Feature: Idempotent BottomSheet close behavior Scenario: user closes a bottom sheet multiple times rapidly Given a bottom sheet is open and configured to navigate back on close When the user triggers multiple close actions in quick succession Then the app navigates back only once And any post-close callback runs at most once And logs are displayed to display this dedupe behaviour ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> Video shows the bottomsheet calling `navigation.goBack()` twice. https://github.com/user-attachments/assets/e56cb0f6-0e54-49c6-8bee-26ebc545e97c ### **After** <!-- [screenshots/recordings] --> N/A - Bottomsheet dedupes duplicate events ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I've included tests if applicable - [x] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Touches a widely used `BottomSheet` close/navigation path, so regressions could affect modal dismissal behavior across the app, though the changes are narrow and add guards rather than altering core flows. > > **Overview** > Prevents duplicate bottom-sheet close side effects by making `BottomSheet` idempotent across a sheet lifecycle: `navigation.goBack()` runs at most once, repeated `onCloseBottomSheet()` calls while closing are ignored, and the post-close callback is guaranteed to run at most once (then cleared), with state reset on open and logs when duplicates are skipped. > > Updates `PayWithModal` to pass a single `onClosed` handler to `onCloseBottomSheet` so token-selection actions (mUSD conversion, perps deposit/order, or `setPayToken`) occur only after the sheet finishes closing, reducing risk of duplicate transaction-triggering side effects from rapid taps. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 9c63858. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> [59a8f6a](59a8f6a) Co-authored-by: Matthew Grainger <46547583+Matt561@users.noreply.github.com>
1 parent 07d0125 commit ac837b3

2 files changed

Lines changed: 58 additions & 20 deletions

File tree

app/component-library/components/BottomSheets/BottomSheet/BottomSheet.tsx

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useStyles } from '../../../hooks';
2020

2121
// Internal dependencies.
2222
import styleSheet from './BottomSheet.styles';
23+
import Logger from '../../../../util/Logger';
2324
import {
2425
BottomSheetProps,
2526
BottomSheetRef,
@@ -47,6 +48,9 @@ const BottomSheet = forwardRef<BottomSheetRef, BottomSheetProps>(
4748
) => {
4849
const postCallback = useRef<BottomSheetPostCallback>();
4950
const bottomSheetDialogRef = useRef<BottomSheetDialogRef>(null);
51+
const didNavigateBackRef = useRef(false);
52+
const closeRequestedRef = useRef(false);
53+
const didRunPostCallbackRef = useRef(false);
5054
const { bottom: screenBottomPadding } = useSafeAreaInsets();
5155
const { styles } = useStyles(styleSheet, {
5256
screenBottomPadding,
@@ -55,14 +59,34 @@ const BottomSheet = forwardRef<BottomSheetRef, BottomSheetProps>(
5559
const navigation = useNavigation();
5660

5761
const onOpenCB = useCallback(() => {
62+
// Reset when the sheet is opened again.
63+
didNavigateBackRef.current = false;
64+
closeRequestedRef.current = false;
65+
didRunPostCallbackRef.current = false;
66+
5867
onOpen?.(!!postCallback.current);
59-
postCallback.current?.();
68+
const callback = postCallback.current;
69+
postCallback.current = undefined;
70+
callback?.();
6071
}, [onOpen]);
6172

6273
const onCloseCB = useCallback(() => {
63-
shouldNavigateBack && navigation.goBack();
64-
onClose?.(!!postCallback.current);
65-
postCallback.current?.();
74+
if (shouldNavigateBack && !didNavigateBackRef.current) {
75+
didNavigateBackRef.current = true;
76+
navigation.goBack();
77+
} else if (shouldNavigateBack && didNavigateBackRef.current) {
78+
Logger.log('[BottomSheet] navigation.goBack skipped (duplicate close)');
79+
}
80+
const callback = postCallback.current;
81+
const hasCallback = !!callback;
82+
83+
onClose?.(hasCallback);
84+
85+
if (!didRunPostCallbackRef.current && hasCallback) {
86+
didRunPostCallbackRef.current = true;
87+
postCallback.current = undefined;
88+
callback?.();
89+
}
6690
}, [navigation, onClose, shouldNavigateBack]);
6791

6892
// Dismiss the sheet when Android back button is pressed.
@@ -79,10 +103,22 @@ const BottomSheet = forwardRef<BottomSheetRef, BottomSheetProps>(
79103

80104
useImperativeHandle(ref, () => ({
81105
onCloseBottomSheet: (callback) => {
106+
if (closeRequestedRef.current) {
107+
Logger.log(
108+
'[BottomSheet] onCloseBottomSheet ignored (already closing)',
109+
);
110+
return;
111+
}
112+
113+
closeRequestedRef.current = true;
82114
postCallback.current = callback;
83115
bottomSheetDialogRef.current?.onCloseDialog();
84116
},
85117
onOpenBottomSheet: (callback) => {
118+
// Opening resets close state; allow new close request afterwards.
119+
didNavigateBackRef.current = false;
120+
closeRequestedRef.current = false;
121+
didRunPostCallbackRef.current = false;
86122
postCallback.current = callback;
87123
bottomSheetDialogRef.current?.onOpenDialog();
88124
},

app/components/Views/confirmations/components/modals/pay-with-modal/pay-with-modal.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,30 @@ export function PayWithModal() {
4242

4343
const handleTokenSelect = useCallback(
4444
(token: AssetType) => {
45-
if (
46-
hasTransactionType(transactionMeta, [TransactionType.musdConversion])
47-
) {
48-
close(() => onMusdPaymentTokenChange(token));
49-
return;
50-
}
45+
const onClosed = () => {
46+
if (
47+
hasTransactionType(transactionMeta, [TransactionType.musdConversion])
48+
) {
49+
onMusdPaymentTokenChange(token);
50+
return;
51+
}
5152

52-
if (
53-
hasTransactionType(transactionMeta, [
54-
TransactionType.perpsDepositAndOrder,
55-
])
56-
) {
57-
close(() => onPerpsPaymentTokenChange(token));
58-
return;
59-
}
53+
if (
54+
hasTransactionType(transactionMeta, [
55+
TransactionType.perpsDepositAndOrder,
56+
])
57+
) {
58+
onPerpsPaymentTokenChange(token);
59+
return;
60+
}
6061

61-
close(() => {
6262
setPayToken({
6363
address: token.address as Hex,
6464
chainId: token.chainId as Hex,
6565
});
66-
});
66+
};
67+
68+
close(onClosed);
6769
},
6870
[
6971
close,

0 commit comments

Comments
 (0)