Skip to content

Commit

Permalink
Merge pull request #211 from gnoswap-labs/GSW-429-change-wallet-conne…
Browse files Browse the repository at this point in the history
…ction-session

[GSW-429] feat: Change wallet connection session
  • Loading branch information
jinoosss authored Oct 16, 2023
2 parents e4d8b53 + d0677c1 commit 5f5a531
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/web/src/common/values/storage-constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export type StorageKeyType =
| "language"
| "search-token-logs";

export type SessionStorageKeyType = "connectedWallet";
export type SessionStorageKeyType = "connected-wallet";
10 changes: 8 additions & 2 deletions packages/web/src/containers/header-container/HeaderContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable */
import Header from "@components/common/header/Header";
import { useRouter } from "next/router";
import React, { useState, useCallback } from "react";
import React, { useState, useCallback, useEffect } from "react";
import { MATH_NEGATIVE_TYPE } from "@constants/option.constant";
import { type TokenInfo } from "@models/token/token-info";
import { useQuery } from "@tanstack/react-query";
Expand Down Expand Up @@ -77,7 +77,7 @@ const HeaderContainer: React.FC = () => {
const [searchMenuToggle, setSearchMenuToggle] = useState(false);
const [keyword, setKeyword] = useState("");
const { breakpoint } = useWindowSize();
const { account, connected, connectAdenaClient } = useWallet();
const { account, connected, initSession, connectAdenaClient } = useWallet();
const {
isFetched,
error,
Expand All @@ -87,6 +87,12 @@ const HeaderContainer: React.FC = () => {
queryFn: () => fetchTokens(keyword),
});

useEffect(() => {
if (window?.adena) {
initSession();
}
}, []);

const onSideMenuToggle = () => {
setSideMenuToggle(prev => !prev);
};
Expand Down
1 change: 0 additions & 1 deletion packages/web/src/hooks/wallet/use-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const useWallet = () => {

useEffect(() => {
if (walletClient) {
initSession();
updateWalletEvents(walletClient);
}
}, [walletClient]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ const GnoswapServiceProvider: React.FC<React.PropsWithChildren> = ({
}) => {
const [networkClient] = useState(new AxiosClient(API_URL));

const [localStorageClient] = useState(WebStorageClient.createLocalStorageClient());
const [localStorageClient, setLocalStorageClient] = useState(WebStorageClient.createLocalStorageClient());

const [sessionStorageClient] = useState(WebStorageClient.createSessionStorageClient());
const [sessionStorageClient, setSessionStorageClient] = useState(WebStorageClient.createSessionStorageClient());

const [walletClient] = useAtom(WalletState.client);

Expand All @@ -43,8 +43,17 @@ const GnoswapServiceProvider: React.FC<React.PropsWithChildren> = ({
const [rpcProvider, setRPCProvider] = useState<GnoProvider | null>(null);

useEffect(() => {
const provider = new GnoWSProvider(network.wsUrl, 5 * 1000);
provider.waitForOpenConnection().then(() => setRPCProvider(provider));
if (window) {
setLocalStorageClient(WebStorageClient.createLocalStorageClient());
setSessionStorageClient(WebStorageClient.createSessionStorageClient());
}
}, []);

useEffect(() => {
if (network) {
const provider = new GnoWSProvider(network.wsUrl, 5 * 1000);
provider.waitForOpenConnection().then(() => setRPCProvider(provider));
}
}, [network]);

const accountRepository = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ export class AccountRepositoryImpl implements AccountRepository {
}

public isConnectedWalletBySession = () => {
const response = this.sessionStorageClient.get("connectedWallet");
const response = this.sessionStorageClient.get("connected-wallet");
return response === "connected";
};

public setConnectedWallet = (connected: boolean) => {
if (connected) {
this.sessionStorageClient.set("connectedWallet", "connected");
this.sessionStorageClient.set("connected-wallet", "connected");
return;
}
this.sessionStorageClient.remove("connectedWallet");
this.sessionStorageClient.remove("connected-wallet");
};

public getAccount = async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/web/src/states/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NetworkModel } from "@models/common/network-model";
import { DEVICE_TYPE } from "@styles/media";
import { atom } from "jotai";
import NetworkData from "@resources/chains.json";
import { atomWithStorage } from "jotai/utils";
import { DEFAULT_SLIPPAGE } from "@constants/option.constant";

Expand All @@ -21,6 +20,6 @@ export const modalContent = atom<React.ReactNode | null>(null);

export const breakpoint = atom<DEVICE_TYPE>(DEVICE_TYPE.WEB);

export const network = atom<NetworkModel>(NetworkData[0]);
export const network = atom<NetworkModel | null>(null);

export const slippage = atomWithStorage<number>("slippage", DEFAULT_SLIPPAGE);

0 comments on commit 5f5a531

Please sign in to comment.