refactor: clean up client init
This commit is contained in:
parent
067aca31b8
commit
95751d6efb
@ -60,53 +60,6 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
|
|||||||
const [accounts, setAccounts] = useState<string[]>([]);
|
const [accounts, setAccounts] = useState<string[]>([]);
|
||||||
const [chains, setChains] = useState<string[]>([]);
|
const [chains, setChains] = useState<string[]>([]);
|
||||||
|
|
||||||
const subscribeToEvents = async (_client: Client) => {
|
|
||||||
if (typeof _client === "undefined") {
|
|
||||||
throw new Error("WalletConnect is not initialized");
|
|
||||||
}
|
|
||||||
|
|
||||||
let _session = {} as SessionTypes.Settled;
|
|
||||||
|
|
||||||
if (_client.session.topics.length) {
|
|
||||||
_session = await _client.session.get(_client.session.topics[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
_client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => {
|
|
||||||
const { uri } = proposal.signal.params;
|
|
||||||
console.log("EVENT", "QR Code Modal open");
|
|
||||||
QRCodeModal.open(uri, () => {
|
|
||||||
console.log("EVENT", "QR Code Modal closed");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
_client.on(CLIENT_EVENTS.pairing.created, async (proposal: PairingTypes.Settled) => {
|
|
||||||
if (typeof client === "undefined") return;
|
|
||||||
setPairings(client.pairing.topics);
|
|
||||||
});
|
|
||||||
|
|
||||||
_client.on(CLIENT_EVENTS.session.deleted, (deletedSession: SessionTypes.Settled) => {
|
|
||||||
if (deletedSession.topic !== _session?.topic) return;
|
|
||||||
console.log("EVENT", "session_deleted");
|
|
||||||
// TODO:
|
|
||||||
// this.resetApp();
|
|
||||||
window.location.reload();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const checkPersistedState = 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);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const onSessionConnected = useCallback(async (incomingSession: SessionTypes.Settled) => {
|
const onSessionConnected = useCallback(async (incomingSession: SessionTypes.Settled) => {
|
||||||
setSession(incomingSession);
|
setSession(incomingSession);
|
||||||
setChains(incomingSession.permissions.blockchain.chains);
|
setChains(incomingSession.permissions.blockchain.chains);
|
||||||
@ -203,8 +156,60 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
const subscribeToEvents = useCallback(
|
||||||
const init = async () => {
|
async (_client: Client) => {
|
||||||
|
if (typeof _client === "undefined") {
|
||||||
|
throw new Error("WalletConnect is not initialized");
|
||||||
|
}
|
||||||
|
|
||||||
|
let _session = {} as SessionTypes.Settled;
|
||||||
|
|
||||||
|
if (_client.session.topics.length) {
|
||||||
|
_session = await _client.session.get(_client.session.topics[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
_client.on(CLIENT_EVENTS.pairing.proposal, async (proposal: PairingTypes.Proposal) => {
|
||||||
|
const { uri } = proposal.signal.params;
|
||||||
|
console.log("EVENT", "QR Code Modal open");
|
||||||
|
QRCodeModal.open(uri, () => {
|
||||||
|
console.log("EVENT", "QR Code Modal closed");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
_client.on(CLIENT_EVENTS.pairing.created, async (proposal: PairingTypes.Settled) => {
|
||||||
|
if (typeof client === "undefined") return;
|
||||||
|
setPairings(client.pairing.topics);
|
||||||
|
});
|
||||||
|
|
||||||
|
_client.on(CLIENT_EVENTS.session.deleted, (deletedSession: SessionTypes.Settled) => {
|
||||||
|
if (deletedSession.topic !== _session?.topic) return;
|
||||||
|
console.log("EVENT", "session_deleted");
|
||||||
|
// TODO:
|
||||||
|
// this.resetApp();
|
||||||
|
window.location.reload();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[client],
|
||||||
|
);
|
||||||
|
|
||||||
|
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 () => {
|
||||||
try {
|
try {
|
||||||
const _client = await Client.init({
|
const _client = await Client.init({
|
||||||
logger: DEFAULT_LOGGER,
|
logger: DEFAULT_LOGGER,
|
||||||
@ -219,10 +224,13 @@ export function ClientContextProvider({ children }: { children: ReactNode | Reac
|
|||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
}, [checkPersistedState, subscribeToEvents]);
|
||||||
|
|
||||||
init();
|
useEffect(() => {
|
||||||
}, []);
|
if (!client) {
|
||||||
|
createClient();
|
||||||
|
}
|
||||||
|
}, [client, createClient]);
|
||||||
|
|
||||||
const value = useMemo(
|
const value = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
|
Loading…
Reference in New Issue
Block a user