Skip to content

Commit

Permalink
feat: [GSW-2033] Improve User Typing state
Browse files Browse the repository at this point in the history
  • Loading branch information
tfrg committed Jan 5, 2025
1 parent 415b380 commit 60706d0
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions packages/web/src/hooks/swap/data/use-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,25 +166,20 @@ export const useSwap = ({ tokenA, tokenB, direction, slippage, swapFee = 15 }: U
return 0;
}, [direction, estimatedAmount, slippage, tokenA]);

const updateSwapAmount = useCallback(
(amount: string) => {
if (!amount) {
setSwapAmount(null);
setIsTyping(false);
return;
}

const processedAmount = amount.endsWith(".") ? amount.slice(0, -1) : amount;
const updateSwapAmount = (amount: string) => {
if (!amount) {
setSwapAmount(null);
setIsTyping(false);
return;
}

let newAmount = 0;
if (BigNumber(processedAmount).isZero()) {
newAmount = 0;
}
newAmount = BigNumber(processedAmount).toNumber();
const processedAmount = amount.endsWith(".") ? amount.slice(0, -1) : amount;
const newAmount = BigNumber(processedAmount).isZero() ? 0 : BigNumber(processedAmount).toNumber();

setSwapAmount(newAmount);
setSwapAmount(prevAmount => {
const hasValueChanged = prevAmount !== newAmount;

if (tokenA && tokenB && !amount.endsWith(".")) {
if (hasValueChanged) {
setIsTyping(true);

if (typingTimeoutRef.current) {
Expand All @@ -195,9 +190,10 @@ export const useSwap = ({ tokenA, tokenB, direction, slippage, swapFee = 15 }: U
setIsTyping(false);
}, SWAP_AMOUNT_DEBOUNCE_TIME_MS + 100);
}
},
[tokenA, tokenB],
);

return hasValueChanged ? newAmount : prevAmount;
});
};

useEffect(() => {
if (debouncedSwapAmount !== null) {
Expand Down

0 comments on commit 60706d0

Please sign in to comment.