-
Notifications
You must be signed in to change notification settings - Fork 158
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
chore: Update SwapSettingsSlippageInput to use Config #1263
Changes from 3 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@coinbase/onchainkit': patch | ||
--- | ||
|
||
-**chore**: updated `SwapSettingsSlippageInput` to use the input config defaultMaxSlippage value. By @cpcramer #1263. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,23 +12,22 @@ const SLIPPAGE_SETTINGS = { | |
export function SwapSettingsSlippageInput({ | ||
className, | ||
}: SwapSettingsSlippageInputReact) { | ||
const { updateLifecycleStatus, lifecycleStatus } = useSwapContext(); | ||
const getMaxSlippage = useCallback(() => { | ||
return lifecycleStatus.statusData.maxSlippage; | ||
}, [lifecycleStatus.statusData]); | ||
const { | ||
updateLifecycleStatus, | ||
lifecycleStatus, | ||
config: { maxSlippage: defaultMaxSlippage }, | ||
cpcramer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} = useSwapContext(); | ||
|
||
// Set initial slippage values to match previous selection or default, | ||
// ensuring consistency when dropdown is reopened | ||
const [slippage, setSlippage] = useState(getMaxSlippage()); | ||
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. Not needed anymore |
||
const [slippageSetting, setSlippageSetting] = useState( | ||
getMaxSlippage() === DEFAULT_MAX_SLIPPAGE | ||
lifecycleStatus.statusData.maxSlippage === defaultMaxSlippage | ||
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. ok nice |
||
? SLIPPAGE_SETTINGS.AUTO | ||
: SLIPPAGE_SETTINGS.CUSTOM, | ||
); | ||
|
||
const updateSlippage = useCallback( | ||
(newSlippage: number) => { | ||
setSlippage(newSlippage); | ||
updateLifecycleStatus({ | ||
statusName: 'slippageChange', | ||
statusData: { | ||
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. only call slippageChange if newSlippage !== lifecycleStatus.statusData.maxSlippage 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. Updated! |
||
|
@@ -39,20 +38,15 @@ export function SwapSettingsSlippageInput({ | |
[updateLifecycleStatus], | ||
); | ||
|
||
// Handles user input for custom slippage | ||
// Parses the input and updates slippage if valid | ||
// Handles user input for custom slippage. | ||
// Parses the input and updates slippage state. | ||
const handleSlippageChange = useCallback( | ||
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. Well done in updating the comments as well |
||
(newSlippage: string) => { | ||
// Empty '' when the input field is cleared. | ||
if (newSlippage === '') { | ||
setSlippage(0); | ||
return; | ||
} | ||
const parsedSlippage = Number.parseFloat(newSlippage); | ||
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. parseFloat('2.') = 2 preventing you from entering decimals 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. I think this is a fast follow update. I belieeve we will need to update 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. ok, parseFloat(2.2) = 2.2, its just typing the interim step will remove the dot before you type the next number 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. @cpcramer in your update to 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. yep! |
||
const isValidNumber = !Number.isNaN(parsedSlippage); | ||
|
||
const newSlippageNumber = Number.parseFloat(newSlippage); | ||
if (!Number.isNaN(newSlippageNumber)) { | ||
updateSlippage(newSlippageNumber); | ||
} | ||
// Update slippage to parsed value if valid, otherwise set to 0 | ||
updateSlippage(isValidNumber ? parsedSlippage : 0); | ||
}, | ||
[updateSlippage], | ||
); | ||
|
@@ -119,7 +113,7 @@ export function SwapSettingsSlippageInput({ | |
<input | ||
id="slippage-input" | ||
type="text" | ||
value={slippage} | ||
value={lifecycleStatus.statusData.maxSlippage} | ||
onChange={(e) => handleSlippageChange(e.target.value)} | ||
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. if you onChange={handleSlippageChange} and handle the event in your callback react won't have to create a new function here on every render 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. Very cool! Curious if we prefer that optimization over (in my opinion) code simplicity Before:
After:
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. I think it makes sense for a handleChange callback to accept a changeEvent 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. i agree with Adam here 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. Updated |
||
disabled={slippageSetting === SLIPPAGE_SETTINGS.AUTO} | ||
className={cn( | ||
|
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.
Removed the
getMaxSlippage
function.useCallback
is not providing many performance benefits here. Likely it's even slower than accessinglifecycleStatus.statusData.maxSlippage
directlyThere 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.
Nice