Skip to content

Commit a6d7c42

Browse files
author
jorgen
committed
extract hook
1 parent 2128c25 commit a6d7c42

File tree

6 files changed

+224
-318
lines changed

6 files changed

+224
-318
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"react-dom": "v18",
5959
"react-feather": "^2.0.9",
6060
"react-hotkeys-hook": "^5.1.0",
61+
"react-number-format": "^5.4.4",
6162
"react-pro-sidebar": "^1.1.0",
6263
"react-router-dom": "v5",
6364
"react-tooltip": "^5.18.0",

src/views/SwapAndBridge/components/TokenInput/DestinationTokenInput.tsx

Lines changed: 14 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
import { useCallback, useEffect, useMemo, useState } from "react";
2-
import { formatUnits } from "ethers/lib/utils";
31
import { ReactComponent as ArrowsCross } from "assets/icons/arrows-cross.svg";
4-
import {
5-
convertTokenToUSD,
6-
convertUSDToToken,
7-
formatAmountForDisplay,
8-
formatUSD,
9-
isValidNumberInput,
10-
parseInputValue,
11-
} from "utils";
122
import { ChangeAccountModal } from "../ChangeAccountModal";
133
import SelectorButton from "../ChainTokenSelector/SelectorButton";
144
import { BalanceSelector } from "../BalanceSelector";
@@ -23,7 +13,7 @@ import {
2313
UnitToggleButtonWrapper,
2414
} from "./styles";
2515
import { useQuoteRequestContext } from "../../hooks/useQuoteRequest/QuoteRequestContext";
26-
import { BigNumber } from "ethers";
16+
import { useTokenAmountInput } from "../../hooks";
2717

2818
export type UnitType = "usd" | "token";
2919

@@ -41,158 +31,24 @@ export const DestinationTokenInput = ({
4131
const { quoteRequest, setUserInput, setOriginToken, setDestinationToken } =
4232
useQuoteRequestContext();
4333

44-
const [inputBuffer, setInputBuffer] = useState<string>("");
45-
const [isUserTyping, setIsUserTyping] = useState(false);
46-
4734
const { destinationToken, originToken } = quoteRequest;
4835

49-
const isUserInput = quoteRequest.userInputField === "destination";
50-
51-
const handleSetInputValue = useCallback(
52-
(value: string) => {
53-
if (!destinationToken) {
54-
setUserInput("destination", null);
55-
return;
56-
}
57-
58-
try {
59-
const parsed = parseInputValue(value, destinationToken, unit);
60-
setUserInput("destination", parsed);
61-
} catch (e) {
62-
setUserInput("destination", null);
63-
}
64-
},
65-
[setUserInput, destinationToken, unit]
66-
);
67-
68-
const handleBalanceClick = useCallback(
69-
(amount: BigNumber) => {
70-
if (!destinationToken) return;
71-
72-
setUserInput("destination", amount);
73-
setIsUserTyping(false);
74-
setInputBuffer("");
75-
},
76-
[destinationToken, setUserInput]
77-
);
78-
79-
const displayValue = useMemo(() => {
80-
if (isUserTyping) {
81-
return inputBuffer;
82-
}
83-
84-
if (!isUserInput && isUpdateLoading) {
85-
return "";
86-
}
87-
88-
const amount = isUserInput
89-
? quoteRequest.userInputAmount
90-
: quoteRequest.quoteOutputAmount;
91-
92-
if (!amount || !destinationToken) {
93-
return "";
94-
}
95-
96-
return formatAmountForDisplay(amount, destinationToken, unit);
97-
}, [
98-
isUserTyping,
99-
inputBuffer,
100-
isUserInput,
101-
isUpdateLoading,
102-
quoteRequest.userInputAmount,
103-
quoteRequest.quoteOutputAmount,
104-
destinationToken,
36+
const {
37+
displayValue,
38+
formattedConvertedAmount,
39+
inputDisabled,
40+
handleInputChange,
41+
handleBalanceClick,
42+
toggleUnit,
43+
} = useTokenAmountInput({
44+
token: destinationToken,
45+
fieldName: "destination",
10546
unit,
106-
]);
107-
108-
const [convertedAmount, setConvertedAmount] = useState<BigNumber>();
109-
110-
useEffect(() => {
111-
const amount = isUserInput
112-
? quoteRequest.userInputAmount
113-
: quoteRequest.quoteOutputAmount;
114-
115-
if (!destinationToken || !amount) {
116-
setConvertedAmount(undefined);
117-
return;
118-
}
119-
120-
try {
121-
const formatted = formatAmountForDisplay(amount, destinationToken, unit);
122-
if (unit === "token") {
123-
const usdValue = convertTokenToUSD(formatted, destinationToken);
124-
setConvertedAmount(usdValue);
125-
} else {
126-
const tokenValue = convertUSDToToken(formatted, destinationToken);
127-
setConvertedAmount(tokenValue);
128-
}
129-
} catch (e) {
130-
setConvertedAmount(undefined);
131-
}
132-
}, [
133-
destinationToken,
134-
quoteRequest.userInputAmount,
135-
quoteRequest.quoteOutputAmount,
136-
unit,
137-
isUserInput,
138-
]);
139-
140-
const toggleUnit = useCallback(() => {
141-
if (!destinationToken || !convertedAmount) {
142-
setUnit(unit === "token" ? "usd" : "token");
143-
return;
144-
}
145-
146-
const newUnit = unit === "token" ? "usd" : "token";
147-
setUnit(newUnit);
148-
149-
if (isUserInput && quoteRequest.userInputAmount) {
150-
setUserInput("destination", convertedAmount);
151-
}
152-
153-
setIsUserTyping(false);
154-
setInputBuffer("");
155-
}, [
156-
unit,
157-
destinationToken,
158-
convertedAmount,
159-
isUserInput,
160-
quoteRequest.userInputAmount,
16147
setUnit,
48+
isUpdateLoading,
16249
setUserInput,
163-
]);
164-
165-
const handleInputChange = useCallback(
166-
(value: string) => {
167-
if (!isValidNumberInput(value)) {
168-
return;
169-
}
170-
171-
setIsUserTyping(true);
172-
setInputBuffer(value);
173-
handleSetInputValue(value);
174-
},
175-
[handleSetInputValue]
176-
);
177-
178-
const handleBlur = useCallback(() => {
179-
setIsUserTyping(false);
180-
setInputBuffer("");
181-
}, []);
182-
183-
const inputDisabled = (() => {
184-
if (!quoteRequest.destinationToken) return true;
185-
return Boolean(!isUserInput && isUpdateLoading);
186-
})();
187-
188-
const formattedConvertedAmount = useMemo(() => {
189-
if (unit === "token") {
190-
if (!convertedAmount) return "$0.00";
191-
return "$" + formatUSD(convertedAmount);
192-
}
193-
if (!convertedAmount) return "0.00";
194-
return `${formatUnits(convertedAmount, destinationToken?.decimals)} ${destinationToken?.symbol}`;
195-
}, [unit, convertedAmount, destinationToken]);
50+
quoteRequest,
51+
});
19652

19753
return (
19854
<TokenInputWrapper>
@@ -213,7 +69,6 @@ export const DestinationTokenInput = ({
21369
placeholder="0.00"
21470
value={displayValue}
21571
onChange={(e) => handleInputChange(e.target.value)}
216-
onBlur={handleBlur}
21772
disabled={inputDisabled}
21873
error={false}
21974
/>

0 commit comments

Comments
 (0)