From b01baf88c4f377ec245794e32c68ef8b3d43d8b6 Mon Sep 17 00:00:00 2001 From: abretonc7s <107169956+abretonc7s@users.noreply.github.com> Date: Mon, 17 Nov 2025 13:23:26 +0800 Subject: [PATCH] refactor: streamline trade action handling in PerpsMarketDetailsView (#22716) ## **Description** This PR fixes TAT-1564 by moving the cross-margin position validation from the order screen to the market details screen. **Background:** The initial implementation showed the cross-margin warning modal *after* users had already navigated to the order screen and entered their trade details. This created a poor user experience where users would: 1. Tap Long/Short on the market details screen 2. Navigate to the order screen 3. Enter trade amount, leverage, and other details 4. Tap "Place Order" 5. Finally see the cross-margin warning modal **Solution:** This fix moves the validation to occur *before* navigation to the order screen: 1. User taps Long/Short on the market details screen 2. Cross-margin validation runs immediately 3. If cross-margin position detected, modal shows and prevents navigation 4. User sees the warning before entering any trade details The implementation: - Added `handleTradeAction(direction)` shared function in `PerpsMarketDetailsView.tsx` that validates both eligibility and cross-margin before navigation - Removed duplicate cross-margin check from `PerpsOrderView.tsx` - Reuses existing infrastructure: `existingPosition` hook data, modal navigation, error tracking ## **Changelog** CHANGELOG entry: Fixed cross-margin position warning to show before entering trade details instead of after ## **Related issues** Fixes: https://consensyssoftware.atlassian.net/browse/TAT-1564 ## **Manual testing steps** ```gherkin Feature: Cross-margin position warning Scenario: user with cross-margin position attempts to open new trade Given user is on the Perps market details screen And user has an existing cross-margin position for this asset When user taps the "Long" or "Short" button Then cross-margin warning modal should appear immediately And user should remain on the market details screen (navigation blocked) Scenario: user with isolated-margin position opens new trade Given user is on the Perps market details screen And user has an existing isolated-margin position for this asset When user taps the "Long" or "Short" button Then user should navigate to the order screen normally And no warning modal should appear Scenario: user with no existing position opens new trade Given user is on the Perps market details screen And user has no existing position for this asset When user taps the "Long" or "Short" button Then user should navigate to the order screen normally And no warning modal should appear ``` ## **Screenshots/Recordings** ### **Before** Cross-margin warning appeared after user entered trade details in order screen (incorrect timing) ### **After** Cross-margin warning appears immediately when tapping Long/Short button, preventing navigation to order screen (correct timing) https://github.com/user-attachments/assets/c1d9c4ef-5fec-4add-946e-f136dcb3d478 ## **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. --- > [!NOTE] > Centralizes long/short handling with pre-navigation cross-margin validation and warning modal in `PerpsMarketDetailsView`. > > - **Perps UI** > - **`PerpsMarketDetailsView.tsx`**: > - Introduces `handleTradeAction(direction)` to unify Long/Short button logic. > - Adds pre-navigation checks: eligibility gate and cross-margin detection using `existingPosition.leverage.type === 'cross'`. > - On cross-margin, navigates to `CROSS_MARGIN_WARNING` modal and tracks a validation error; otherwise calls `navigateToOrder`. > - Updates `handleLongPress`/`handleShortPress` to delegate to `handleTradeAction`. > > Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit c3db7e8e0c77368f5f07c67d5e9b3e2af40c8900. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot). --- .../PerpsMarketDetailsView.tsx | 58 +++++++++++++------ 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/app/components/UI/Perps/Views/PerpsMarketDetailsView/PerpsMarketDetailsView.tsx b/app/components/UI/Perps/Views/PerpsMarketDetailsView/PerpsMarketDetailsView.tsx index 6101d2105ed8..363aa62d9120 100644 --- a/app/components/UI/Perps/Views/PerpsMarketDetailsView/PerpsMarketDetailsView.tsx +++ b/app/components/UI/Perps/Views/PerpsMarketDetailsView/PerpsMarketDetailsView.tsx @@ -527,28 +527,50 @@ const PerpsMarketDetailsView: React.FC = () => { }); }, [market, isWatchlist, track]); - const handleLongPress = () => { - if (!isEligible) { - setIsEligibilityModalVisible(true); - return; - } + const handleTradeAction = useCallback( + (direction: 'long' | 'short') => { + if (!isEligible) { + setIsEligibilityModalVisible(true); + return; + } - navigateToOrder({ - direction: 'long', - asset: market.symbol, - }); + // Check for cross-margin position (MetaMask only supports isolated margin) + if (existingPosition?.leverage?.type === 'cross') { + navigation.navigate(Routes.PERPS.MODALS.ROOT, { + screen: Routes.PERPS.MODALS.CROSS_MARGIN_WARNING, + }); + + track(MetaMetricsEvents.PERPS_ERROR, { + [PerpsEventProperties.ERROR_TYPE]: + PerpsEventValues.ERROR_TYPE.VALIDATION, + [PerpsEventProperties.ERROR_MESSAGE]: + 'Cross margin position detected', + }); + + return; + } + + navigateToOrder({ + direction, + asset: market.symbol, + }); + }, + [ + isEligible, + existingPosition, + navigation, + track, + navigateToOrder, + market?.symbol, + ], + ); + + const handleLongPress = () => { + handleTradeAction('long'); }; const handleShortPress = () => { - if (!isEligible) { - setIsEligibilityModalVisible(true); - return; - } - - navigateToOrder({ - direction: 'short', - asset: market.symbol, - }); + handleTradeAction('short'); }; const { navigateToConfirmation } = useConfirmNavigation();