Skip to content

Commit 447b1cd

Browse files
authored
fix: default settings (#88)
* fix: default values setting * v1.3.1
1 parent 30a4aa4 commit 447b1cd

4 files changed

Lines changed: 48 additions & 35 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@socket.tech/plugin",
3-
"version": "1.3.0",
3+
"version": "1.3.1",
44
"main": "dist/index.js",
55
"module": "dist/index.es.js",
66
"types": "dist/index.d.ts",

src/components/Input.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ethers } from "ethers";
22
import { useDispatch, useSelector } from "react-redux";
3-
import { useContext, useEffect, useState, useRef } from "react";
3+
import { useContext, useEffect, useState } from "react";
44
import { Currency, Network, onNetworkChange, onTokenChange } from "../types";
55
import { NATIVE_TOKEN_ADDRESS } from "../consts";
66

@@ -96,7 +96,9 @@ export const Input = ({
9696
const sameChainSwapsEnabled = useSelector(
9797
(state: any) => state.customSettings.sameChainSwapsEnabled
9898
);
99-
const initialAmount = useSelector((state:any)=> state.customSettings.initialAmount);
99+
const initialAmount = useSelector(
100+
(state: any) => state.customSettings.initialAmount
101+
);
100102

101103
function updateNetwork(network: Network) {
102104
dispatch(setSourceChain(network?.chainId));
@@ -142,7 +144,7 @@ export const Input = ({
142144
) ?? _supportedNetworks?.[0]
143145
);
144146
}
145-
}, [allNetworks]);
147+
}, [allNetworks, defaultSourceNetwork]);
146148

147149
// For Input & tokens
148150
const inputAmountFromReduxState = useSelector(
@@ -233,6 +235,18 @@ export const Input = ({
233235
}
234236
}, [allSourceTokens]);
235237

238+
// to set default source token when changed
239+
useEffect(() => {
240+
if (defaultSourceTokenAddress && allSourceTokens) {
241+
const _token =
242+
allSourceTokens?.filter(
243+
(x: Currency) =>
244+
x.address.toLowerCase() === defaultSourceTokenAddress.toLowerCase()
245+
)?.[0] ?? fallbackToUSDC();
246+
_setSourceToken(_token);
247+
}
248+
}, [defaultSourceTokenAddress, allSourceTokens]);
249+
236250
const [_sourceToken, _setSourceToken] = useState<Currency>();
237251
useDebounce(
238252
() => {
@@ -305,21 +319,10 @@ export const Input = ({
305319
} else formateAndParseAmount(balance);
306320
}
307321

308-
// to set the initialAmount if any. To be executed only on first render
309-
const firstRender = useRef(true);
322+
// to set the initialAmount if any
310323
useEffect(() => {
311-
if (initialAmount && sourceToken && firstRender.current) {
312-
const truncatedValue = truncateDecimalValue(
313-
initialAmount,
314-
sourceToken?.decimals
315-
);
316-
onChangeInput(truncatedValue);
317-
firstRender.current = false;
318-
} else if (!firstRender.current) {
319-
// Reset source amount on mount
320-
inputAmountFromReduxState && dispatch(setSourceAmount(null));
321-
}
322-
}, [initialAmount, sourceToken]);
324+
if (initialAmount) onChangeInput(initialAmount);
325+
}, [initialAmount]);
323326

324327
useEffect(() => {
325328
// resetting the source chain on unmount

src/components/Output.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useDispatch, useSelector } from "react-redux";
2-
import { useContext, useEffect, useState } from "react";
2+
import { useContext, useEffect, useState, useRef } from "react";
33
import { Currency, Network, onNetworkChange, onTokenChange } from "../types";
44

55
// component
@@ -95,7 +95,7 @@ export const Output = ({
9595
// on toggle, the dest chain state would retain causing issues in setting token on the first render
9696
return () => {
9797
dispatch(setDestChain(null));
98-
}
98+
};
9999
}, []);
100100

101101
function updateNetwork(network: Network) {
@@ -171,7 +171,7 @@ export const Output = ({
171171
updateNetwork(
172172
supportedNetworks?.find(
173173
(x: Network) => x.chainId === defaultDestNetwork
174-
)
174+
) ?? supportedNetworks?.[0]
175175
);
176176
}
177177
}
@@ -199,7 +199,7 @@ export const Output = ({
199199
)?.[0];
200200

201201
// If same chains are selected, and if the source token is same as usdc, set the dest token to the first token from the list
202-
// todo - if usdc is not found, should show native token.
202+
// todo - if usdc is not found, should show native token.
203203
if (
204204
sourceChainId === destChainId &&
205205
usdc?.address === sourceToken?.address
@@ -261,6 +261,18 @@ export const Output = ({
261261
}
262262
}, [allDestTokens, sourceToken]);
263263

264+
// to set default dest token when changed
265+
useEffect(() => {
266+
if (defaultDestTokenAddress && allDestTokens) {
267+
const _token =
268+
allDestTokens?.filter(
269+
(x: Currency) =>
270+
x.address.toLowerCase() === defaultDestTokenAddress.toLowerCase()
271+
)?.[0] ?? fallbackToUSDC();
272+
_setDestToken(_token);
273+
}
274+
}, [defaultDestTokenAddress, allDestTokens]);
275+
264276
const [_destToken, _setDestToken] = useState<Currency>();
265277
useDebounce(
266278
() => {

src/hooks/apis.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useContext, useEffect, useState } from "react";
1+
import { useContext } from "react";
22
import {
33
Balances,
44
ChainId,
@@ -31,17 +31,15 @@ export const initSocket = (apiKey: string, _singleTxOnly: boolean) => {
3131
// Function to get the chains supported by socket apis.
3232
export const useChains = () => {
3333
const dispatch = useDispatch();
34-
const [allChains, setAllChains] = useState(null);
35-
useEffect(() => {
36-
async function fetchSupportedNetworks() {
37-
const supportedNetworks = await Supported.getAllSupportedChains();
38-
setAllChains(supportedNetworks);
39-
dispatch(setNetworks(supportedNetworks?.result));
40-
}
41-
fetchSupportedNetworks();
42-
}, []);
4334

44-
return allChains;
35+
async function fetchSupportedNetworks() {
36+
const supportedNetworks = await Supported.getAllSupportedChains();
37+
dispatch(setNetworks(supportedNetworks?.result));
38+
return supportedNetworks;
39+
}
40+
41+
const { data } = useSWR("fetching chains", fetchSupportedNetworks);
42+
return data;
4543
};
4644

4745
import { useRoutes } from "./apis/useRoutes";
@@ -81,13 +79,13 @@ export const useBalance = (
8179

8280
const { data, error, isValidating, mutate } = useSWR(
8381
shouldFetch ? [tokenAddress, chainId, userAddress, "token-balance"] : null,
84-
fetchBalance,
82+
fetchBalance
8583
);
8684

8785
return {
8886
data: data?.result,
8987
isBalanceLoading: userAddress && !error && !data,
90-
mutate
88+
mutate,
9189
};
9290
};
9391

0 commit comments

Comments
 (0)