-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #383 from skip-mev/add-widget-v2
Release widgetv2
- Loading branch information
Showing
13 changed files
with
4,505 additions
and
1,838 deletions.
There are no files selected for viewing
Submodule chain-registry
updated
340 files
Submodule initia-registry
updated
47 files
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,19 @@ | ||
import { Chain } from "@skip-go/client"; | ||
import { useAssets, useChains } from "@skip-go/widget"; | ||
import { useQueryState } from "nuqs"; | ||
import { useEffect, useState } from "react"; | ||
import toast from "react-hot-toast"; | ||
|
||
export const useURLQueryParams = () => { | ||
const { data: chains } = useChains(); | ||
const { isReady, assetsByChainID } = useAssets(); | ||
const [state, setState] = useState<{ | ||
srcChain?: string; | ||
srcAssetDenom?: string; | ||
destChain?: string; | ||
destAssetDenom?: string; | ||
amountIn?: string; | ||
amountOut?: string; | ||
}>(); | ||
|
||
const toastId = "url-params-toast"; | ||
|
||
const [srcChainQP, setSrcChainQP] = useQueryState("src_chain"); | ||
const [srcAssetQP, setSrcAssetQP] = useQueryState("src_asset"); | ||
const [destChainQP, setDestChainQP] = useQueryState("dest_chain"); | ||
|
||
useEffect(() => { | ||
if (!chains || !isReady) return; | ||
if (srcChainQP) { | ||
const findChain = chains.find((x: Chain) => x.chainID.toLowerCase() === decodeURI(srcChainQP).toLowerCase()); | ||
if (findChain) { | ||
if (srcAssetQP) { | ||
const assets = assetsByChainID(findChain.chainID); | ||
const findAsset = assets.find((x) => x.denom.toLowerCase() === decodeURI(srcAssetQP).toLowerCase()); | ||
if (findAsset) { | ||
setState((prev) => ({ ...prev, srcChain: findChain.chainID, srcAssetDenom: findAsset.denom })); | ||
setSrcChainQP(null); | ||
setSrcAssetQP(null); | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
return; | ||
} | ||
} | ||
setState((prev) => ({ ...prev, srcChain: findChain.chainID })); | ||
} | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
setSrcChainQP(null); | ||
setSrcAssetQP(null); | ||
} else { | ||
if (destChainQP || state?.destChain) return; | ||
setState((prev) => ({ ...prev, srcChain: "cosmoshub-4" })); | ||
} | ||
}, [ | ||
assetsByChainID, | ||
chains, | ||
destChainQP, | ||
isReady, | ||
setSrcAssetQP, | ||
setSrcChainQP, | ||
srcAssetQP, | ||
srcChainQP, | ||
state?.destChain, | ||
]); | ||
|
||
const [destAssetQP, setDestAssetQP] = useQueryState("dest_asset"); | ||
useEffect(() => { | ||
if (!chains || !isReady) return; | ||
if (destChainQP) { | ||
const findChain = chains.find((x: Chain) => x.chainID.toLowerCase() === decodeURI(destChainQP).toLowerCase()); | ||
if (findChain) { | ||
if (destAssetQP) { | ||
const assets = assetsByChainID(findChain.chainID); | ||
const findAsset = assets.find((x) => x.denom.toLowerCase() === decodeURI(destAssetQP).toLowerCase()); | ||
if (findAsset) { | ||
setState((prev) => ({ ...prev, destChain: findChain.chainID, destAssetDenom: findAsset.denom })); | ||
setDestChainQP(null); | ||
setDestAssetQP(null); | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
return; | ||
} | ||
} | ||
setState((prev) => ({ ...prev, destChain: findChain.chainID })); | ||
} | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
setDestChainQP(null); | ||
setDestAssetQP(null); | ||
} | ||
}, [assetsByChainID, chains, setDestAssetQP, setDestChainQP, destAssetQP, destChainQP, isReady]); | ||
|
||
const [amountInQP, setAmountInQP] = useQueryState("amount_in"); | ||
const [amountOutQP, setAmountOutQP] = useQueryState("amount_out"); | ||
|
||
useEffect(() => { | ||
if (amountInQP) { | ||
setState((prev) => ({ ...prev, amountIn: amountInQP })); | ||
setAmountOutQP(null); | ||
setAmountInQP(null); | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
return; | ||
} | ||
if (amountOutQP) { | ||
setState((prev) => ({ ...prev, amountOut: amountOutQP })); | ||
setAmountOutQP(null); | ||
setAmountInQP(null); | ||
toast.success("URL parameters processed successfully", { | ||
id: toastId, | ||
duration: 5000, | ||
}); | ||
} | ||
}, [amountInQP, amountOutQP, setAmountInQP, setAmountOutQP]); | ||
|
||
useEffect(() => { | ||
// this is a loading state when we are waiting for chains and assets to load when query params are present | ||
if ( | ||
(!chains || !isReady) && | ||
(srcChainQP || srcAssetQP || destChainQP || destAssetQP || amountInQP || amountOutQP) | ||
) { | ||
toast.loading("URL parameters are being processed...", { | ||
id: toastId, | ||
duration: Infinity, | ||
}); | ||
} | ||
}, [amountInQP, amountOutQP, chains, destAssetQP, destChainQP, isReady, srcAssetQP, srcChainQP]); | ||
return state; | ||
const [srcChainQP] = useQueryState("src_chain"); | ||
const [srcAssetQP] = useQueryState("src_asset"); | ||
const [destChainQP] = useQueryState("dest_chain"); | ||
const [destAssetQP] = useQueryState("dest_asset"); | ||
const [amountInQP] = useQueryState("amount_in"); | ||
const [amountOutQP] = useQueryState("amount_out"); | ||
|
||
return { | ||
srcChainID: srcChainQP ?? undefined, | ||
srcAssetDenom: srcAssetQP ?? undefined, | ||
destChainID: destChainQP ?? undefined, | ||
destAssetDenom: destAssetQP ?? undefined, | ||
amountIn: amountInQP ? Number(amountInQP) : undefined, | ||
amountOut: amountOutQP ? Number(amountOutQP) : undefined, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.