Skip to content

Commit c4ccc70

Browse files
Kureevmatallui
andauthored
refactor(predict): migrate usePredictOrderPreview to React Query (MetaMask#26875)
## Summary - Migrate `usePredictOrderPreview` hook from manual state management to React Query (`useQuery`) - Extract query key factory and query options into `queries/orderPreview.ts` - Simplify debounced order preview calculation by leveraging React Query for caching and automatic refetch ## Test plan - [x] Unit tests updated and passing - [ ] Verify order preview updates correctly when changing amount/side - [ ] Verify debounce still works to avoid rapid recalculations <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Behavior shifts from manual async/timer management to React Query caching and polling, which can subtly change loading states, refresh timing, and stale-data handling in UI consumers. Moderate risk due to being user-facing trading/preview flows, though logic is mostly a refactor with tests updated. > > **Overview** > Refactors `usePredictOrderPreview` from manual state/operation tracking and custom timers to a debounced `useQuery` flow, using `query.isFetching` for `isCalculating`, deriving `isLoading` from `preview === null && !error`, and using `refetchInterval` for optional auto-refresh. > > Introduces `predictQueries.orderPreview` (new `queries/orderPreview.ts`) with stable query keys, `keepPreviousData` placeholder behavior, and an `Engine.context.PredictController.previewOrder` query function; error handling now parses localized messages and logs via `Logger.error` when the query fails. > > Updates the hook’s unit tests to run under a `QueryClientProvider`, mock `Engine`/`Logger`, and align expectations with the new debounce, loading, error, and auto-refresh behavior. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 6f60a9e. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Luis Taniça <matallui@gmail.com>
1 parent 7e988fc commit c4ccc70

4 files changed

Lines changed: 316 additions & 412 deletions

File tree

0 commit comments

Comments
 (0)