Skip to content

Commit

Permalink
fix: use mobile-connector when dApp is launched within a mobile app (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fracek authored Aug 4, 2024
2 parents 72c4b69 + 574a6fa commit 85bf16a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-files-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@starknet-react/core": minor
---

use mobile-connector when dApp is launched within an in-app mobile browser
3 changes: 2 additions & 1 deletion packages/core/src/connectors/discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useCallback, useEffect, useMemo, useState } from "react";

import { Connector } from "./base";
import { injected } from "./helpers";
import { withMobileConnector } from "~/connectors/mobile";

export type UseInjectedConnectorsProps = {
/** List of recommended connectors to display. */
Expand Down Expand Up @@ -42,7 +43,7 @@ export function useInjectedConnectors({
});
}, [injectedConnectors, recommended, includeRecommended, order]);

return { connectors };
return { connectors: withMobileConnector(connectors) };
}

function mergeConnectors(
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/connectors/mobile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Connector } from "~/connectors/base";
import { braavos } from "~/connectors/helpers";

export const isMobile = (): boolean => {
const userAgent =
typeof window !== "undefined" ? window.navigator?.userAgent : "";
return /Android|iPhone|iPad|iPod/i.test(userAgent);
};

export const withMobileConnector = (connectors: Connector[]): Connector[] => {
if (
isMobile() &&
// In the context of a mobile browser, `starknet_braavos` is only injected
// if the dApp is launched within the Braavos in-app dApp browser
!!window.starknet_braavos
) {
return [braavos()];
}

return connectors;
};
5 changes: 3 additions & 2 deletions packages/core/src/context/starknet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ConnectorNotFoundError } from "~/errors";
import { ChainProviderFactory } from "~/providers";
import { ExplorerFactory } from "~/explorers/";
import { AccountProvider } from "./account";
import { withMobileConnector } from "~/connectors/mobile";

const defaultQueryClient = new QueryClient();

Expand Down Expand Up @@ -115,7 +116,7 @@ function useStarknetManager({
const [state, setState] = useState<StarknetManagerState>({
currentChain: defaultChain,
currentProvider: defaultProvider,
connectors,
connectors: withMobileConnector(connectors),
});

const updateChainAndProvider = useCallback(
Expand Down Expand Up @@ -262,7 +263,7 @@ function useStarknetManager({
}

if (autoConnect && !connectorRef.current) {
tryAutoConnect(connectors);
tryAutoConnect(withMobileConnector(connectors));
}
// Dependencies intentionally omitted since we only want
// this executed once.
Expand Down

0 comments on commit 85bf16a

Please sign in to comment.