Commit a82a469
authored
fix(perps): Button flash when open a market with an open position (#27671)
## **Description**
When navigating to a market with an existing position, Long/Short
buttons briefly flashed before switching to Modify/Close. Root cause:
`positions` in `usePerpsLivePositions` was derived from `rawPositions`
via a `useEffect`, creating a one-frame lag where `isInitialLoading` was
`false` but `positions` still held stale data. Fix: replace the
state+effect pattern with `useMemo` for synchronous derivation.
## **Changelog**
CHANGELOG entry: Fixed button flash (Long/Short briefly visible) when
opening a market with an existing position
## **Related issues**
Fixes:
[TAT-2236](https://consensyssoftware.atlassian.net/browse/TAT-2236)
## **Manual testing steps**
```gherkin
Feature: Market detail action buttons
Scenario: Open market with existing position
Given I have an open BTC position
When I navigate to the BTC market detail screen
Then I see Modify/Close buttons immediately
And I do not see Long/Short buttons flash
Scenario: Open market without position
Given I have no position on SOL
When I navigate to the SOL market detail screen
Then I see Long/Short buttons immediately
```
## **Screenshots/Recordings**
### **Before**
See automation/27671/before.mp4
### **After**
See automation/27671/after.mp4
## **Pre-merge author checklist**
- [x] I've followed MetaMask Contributor Docs and Coding Standards
- [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 format if applicable
- [x] I've applied the right labels on the PR
## **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]
> **Low Risk**
> Low risk: small hook refactor to compute `positions` synchronously
plus a targeted regression test; main risk is subtle rendering behavior
changes around initial WebSocket updates.
>
> **Overview**
> Fixes a one-render mismatch in `usePerpsLivePositions` where
`isInitialLoading` could flip to `false` before derived `positions`
updated, causing a brief Long/Short button flash when opening a market
with an existing position.
>
> `positions` is now derived from `rawPositions` and `priceData` via
`useMemo` (instead of state set in an effect), and a new regression test
asserts that the first WebSocket update makes `isInitialLoading=false`
and `positions` available in the same render.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
997d441. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 2da84f5 commit a82a469
2 files changed
Lines changed: 38 additions & 14 deletions
Lines changed: 32 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
738 | 738 | | |
739 | 739 | | |
740 | 740 | | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
741 | 773 | | |
742 | 774 | | |
743 | 775 | | |
| |||
Lines changed: 6 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | 102 | | |
106 | 103 | | |
107 | 104 | | |
| |||
113 | 110 | | |
114 | 111 | | |
115 | 112 | | |
116 | | - | |
117 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
118 | 116 | | |
119 | | - | |
120 | | - | |
| 117 | + | |
121 | 118 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
| 119 | + | |
128 | 120 | | |
129 | 121 | | |
130 | 122 | | |
| |||
0 commit comments