Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: argent mobile connection #24

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/components/connection/starknetConnect.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import React, { FunctionComponent } from "react";
import React, { FunctionComponent, useEffect, useState } from "react";
import { Modal, useMediaQuery } from "@mui/material";
import { Connector } from "starknetkit";
import styles from "../../styles/components/walletConnect.module.css";
Expand All @@ -9,6 +9,7 @@ import {
getConnectorDiscovery,
getConnectorIcon,
getConnectorName,
isInArgentMobileAppBrowser,
sortConnectors,
} from "@/utils/starknetConnectorsWrapper";
import Button from "../button";
Expand All @@ -29,6 +30,12 @@ const StarknetWalletConnect: FunctionComponent<StarknetWalletConnectProps> = ({
onWalletConnected,
}) => {
const { connectAsync } = useConnect();
const [isArgentMobile, setIsArgentMobile] = useState(false);

useEffect(() => {
if (typeof window !== "undefined")
setIsArgentMobile(isInArgentMobileAppBrowser());
}, []);

const connect = async (connector: Connector) => {
await connectAsync({ connector });
Expand All @@ -38,6 +45,9 @@ const StarknetWalletConnect: FunctionComponent<StarknetWalletConnectProps> = ({
const isMobile = useMediaQuery("(max-width: 768px)");

const filterConnectors = (connectors: Connector[]) => {
if (isArgentMobile) {
return connectors.filter((connector) => connector.id === "argentMobile");
}
if (!isMobile) return connectors;
return connectors.filter((connector) => connector.id !== "argentX");
};
Expand Down
15 changes: 14 additions & 1 deletion utils/starknetConnectorsWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { constants } from "starknet";
import { Connector } from "starknetkit";
import { Connector, StarknetWindowObject } from "starknetkit";
import { ArgentMobileConnector } from "starknetkit/argentMobile";
import { InjectedConnector } from "starknetkit/injected";
import { WebWalletConnector } from "starknetkit/webwallet";
Expand Down Expand Up @@ -146,6 +146,19 @@ export const getBraavosWebsite = (): string => {
return wallets.find((wallet) => wallet.id === "braavos")?.website as string;
};

export const isInArgentMobileAppBrowser = (): boolean => {
if (typeof window === "undefined" || !window?.starknet_argentX) {
return false;
}

const starknetMobile =
window?.starknet_argentX as unknown as StarknetWindowObject & {
isInAppBrowser: boolean;
};

return starknetMobile?.isInAppBrowser;
};

const wallets = [
{
id: "argentX",
Expand Down