-
Notifications
You must be signed in to change notification settings - Fork 13
[TASK-16106] fix: values getting switched in TokenAmountInput #1395
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { PEANUT_WALLET_TOKEN_DECIMALS, STABLE_COINS } from '@/constants' | |
| import { tokenSelectorContext } from '@/context' | ||
| import { formatTokenAmount, formatCurrency } from '@/utils' | ||
| import { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react' | ||
| import { useRouter, useSearchParams } from 'next/navigation' | ||
| import Icon from '../Icon' | ||
| import { twMerge } from 'tailwind-merge' | ||
| import { Icon as IconComponent } from '@/components/Global/Icons/Icon' | ||
|
|
@@ -58,6 +59,8 @@ const TokenAmountInput = ({ | |
| defaultSliderSuggestedAmount, | ||
| }: TokenAmountInputProps) => { | ||
| const { selectedTokenData } = useContext(tokenSelectorContext) | ||
| const router = useRouter() | ||
| const searchParams = useSearchParams() | ||
| const inputRef = useRef<HTMLInputElement>(null) | ||
| const inputType = useMemo(() => (window.innerWidth < 640 ? 'text' : 'number'), []) | ||
| const [isFocused, setIsFocused] = useState(false) | ||
|
|
@@ -173,7 +176,13 @@ const TokenAmountInput = ({ | |
| // to change when we change the display mode or the value (we already call | ||
| // onchange on the input change so dont add those dependencies here!) | ||
| useEffect(() => { | ||
| onChange(displayValue, isInputUsd) | ||
| if (!isInitialInputUsd) { | ||
| const value = tokenValue ? Number(tokenValue) : 0 | ||
| const formattedValue = (value * (currency?.price ?? 1)).toFixed(2) | ||
| onChange(formattedValue, isInputUsd) | ||
| } else { | ||
| onChange(displayValue, isInputUsd) | ||
| } | ||
| }, [selectedTokenData?.price]) // Seriously, this is ok | ||
|
Comment on lines
+179
to
186
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Missing dependencies and unclear conditional logic. This useEffect has several issues:
Consider either:
The comment "Seriously, this is ok" no longer applies given the added complexity. 🤖 Prompt for AI Agents
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. coderabbit very upset at u
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Hugo0 😅 I know, I know! But hear me out – this effect is doing some gymnastics that might bite later: The useEffect is using The specific risk: When the token price updates, the effect will:
This might work fine initially but could cause subtle bugs when users interact with the component before/after price updates. Quick fix options:
Happy to dig deeper if you'd like! What behavior were you seeing that led to this fix? 🤔 🧠 Learnings used |
||
|
|
||
| useEffect(() => { | ||
|
|
@@ -346,6 +355,16 @@ const TokenAmountInput = ({ | |
| setAlternativeDisplayValue(currentValue) | ||
| } | ||
| setIsInputUsd(!isInputUsd) | ||
|
|
||
| // Toggle swap-currency parameter in URL | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: putting state in url |
||
| const params = new URLSearchParams(searchParams.toString()) | ||
| const currentSwapValue = params.get('swap-currency') | ||
| if (currentSwapValue === 'true') { | ||
| params.set('swap-currency', 'false') | ||
| } else { | ||
| params.set('swap-currency', 'true') | ||
| } | ||
| router.replace(`?${params.toString()}`, { scroll: false }) | ||
| }} | ||
| > | ||
| <Icon name={'switch'} className="ml-5 rotate-90 cursor-pointer" width={32} height={32} /> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: comment on what this var is for would be good (swap currency is what; the currency we're swapping into, or from, or what?)