11import invariant from "invariant" ;
2- import { useEffect , useRef } from "react" ;
2+ import { useAtom , useAtomValue } from "jotai" ;
3+ import { useEffect } from "react" ;
34import { toDisplayAmount } from "utils" ;
45import { SwapQuote , SwapState } from "../state" ;
5-
6- // TODO: no need to pass props, use atoms directly
7- export const useSwapSimulation = ( {
8- swapState,
9- setInternalSwapState,
10- quote,
11- } : {
12- swapState : SwapState ;
13- setInternalSwapState : React . Dispatch < React . SetStateAction < SwapState > > ;
14- quote ?: SwapQuote ;
15- } ) : void => {
16- const swapStateRef = useRef ( swapState ) ;
17-
18- useEffect ( ( ) => {
19- swapStateRef . current = swapState ;
20- } , [ swapState ] ) ;
6+ import {
7+ internalSwapStateAtom ,
8+ swapQuoteAtom ,
9+ swapStateAtom ,
10+ } from "../state/atoms" ;
11+
12+ export const useSwapSimulation = ( ) : void => {
13+ const { data : quote } = useAtomValue ( swapQuoteAtom ) ;
14+ const [ internalSwapState , setInternalSwapState ] = useAtom (
15+ internalSwapStateAtom
16+ ) ;
17+ const swapState = useAtomValue ( swapStateAtom ) ;
2118
2219 useEffect ( ( ) => {
2320 const simulate = ( quote : SwapQuote , swapState : SwapState ) : void => {
@@ -39,15 +36,15 @@ export const useSwapSimulation = ({
3936 const simulateBuy = swapState . mode === "buy" ;
4037
4138 if ( simulateSell && sellAsset ) {
42- if ( swapState . sellAmount === swapStateRef . current . sellAmount ) {
39+ if ( swapState . sellAmount === internalSwapState . sellAmount ) {
4340 setInternalSwapState ( ( s ) => ( {
4441 ...s ,
4542 buyAmount : toDisplayAmount ( buyAsset , quote . amountOut ) ,
4643 sellAmountPerOneBuy,
4744 } ) ) ;
4845 }
4946 } else if ( simulateBuy && buyAsset ) {
50- if ( swapState . buyAmount === swapStateRef . current . buyAmount ) {
47+ if ( swapState . buyAmount === internalSwapState . buyAmount ) {
5148 setInternalSwapState ( ( s ) => ( {
5249 ...s ,
5350 sellAmount : toDisplayAmount ( sellAsset , quote . amountIn ) ,
0 commit comments