From cd909d2da5fc796a68636dc7fd33324e1ae36c19 Mon Sep 17 00:00:00 2001 From: Iris Date: Wed, 11 Oct 2023 10:34:41 +0200 Subject: [PATCH] fix: ordering wallet & move functions to utils --- components/UI/wallets.tsx | 75 +++++---------------------------- hooks/useGetDiscoveryWallets.ts | 36 ++++++++++++++++ pages/_app.tsx | 2 +- utils/browserService.ts | 9 ++++ 4 files changed, 57 insertions(+), 65 deletions(-) create mode 100644 hooks/useGetDiscoveryWallets.ts create mode 100644 utils/browserService.ts diff --git a/components/UI/wallets.tsx b/components/UI/wallets.tsx index ec796dc7..46b8f63e 100644 --- a/components/UI/wallets.tsx +++ b/components/UI/wallets.tsx @@ -1,4 +1,4 @@ -import React, { useMemo, useState } from "react"; +import React from "react"; import styles from "../../styles/components/wallets.module.css"; import { Connector, useAccount, useConnectors } from "@starknet-react/core"; import Button from "./button"; @@ -6,6 +6,7 @@ import { FunctionComponent, useEffect } from "react"; import { Modal } from "@mui/material"; import WalletIcons from "./iconsComponents/icons/walletIcons"; import getDiscoveryWallets from "get-starknet-core"; +import useGetDiscoveryWallets from "../../hooks/useGetDiscoveryWallets"; type WalletsProps = { closeWallet: () => void; @@ -18,17 +19,9 @@ const Wallets: FunctionComponent = ({ }) => { const { connect, connectors } = useConnectors(); const { account } = useAccount(); - const [argent, setArgent] = useState(""); - const [braavos, setBraavos] = useState(""); - const combinations = [ - [0, 1, 2], - [0, 2, 1], - [1, 0, 2], - [1, 2, 0], - [2, 0, 1], - [2, 1, 0], - ]; - const rand = useMemo(() => Math.floor(Math.random() * 6), []); + const downloadLinks = useGetDiscoveryWallets( + getDiscoveryWallets.getDiscoveryWallets() + ); useEffect(() => { if (account) { @@ -36,46 +29,11 @@ const Wallets: FunctionComponent = ({ } }, [account, closeWallet]); - useEffect(() => { - // get wallets download links from get-starknet-core - // if browser is not recognized, it will default to their download pages - getDiscoveryWallets.getDiscoveryWallets().then((wallets) => { - const browser = getBrowser(); - - wallets.map((wallet) => { - if (wallet.id === "argentX") { - setArgent( - browser - ? wallet.downloads[browser as keyof typeof wallet.downloads] - : "https://www.argent.xyz/argent-x/" - ); - } else if (wallet.id === "braavos") { - setBraavos( - browser - ? wallet.downloads[browser as keyof typeof wallet.downloads] - : "https://braavos.app/download-braavos-wallet/" - ); - } - }); - }); - }, []); - function connectWallet(connector: Connector): void { connect(connector); closeWallet(); } - function getBrowser(): string | undefined { - const userAgent = navigator.userAgent; - if (userAgent.includes("Chrome")) { - return "chrome"; - } else if (userAgent.includes("Firefox")) { - return "firefox"; - } else { - return undefined; - } - } - return ( = ({

You need a Starknet wallet

- {combinations[rand].map((index) => { - const connector = connectors[index]; + {connectors.map((connector) => { if (connector.available()) { return (
@@ -123,7 +80,11 @@ const Wallets: FunctionComponent = ({ -
- ); - } - })} */}
); diff --git a/hooks/useGetDiscoveryWallets.ts b/hooks/useGetDiscoveryWallets.ts new file mode 100644 index 00000000..efb26b47 --- /dev/null +++ b/hooks/useGetDiscoveryWallets.ts @@ -0,0 +1,36 @@ +import { useEffect, useState } from "react"; +import { WalletProvider } from "get-starknet-core"; +import { getBrowser } from "../utils/browserService"; + +export default function useGetDiscoveryWallets( + getDiscoveryWallets: Promise +) { + const [argentX, setArgentX] = useState(""); + const [braavos, setBraavos] = useState(""); + + useEffect(() => { + if (typeof navigator !== "undefined") { + getDiscoveryWallets.then((wallets) => { + const browser = getBrowser(navigator.userAgent); + + wallets.map((wallet) => { + if (wallet.id === "argentX") { + setArgentX( + browser + ? wallet.downloads[browser as keyof typeof wallet.downloads] + : "https://www.argent.xyz/argent-x/" + ); + } else if (wallet.id === "braavos") { + setBraavos( + browser + ? wallet.downloads[browser as keyof typeof wallet.downloads] + : "https://braavos.app/download-braavos-wallet/" + ); + } + }); + }); + } + }, [getDiscoveryWallets]); + + return { argentX, braavos }; +} diff --git a/pages/_app.tsx b/pages/_app.tsx index 87d61f41..51406e0d 100644 --- a/pages/_app.tsx +++ b/pages/_app.tsx @@ -15,8 +15,8 @@ import { WebWalletConnector } from "@argent/starknet-react-webwallet-connector"; function MyApp({ Component, pageProps }: AppProps) { const connectors = useMemo( () => [ - new InjectedConnector({ options: { id: "argentX" } }), new InjectedConnector({ options: { id: "braavos" } }), + new InjectedConnector({ options: { id: "argentX" } }), new WebWalletConnector({ url: process.env.NEXT_PUBLIC_IS_TESTNET === "true" diff --git a/utils/browserService.ts b/utils/browserService.ts new file mode 100644 index 00000000..26f451ca --- /dev/null +++ b/utils/browserService.ts @@ -0,0 +1,9 @@ +export function getBrowser(userAgent: string): string | undefined { + if (userAgent.includes("Chrome")) { + return "chrome"; + } else if (userAgent.includes("Firefox")) { + return "firefox"; + } else { + return undefined; + } +}