Skip to content

Commit

Permalink
fix: reset balance when dissconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
quangdz1704 committed Jul 22, 2024
1 parent 6599085 commit adcf720
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
32 changes: 11 additions & 21 deletions components/layout/connectButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import classNames from "classnames";
import { FC, useCallback, useEffect, useRef, useState } from "react";
import ConnectedInfo from "../connectedInfo";
import styles from "./index.module.scss";
import { useTokenActions } from "@/stores/token/selector";

export type ConnectStatus =
| "init"
Expand All @@ -55,6 +56,8 @@ const ConnectButton: FC<{ fullWidth?: boolean }> = ({ fullWidth }) => {
handleSetTonAddress,
handleSetTonWallet,
} = useAuthenticationActions();
const { handleResetAmountsCache, handleResetTonAmountsCache } =
useTokenActions();
const [connectStatus, setConnectStatus] = useState<
OraiWallet | TonWallet | "init"
>("init");
Expand Down Expand Up @@ -100,20 +103,9 @@ const ConnectButton: FC<{ fullWidth?: boolean }> = ({ fullWidth }) => {
try {
setConnectStatus(walletType);

const walletsList = await connector.getWallets(); // or use `walletsList` fetched before
const _walletsList = await connector.getWallets(); // or use `walletsList` fetched before

const embeddedWallet = walletsList.find(
isWalletInfoCurrentlyEmbedded
) as WalletInfoCurrentlyEmbedded;

// if (embeddedWallet) {
// connector.connect({ jsBridgeKey: embeddedWallet.jsBridgeKey });
// return;
// }

// await connector.disconnect();

const addressConnected = connector.connect({
const _addressConnected = connector.connect({
jsBridgeKey: walletType || "tonkeeper",
});
console.log("addressConnected", connector.account);
Expand All @@ -128,8 +120,10 @@ const ConnectButton: FC<{ fullWidth?: boolean }> = ({ fullWidth }) => {

const handleDisconnectOraichain = (walletType: OraiWallet) => {
if (oraiAddress && walletType === oraiWallet) {
handleSetOraiAddress({ oraiAddress: undefined }),
handleSetOraiWallet({ oraiWallet: undefined });
handleSetOraiAddress({ oraiAddress: undefined });
handleSetOraiWallet({ oraiWallet: undefined });
handleResetAmountsCache();
handleResetTonAmountsCache();
}
};

Expand All @@ -142,6 +136,8 @@ const ConnectButton: FC<{ fullWidth?: boolean }> = ({ fullWidth }) => {
if (tonAddress && walletType === tonWallet) {
handleSetTonAddress({ tonAddress: undefined });
handleSetTonWallet({ tonWallet: undefined });
handleResetTonAmountsCache();
handleResetAmountsCache();
}
} catch (error) {
console.log("error disconnect TON :>>", error);
Expand Down Expand Up @@ -181,12 +177,6 @@ const ConnectButton: FC<{ fullWidth?: boolean }> = ({ fullWidth }) => {
}
}, [tonAddress, tonWallet]);

// useEffect(() => {
// if (open && oraiAddress) {
// setStep(2);
// }
// }, [open]);

// @ts-ignore
const isCheckOwallet =
typeof window !== "undefined" && window?.owallet?.["isOwallet"];
Expand Down
12 changes: 12 additions & 0 deletions stores/token/useTokenStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface TokenActions {
handleSetPricesCache: (prices: CoinGeckoPrices<string>) => void;
handleSetAmountsCache: (amounts: AmountDetails) => void;
handleSetTonAmountsCache: (amountsTon: AmountDetails) => void;
handleResetAmountsCache: () => void;
handleResetTonAmountsCache: () => void;
}

const initialState: IToken = {
Expand Down Expand Up @@ -47,6 +49,16 @@ const useTokenStore = create<IToken & { actions: TokenActions }>()(
...amountsTon,
};
}),
handleResetTonAmountsCache() {
set((state) => {
state.amountsTon = {};
});
},
handleResetAmountsCache() {
set((state) => {
state.amounts = {};
});
},
},
})),
{
Expand Down

0 comments on commit adcf720

Please sign in to comment.