Commit 19722d7
authored
refactor(predict): migrate useUnrealizedPnL to React Query (MetaMask#26877)
## Summary
- Migrate `useUnrealizedPnL` from manual `useState`/`useEffect` to React
Query's `useQuery`, completing React Query adoption across the Predict
module
- Extract query key factory and `queryOptions()` into
`queries/unrealizedPnL.ts` following the established pattern
(`balance.ts`, `positions.ts`)
- Register the new query in `predictQueries` index
- Update consumer (`PredictPositionsHeader`) to use the simplified
`loadUnrealizedPnL()` signature (no more `{ isRefresh }` arg — React
Query handles this)
## Test plan
- [ ] `npx jest useUnrealizedPnL` — all tests pass
- [ ] `npx jest PredictPositionsHeader` — consumer tests still pass
- [ ] Verify unrealized P&L displays correctly on the Positions screen
- [ ] Verify pull-to-refresh updates P&L data
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Moderate risk because it changes how unrealized P&L is fetched/cached
(React Query) and when the UI shows/refreshes it (focus +
position-gated), which could affect staleness and error/display states.
>
> **Overview**
> Migrates `useUnrealizedPnL` from manual `useState`/`useEffect`
fetching to a React Query `useQuery` implementation backed by a new
`predictQueries.unrealizedPnL` entry and `queries/unrealizedPnL.ts`
(keys + `queryOptions`).
>
> Updates `PredictPositionsHeader` to consume the new hook shape
(`data`/`error` as `Error`), **only render P&L when there are active
(non-claimable) positions**, and refresh by invalidating the unrealized
P&L query on screen focus and via the imperative `refresh()` handler.
>
> Ensures unrealized P&L cache is refreshed after relevant actions by
invalidating `predictQueries.unrealizedPnL` on order placement and on
confirmed deposit/claim/withdraw toast events; tests are adjusted
accordingly.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
3100866. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent bb33d3c commit 19722d7
9 files changed
Lines changed: 203 additions & 397 deletions
File tree
- app/components/UI/Predict
- components/PredictPositionsHeader
- hooks
- queries
Lines changed: 29 additions & 45 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
79 | 88 | | |
80 | 89 | | |
81 | 90 | | |
| |||
87 | 96 | | |
88 | 97 | | |
89 | 98 | | |
90 | | - | |
| 99 | + | |
91 | 100 | | |
92 | 101 | | |
93 | 102 | | |
| |||
104 | 113 | | |
105 | 114 | | |
106 | 115 | | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
| |||
191 | 192 | | |
192 | 193 | | |
193 | 194 | | |
194 | | - | |
| 195 | + | |
195 | 196 | | |
196 | 197 | | |
197 | 198 | | |
198 | 199 | | |
199 | 200 | | |
200 | | - | |
| 201 | + | |
201 | 202 | | |
202 | | - | |
203 | | - | |
| 203 | + | |
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
| |||
265 | 265 | | |
266 | 266 | | |
267 | 267 | | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
| 268 | + | |
281 | 269 | | |
282 | 270 | | |
283 | 271 | | |
284 | 272 | | |
285 | 273 | | |
286 | 274 | | |
287 | 275 | | |
288 | | - | |
| 276 | + | |
289 | 277 | | |
290 | 278 | | |
291 | 279 | | |
| |||
303 | 291 | | |
304 | 292 | | |
305 | 293 | | |
306 | | - | |
| 294 | + | |
307 | 295 | | |
308 | 296 | | |
309 | 297 | | |
310 | 298 | | |
311 | 299 | | |
312 | | - | |
| 300 | + | |
313 | 301 | | |
314 | | - | |
315 | | - | |
| 302 | + | |
316 | 303 | | |
317 | 304 | | |
318 | 305 | | |
| |||
326 | 313 | | |
327 | 314 | | |
328 | 315 | | |
329 | | - | |
| 316 | + | |
330 | 317 | | |
331 | | - | |
| 318 | + | |
332 | 319 | | |
333 | | - | |
334 | | - | |
| 320 | + | |
335 | 321 | | |
336 | 322 | | |
337 | 323 | | |
| |||
357 | 343 | | |
358 | 344 | | |
359 | 345 | | |
360 | | - | |
| 346 | + | |
361 | 347 | | |
362 | 348 | | |
363 | 349 | | |
364 | 350 | | |
365 | 351 | | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
370 | 355 | | |
371 | 356 | | |
372 | 357 | | |
| |||
379 | 364 | | |
380 | 365 | | |
381 | 366 | | |
382 | | - | |
| 367 | + | |
383 | 368 | | |
384 | 369 | | |
385 | 370 | | |
386 | 371 | | |
387 | 372 | | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
392 | 376 | | |
393 | 377 | | |
394 | 378 | | |
| |||
Lines changed: 27 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
12 | 16 | | |
13 | 17 | | |
| 18 | + | |
14 | 19 | | |
15 | 20 | | |
16 | 21 | | |
| |||
41 | 46 | | |
42 | 47 | | |
43 | 48 | | |
| 49 | + | |
44 | 50 | | |
45 | 51 | | |
46 | 52 | | |
| |||
89 | 95 | | |
90 | 96 | | |
91 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
92 | 101 | | |
93 | | - | |
| 102 | + | |
94 | 103 | | |
95 | | - | |
96 | 104 | | |
97 | 105 | | |
98 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
99 | 121 | | |
100 | 122 | | |
101 | | - | |
| 123 | + | |
102 | 124 | | |
103 | 125 | | |
104 | 126 | | |
| |||
122 | 144 | | |
123 | 145 | | |
124 | 146 | | |
125 | | - | |
| 147 | + | |
126 | 148 | | |
127 | 149 | | |
128 | 150 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
194 | 194 | | |
195 | 195 | | |
196 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
197 | 201 | | |
198 | 202 | | |
199 | 203 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
192 | 197 | | |
193 | 198 | | |
194 | 199 | | |
| |||
335 | 340 | | |
336 | 341 | | |
337 | 342 | | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
338 | 348 | | |
339 | 349 | | |
340 | 350 | | |
| |||
429 | 439 | | |
430 | 440 | | |
431 | 441 | | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
432 | 447 | | |
433 | 448 | | |
434 | 449 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
164 | 168 | | |
165 | 169 | | |
166 | 170 | | |
| |||
0 commit comments