Skip to content

Commit

Permalink
Merge pull request #129 from alleslabs/fix/persist-wallet
Browse files Browse the repository at this point in the history
fix: wallet disconnection on network change
  • Loading branch information
evilpeach committed Jan 27, 2023
2 parents 1b1e5f4 + 19ed94c commit 2cc39df
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Bug fixes

- [#129](https://github.com/alleslabs/celatone-frontend/pull/129) Fix wallet disconnection on network query change
- [#124](https://github.com/alleslabs/celatone-frontend/pull/124) Fix public project query, display project image in contract details page
- [#125](https://github.com/alleslabs/celatone-frontend/pull/125) Fix incorrect CosmJS execute snippet
- [#117](https://github.com/alleslabs/celatone-frontend/pull/117) Fix native token label formatting
Expand Down
20 changes: 4 additions & 16 deletions src/lib/app-provider/contexts/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { useWallet } from "@cosmos-kit/react";
import big from "big.js";
import { GraphQLClient } from "graphql-request";
import { observer } from "mobx-react-lite";
import { useRouter } from "next/router";
import type { ReactNode } from "react";
import { useEffect, useContext, useMemo, createContext } from "react";

import { useNetworkChange } from "../hooks/useNetworkChange";
import { getIndexerGraphClient } from "../query-client";
import type { AppConstants } from "../types";
import {
getExplorerTxUrl,
getExplorerUserAddressUrl,
} from "lib/app-fns/explorer";
import { LoadingOverlay } from "lib/components/LoadingOverlay";
import { DEFAULT_ADDRESS, getChainNameByNetwork } from "lib/data";
import { DEFAULT_ADDRESS } from "lib/data";
import {
useCodeStore,
useContractStore,
Expand Down Expand Up @@ -64,8 +64,7 @@ export const AppProvider = <ContractAddress, Constants extends AppConstants>({
appContractAddressMap,
constants,
}: AppProviderProps<ContractAddress, Constants>) => {
const router = useRouter();
const { currentChainName, currentChainRecord, setCurrentChain } = useWallet();
const { currentChainName, currentChainRecord } = useWallet();
const { setCodeUserKey, isCodeUserKeyExist } = useCodeStore();
const { setContractUserKey, isContractUserKeyExist } = useContractStore();
const { setProjectUserKey, isProjectUserKeyExist } = usePublicProjectStore();
Expand Down Expand Up @@ -120,18 +119,7 @@ export const AppProvider = <ContractAddress, Constants extends AppConstants>({
}
}, [currentChainName, setCodeUserKey, setContractUserKey, setProjectUserKey]);

useEffect(() => {
/**
* @remarks Condition checking varies by chain
* @todos Change default to mainnet later (currently is testnet)
* @todos Support localnet case later
*/
if (router.query.network === "mainnet") {
setCurrentChain(getChainNameByNetwork("mainnet"));
} else {
setCurrentChain(getChainNameByNetwork("testnet"));
}
}, [router.query.network, setCurrentChain]);
useNetworkChange();

const AppContent = observer(() => {
if (
Expand Down
1 change: 1 addition & 0 deletions src/lib/app-provider/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./useQueryCmds";
export * from "./useExecuteCmds";
export * from "./useTokensInfo";
export * from "./useInternalNavigate";
export * from "./useNetworkChange";
25 changes: 25 additions & 0 deletions src/lib/app-provider/hooks/useNetworkChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { useWallet } from "@cosmos-kit/react";
import { useRouter } from "next/router";
import { useEffect, useRef } from "react";

import { getChainNameByNetwork } from "lib/data";

export const useNetworkChange = () => {
const router = useRouter();
const { currentChainName, setCurrentChain } = useWallet();
const networkRef = useRef<string>();

/**
* @todos Change default to mainnet later (currently is testnet)
*/

useEffect(() => {
const networkRoute = (router.query.network as string) || "testnet";

if (networkRoute !== networkRef.current) {
networkRef.current = networkRoute;
const chainName = getChainNameByNetwork(networkRoute);
if (currentChainName !== chainName) setCurrentChain(chainName);
}
}, [router.query.network, currentChainName, setCurrentChain]);
};

2 comments on commit 2cc39df

@vercel
Copy link

@vercel vercel bot commented on 2cc39df Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 2cc39df Jan 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.