assembly/composables/useEagerConnect.ts

36 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-09-03 13:59:09 +00:00
import { useWeb3 } from "@instadapp/vue-web3";
2021-09-06 16:31:40 +00:00
import { injected, gnosisSafe } from "../connectors";
import { onMounted, ref, watch, watchEffect } from "@nuxtjs/composition-api";
import { useSafeAppConnection } from "./useSafeAppConnection";
2021-08-31 19:23:47 +00:00
export function useEagerConnect() {
const { activate, active } = useWeb3();
2021-09-06 16:31:40 +00:00
const { tried: triedToConnectToSafe } = useSafeAppConnection(gnosisSafe);
2021-08-31 19:23:47 +00:00
const tried = ref(false);
2021-09-06 16:31:40 +00:00
watchEffect(() => {
2021-09-06 22:36:09 +00:00
if (triedToConnectToSafe.value && !active.value && !tried.value) {
2021-09-06 16:31:40 +00:00
injected.isAuthorized().then((isAuthorized: boolean) => {
if (isAuthorized) {
activate(injected, undefined, true).catch(() => {
tried.value = true;
});
} else {
2021-08-31 19:23:47 +00:00
tried.value = true;
2021-09-06 16:31:40 +00:00
}
});
}
2021-08-31 19:23:47 +00:00
});
// if the connection worked, wait until we get confirmation of that to flip the flag
watch([tried, active], () => {
if (!tried.value && active.value) {
tried.value = true;
}
});
return {
tried
};
2021-09-06 16:31:40 +00:00
}