Skip to content

Commit

Permalink
cover mobile wallet disconnect case #5285
Browse files Browse the repository at this point in the history
  • Loading branch information
2nthony committed Feb 19, 2025
1 parent 12a0ed7 commit 44e503e
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions packages/next-common/context/walletconnect/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ export default function WalletConnectProvider({ children }) {

const [session, setSession] = useState(defaultWalletConnect.session);

const [walletConnectSession, setWalletConnectSession] = useLocalStorage(
const [cachedSession, setCachedSession] = useLocalStorage(
CACHE_KEY.walletConnectSession,
session,
);
useEffect(() => {
if (walletConnectSession) {
setSession(walletConnectSession);
if (cachedSession) {
setSession(cachedSession);
}
}, [walletConnectSession]);
}, [cachedSession]);

const clearSession = useCallback(() => {
setSession(null);
setCachedSession(null);
}, [setCachedSession, setSession]);

// Init WalletConnect provider
useEffect(() => {
Expand Down Expand Up @@ -113,12 +118,12 @@ export default function WalletConnectProvider({ children }) {
.then((result) => {
result.approval().then((session) => {
setSession(session);
setWalletConnectSession(session);
setCachedSession(session);
});

return result;
});
}, [chainId, provider, setWalletConnectSession]);
}, [chainId, provider, setCachedSession]);

const disconnect = useCallback(async () => {
if (!provider || !session) {
Expand All @@ -134,9 +139,8 @@ export default function WalletConnectProvider({ children }) {
console.error(error);
}

setSession(null);
setWalletConnectSession(null);
}, [provider, session, setWalletConnectSession]);
clearSession();
}, [provider, session, clearSession]);

const fetchAddresses = useCallback(async () => {
if (!provider || !session) {
Expand Down Expand Up @@ -204,8 +208,7 @@ export default function WalletConnectProvider({ children }) {
useEffect(() => {
if (provider) {
provider.on("disconnect", () => {
setSession(null);
setWalletConnectSession(null);
clearSession();
disconnectAccount();
});

Expand All @@ -222,7 +225,19 @@ export default function WalletConnectProvider({ children }) {
provider.client.removeAllListeners();
}
};
}, [disconnectAccount, provider, setWalletConnectSession]);
}, [clearSession, disconnectAccount, provider]);

// If web closed, mobile wallet do disconnect, next time open web, clear session and disconnect account
useEffect(() => {
if (provider && cachedSession) {
const active = provider.client.session.get(cachedSession.topic);
if (!active) {
clearSession();
disconnectAccount();
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [provider, clearSession, disconnectAccount]);

// TODO: check expiry, session2.expiry
// If a session has been connected to, check if it has not expired. If it has, disconnect from
Expand Down

0 comments on commit 44e503e

Please sign in to comment.