1 import React from "react";
2 3 import { isHttpMode } from "src/utils/isHttpMode";
4 5 const STORAGE_KEY = "bitcoin-protocol-handler-registered";
6 7 export function useRegisterProtocolHandler(basePath: string) {
8 React.useEffect(() => {
9 if (!isHttpMode() || !("registerProtocolHandler" in navigator)) {
10 return;
11 }
12 13 try {
14 // Browsers re-prompt every time registerProtocolHandler is called if the
15 // user previously dismissed the prompt without accepting or denying it.
16 // Limit to once per browser session so we don't ask on every page load,
17 // but users still get re-asked in a new session if they didn't opt in.
18 // sessionStorage access can throw in restricted/private modes, so it's
19 // inside the same try/catch as registerProtocolHandler.
20 if (sessionStorage.getItem(STORAGE_KEY)) {
21 return;
22 }
23 24 const normalizedBasePath = basePath.replace(/\/$/, "");
25 const handlerUrl = `${window.location.origin}${normalizedBasePath}/wallet/send?bip21=%s`;
26 navigator.registerProtocolHandler("bitcoin", handlerUrl);
27 sessionStorage.setItem(STORAGE_KEY, "true");
28 } catch (e) {
29 console.error("Failed to register bitcoin protocol handler", e);
30 }
31 }, [basePath]);
32 }
33