diff --git a/apps/cowswap-frontend/src/modules/limitOrders/updaters/InitialPriceUpdater/index.tsx b/apps/cowswap-frontend/src/modules/limitOrders/updaters/InitialPriceUpdater/index.tsx index c8695504e2..10f8d20bd8 100644 --- a/apps/cowswap-frontend/src/modules/limitOrders/updaters/InitialPriceUpdater/index.tsx +++ b/apps/cowswap-frontend/src/modules/limitOrders/updaters/InitialPriceUpdater/index.tsx @@ -1,26 +1,30 @@ -import { useSetAtom } from 'jotai' -import { useLayoutEffect, useState } from 'react' +import { useAtomValue, useSetAtom } from 'jotai' +import { useEffect, useLayoutEffect, useState } from 'react' import { usePrevious } from '@cowprotocol/common-hooks' import { Writeable } from 'types' -import { useGetInitialPrice } from 'modules/limitOrders/hooks/useGetInitialPrice' -import { useUpdateActiveRate } from 'modules/limitOrders/hooks/useUpdateActiveRate' -import { LimitRateState, updateLimitRateAtom } from 'modules/limitOrders/state/limitRateAtom' - -import { useLimitOrdersDerivedState } from '../../hooks/useLimitOrdersDerivedState' +import { useGetInitialPrice } from '../../hooks/useGetInitialPrice' +import { useLimitOrdersRawState } from '../../hooks/useLimitOrdersRawState' +import { useUpdateActiveRate } from '../../hooks/useUpdateActiveRate' +import { limitRateAtom, LimitRateState, updateLimitRateAtom } from '../../state/limitRateAtom' // Fetch and update initial price for the selected token pair export function InitialPriceUpdater() { - const { inputCurrency, outputCurrency } = useLimitOrdersDerivedState() + const { inputCurrencyId, outputCurrencyId } = useLimitOrdersRawState() + const { isTypedValue } = useAtomValue(limitRateAtom) const updateLimitRateState = useSetAtom(updateLimitRateAtom) const updateRate = useUpdateActiveRate() - const [isInitialPriceSet, setIsInitialPriceSet] = useState(false) + const [isInitialPriceSet, setIsInitialPriceSet] = useState(isTypedValue) const { price, isLoading } = useGetInitialPrice() const prevPrice = usePrevious(price) + useEffect(() => { + setIsInitialPriceSet(isTypedValue) + }, [isTypedValue]) + useLayoutEffect(() => { const update: Partial> = { initialRate: price, @@ -28,10 +32,6 @@ export function InitialPriceUpdater() { isLoading: isInitialPriceSet ? false : isLoading, } - if (!isInitialPriceSet) { - update.isTypedValue = false - } - updateLimitRateState(update) }, [isInitialPriceSet, price, isLoading, updateLimitRateState]) @@ -40,6 +40,7 @@ export function InitialPriceUpdater() { if (!price || isInitialPriceSet || isLoading || prevPrice?.equalTo(price)) return setIsInitialPriceSet(true) + updateRate({ activeRate: price, isInitialPriceSet: true, @@ -53,7 +54,7 @@ export function InitialPriceUpdater() { // Reset initial price set flag when any token was changed useLayoutEffect(() => { setIsInitialPriceSet(false) - }, [inputCurrency, outputCurrency]) + }, [inputCurrencyId, outputCurrencyId]) return null }