Commit ccf1e6c
authored
fix(perps): extract payment token init into useInitPerpsPaymentToken hook and move it to the parent cp-7.73.0 (#28545)
## **Description**
The `PerpsPayRow` component contained ~110 lines of payment token
initialization logic (pending config syncing, default token selection,
asset-change resets) mixed in with its rendering concerns. This made the
component harder to follow and impossible to unit-test the
initialization behavior in isolation.
This PR extracts that logic into a dedicated `useInitPerpsPaymentToken`
hook and calls it from `PerpsOrderViewContentBase` instead. The hook
encapsulates:
- Syncing the pay token from `pendingTradeConfiguration` when resuming
an order
- Falling back to the highest-balance allowlist token when the user has
no perps balance
- Resetting state when the traded asset changes
- Cleaning up the controller's selected payment token on unmount
A `key` prop keyed on `payToken.symbol` was also added to the
`PerpsSlider` so it recomputes width when the payment token changes,
keeping the slider range in sync, allowing the user to use the slider
## **Changelog**
CHANGELOG entry: null
## **Related issues**
Fixes: #28539
## **Manual testing steps**
```gherkin
Feature: Perps payment token initialization
Scenario: user opens a perps order with some perps balance and other tokens
Given the user selects USDC on Arbitrum
When user navigates changes the amount to a different amount
Then the USDC on Arbitrum should be still selected
And user should be able to use slider if they have enough balance to cover the trade
```
## **Screenshots/Recordings**
### **Before**
N/A
### **After**
N/A
## **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 Perps payment-token selection/init logic and its interaction
with `PerpsController`/confirmation pay-token state, which could affect
default token selection and persisted pending configs if incorrect.
>
> **Overview**
> Moves Perps “pay with” token initialization/sync logic out of
`PerpsPayRow` into a new `useInitPerpsPaymentToken` hook, and runs it
from `PerpsOrderView` so pending trade config, default-token fallback,
asset-change resets, and unmount cleanup are handled at the screen
level.
>
> Simplifies `PerpsPayRow` to be a pure display/navigation row (dropping
the `initialAsset` prop) and updates tests accordingly, while adding
focused unit tests for `useInitPerpsPaymentToken`.
>
> Adds a `key` to `PerpsSlider` based on `payToken.symbol` to force a
re-mount when the payment token changes, keeping the slider’s
range/width in sync.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
d3a7d10. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 9cf2c1c commit ccf1e6c
5 files changed
Lines changed: 358 additions & 551 deletions
File tree
- app/components/UI/Perps/Views/PerpsOrderView
Lines changed: 4 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
| 152 | + | |
152 | 153 | | |
153 | 154 | | |
154 | 155 | | |
| |||
1229 | 1230 | | |
1230 | 1231 | | |
1231 | 1232 | | |
| 1233 | + | |
| 1234 | + | |
1232 | 1235 | | |
1233 | 1236 | | |
1234 | 1237 | | |
| |||
1330 | 1333 | | |
1331 | 1334 | | |
1332 | 1335 | | |
| 1336 | + | |
1333 | 1337 | | |
1334 | 1338 | | |
1335 | 1339 | | |
| |||
1482 | 1486 | | |
1483 | 1487 | | |
1484 | 1488 | | |
1485 | | - | |
1486 | 1489 | | |
1487 | 1490 | | |
1488 | 1491 | | |
| |||
0 commit comments