feat: gets persistence check working with provider

This commit is contained in:
Ben Kremer 2022-02-14 14:40:56 +01:00
parent 4d1caa7997
commit ca5b9674d7

View File

@ -50,6 +50,7 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
const [isFetchingBalances, setIsFetchingBalances] = useState(false); const [isFetchingBalances, setIsFetchingBalances] = useState(false);
const [isInitializing, setIsInitializing] = useState(false); const [isInitializing, setIsInitializing] = useState(false);
const [hasCheckedPersistedSession, setHasCheckedPersistedSession] = useState(false);
const [balances, setBalances] = useState<{ symbol: string; balance: string }[]>([]); const [balances, setBalances] = useState<{ symbol: string; balance: string }[]>([]);
const [accounts, setAccounts] = useState<string[]>([]); const [accounts, setAccounts] = useState<string[]>([]);
@ -104,23 +105,6 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
}); });
}, []); }, []);
// const _checkPersistedState = useCallback(
// async (_client: Client) => {
// if (typeof _client === "undefined") {
// throw new Error("WalletConnect is not initialized");
// }
// // populates existing pairings to state
// setPairings(_client.pairing.topics);
// if (typeof session !== "undefined") return;
// // populates existing session to state (assume only the top one)
// if (_client.session.topics.length) {
// const _session = await _client.session.get(_client.session.topics[0]);
// onSessionConnected(_session);
// }
// },
// [session, onSessionConnected],
// );
const createClient = useCallback(async () => { const createClient = useCallback(async () => {
try { try {
setIsInitializing(true); setIsInitializing(true);
@ -133,8 +117,6 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
setClient(_client); setClient(_client);
await _subscribeToClientEvents(_client); await _subscribeToClientEvents(_client);
// TODO:
// await _checkPersistedState(_client);
} catch (err) { } catch (err) {
throw err; throw err;
} finally { } finally {
@ -142,12 +124,6 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
} }
}, [_subscribeToClientEvents]); }, [_subscribeToClientEvents]);
useEffect(() => {
if (!client) {
createClient();
}
}, [client, createClient]);
const onEnable = useCallback( const onEnable = useCallback(
async (caipChainId: string) => { async (caipChainId: string) => {
if (!client) { if (!client) {
@ -201,7 +177,41 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
[client], [client],
); );
console.log(balances); const _checkForPersistedSession = useCallback(
async (_client: Client) => {
if (typeof _client === "undefined") {
throw new Error("WalletConnect is not initialized");
}
// populates existing pairings to state
setPairings(_client.pairing.topics);
if (typeof session !== "undefined") return;
// populates existing session to state (assume only the top one)
if (_client.session.topics.length) {
const _session = await _client.session.get(_client.session.topics[0]);
const [namespace, chainId] = _session.state.accounts[0].split(":");
const caipChainId = `${namespace}:${chainId}`;
onEnable(caipChainId);
}
},
[session, onEnable],
);
useEffect(() => {
if (!client) {
createClient();
}
}, [client, createClient]);
useEffect(() => {
const getPersistedSession = async () => {
if (client && !hasCheckedPersistedSession) {
await _checkForPersistedSession(client);
setHasCheckedPersistedSession(true);
}
};
getPersistedSession();
}, [client, _checkForPersistedSession, hasCheckedPersistedSession]);
const value = useMemo( const value = useMemo(
() => ({ () => ({