2021-09-06 16:31:40 +00:00
|
|
|
import { useWeb3 } from "@instadapp/vue-web3";
|
|
|
|
import { injected } from "../connectors";
|
|
|
|
import { onMounted, ref, watch } from "@nuxtjs/composition-api";
|
|
|
|
import { SafeAppConnector } from "@gnosis.pm/safe-apps-web3-react";
|
2021-09-06 16:42:15 +00:00
|
|
|
import { useNetwork } from "./useNetwork";
|
2021-09-06 16:31:40 +00:00
|
|
|
|
|
|
|
export function useSafeAppConnection(connector?: SafeAppConnector) {
|
|
|
|
const { activate, active } = useWeb3();
|
2021-09-06 16:42:15 +00:00
|
|
|
const { activeNetworkId} = useNetwork();
|
2021-09-06 16:31:40 +00:00
|
|
|
|
|
|
|
const tried = ref(false);
|
|
|
|
|
|
|
|
onMounted(() => {
|
2021-09-06 16:42:15 +00:00
|
|
|
connector?.isSafeApp().then(async (loadedInSafe: boolean) => {
|
2021-09-06 16:31:40 +00:00
|
|
|
if (loadedInSafe) {
|
2021-09-06 16:42:15 +00:00
|
|
|
await activate(connector, undefined, true).catch(() => {
|
2021-09-06 16:31:40 +00:00
|
|
|
tried.value = true;
|
|
|
|
});
|
2021-09-06 16:42:15 +00:00
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
activeNetworkId.value = await connector.getChainId()
|
2021-09-06 16:31:40 +00:00
|
|
|
} else {
|
|
|
|
tried.value = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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
|
|
|
|
};
|
|
|
|
}
|