Get chain only from params without route

This commit is contained in:
abefernan
2023-07-10 17:24:42 +02:00
parent 81f82e2ed9
commit a7cfa8c7e0
+7 -6
View File
@@ -3,11 +3,11 @@ import { ChainInfo } from "./types";
const localStorageKey = "context-chain-info";
export const getChainFromUrl = (chainName: string | undefined) => {
export const getChainFromUrl = (chainName: string | null) => {
const params = new URLSearchParams(location.search);
const chain: ChainInfo = {
registryName: chainName || "",
registryName: decodeURIComponent(params.get("registryName") || ""),
chainId: decodeURIComponent(params.get("chainId") || ""),
nodeAddress: decodeURIComponent(params.get("nodeAddress") || ""),
denom: decodeURIComponent(params.get("denom") || ""),
@@ -20,13 +20,14 @@ export const getChainFromUrl = (chainName: string | undefined) => {
explorerLink: decodeURIComponent(params.get("explorerLink") || ""),
};
return isChainInfoFilled(chain) ? chain : null;
const isChainNameValid = chain.registryName === chainName || !chainName;
return isChainNameValid && isChainInfoFilled(chain) ? chain : null;
};
const setChainInUrl = ({ registryName, ...restChainFields }: ChainInfo) => {
export const setChainInUrl = (chain: ChainInfo) => {
const params = new URLSearchParams();
for (const [key, value] of Object.entries(restChainFields)) {
for (const [key, value] of Object.entries(chain)) {
if (typeof value === "object") {
params.set(key, encodeURIComponent(JSON.stringify(value)));
} else {
@@ -34,7 +35,7 @@ const setChainInUrl = ({ registryName, ...restChainFields }: ChainInfo) => {
}
}
window.history.replaceState({}, registryName, `${location.pathname}?${params}`);
window.history.replaceState({}, "", `${location.pathname}?${params}`);
};
export const getChainFromStorage = (chainName: string | undefined) => {