Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(limit-orders): do not override user entered price #5232

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
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<Writeable<LimitRateState>> = {
initialRate: price,
// Don't change isLoading flag when price is already set
isLoading: isInitialPriceSet ? false : isLoading,
}

if (!isInitialPriceSet) {
update.isTypedValue = false
}

updateLimitRateState(update)
}, [isInitialPriceSet, price, isLoading, updateLimitRateState])

Expand All @@ -40,6 +40,7 @@ export function InitialPriceUpdater() {
if (!price || isInitialPriceSet || isLoading || prevPrice?.equalTo(price)) return

setIsInitialPriceSet(true)

updateRate({
activeRate: price,
isInitialPriceSet: true,
Expand All @@ -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
}
Loading