diff --git a/dapp/src/DApp.tsx b/dapp/src/DApp.tsx index 7042edff1..6f5e4a9bc 100644 --- a/dapp/src/DApp.tsx +++ b/dapp/src/DApp.tsx @@ -1,18 +1,14 @@ -import React, { useContext } from "react" +import React from "react" import { ChakraProvider, Box, Text } from "@chakra-ui/react" -import { useDetectThemeMode } from "./hooks" -import { LedgerWalletAPIProvider } from "./providers" +import { useDetectThemeMode, useWalletContext } from "./hooks" import theme from "./theme" -import { - LedgerLiveAppContext, - LedgerLiveAppProvider, -} from "./contexts/LedgerLiveAppContext" +import { LedgerWalletAPIProvider, WalletContextProvider } from "./contexts" import Navbar from "./components/Navbar" function DApp() { useDetectThemeMode() - const { btcAccount, ethAccount } = useContext(LedgerLiveAppContext) + const { btcAccount, ethAccount } = useWalletContext() return ( @@ -27,11 +23,11 @@ function DApp() { function DAppProviders() { return ( - + - + ) } diff --git a/dapp/src/components/Navbar/ConnectWallet.tsx b/dapp/src/components/Navbar/ConnectWallet.tsx index c7099f1c8..9e761ea9c 100644 --- a/dapp/src/components/Navbar/ConnectWallet.tsx +++ b/dapp/src/components/Navbar/ConnectWallet.tsx @@ -1,4 +1,4 @@ -import React, { useContext } from "react" +import React from "react" import { Box, Button, Image, Text } from "@chakra-ui/react" import { Account } from "@ledgerhq/wallet-api-client" import BitcoinIcon from "../../assets/bitcoin.svg" @@ -8,14 +8,14 @@ import { BITCOIN } from "../../constants" import { useRequestBitcoinAccount, useRequestEthereumAccount, + useWalletContext, } from "../../hooks" -import { LedgerLiveAppContext } from "../../contexts/LedgerLiveAppContext" import { formatSatoshiAmount, truncateAddress } from "../../utils" export type ConnectButtonsProps = { leftIcon: string rightIcon: string - account: Account | null + account: Account | undefined requestAccount: () => Promise } @@ -41,9 +41,9 @@ function ConnectButton({ } export default function ConnectWallet() { - const requestBitcoinAccount = useRequestBitcoinAccount() - const requestEthereumAccount = useRequestEthereumAccount() - const { btcAccount } = useContext(LedgerLiveAppContext) + const { requestAccount: requestBitcoinAccount } = useRequestBitcoinAccount() + const { requestAccount: requestEthereumAccount } = useRequestEthereumAccount() + const { btcAccount, ethAccount } = useWalletContext() return ( @@ -59,17 +59,17 @@ export default function ConnectWallet() { { - await requestBitcoinAccount.requestAccount() + await requestBitcoinAccount() }} /> { - await requestEthereumAccount.requestAccount() + await requestEthereumAccount() }} /> diff --git a/dapp/src/contexts/LedgerLiveAppContext.tsx b/dapp/src/contexts/LedgerLiveAppContext.tsx deleted file mode 100644 index 576462abe..000000000 --- a/dapp/src/contexts/LedgerLiveAppContext.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import { Account } from "@ledgerhq/wallet-api-client" -import React, { createContext, useMemo, useState } from "react" - -type LedgerLiveAppContextValue = { - btcAccount: Account | undefined - setBtcAccount: React.Dispatch> - ethAccount: Account | undefined - setEthAccount: React.Dispatch> -} - -export const LedgerLiveAppContext = createContext({ - btcAccount: undefined, - setBtcAccount: () => {}, - ethAccount: undefined, - setEthAccount: () => {}, -}) - -export function LedgerLiveAppProvider({ - children, -}: { - children: React.ReactNode -}): React.ReactElement { - const [btcAccount, setBtcAccount] = useState(undefined) - const [ethAccount, setEthAccount] = useState(undefined) - - const contextValue: LedgerLiveAppContextValue = - useMemo( - () => ({ - btcAccount, - setBtcAccount, - ethAccount, - setEthAccount, - }), - [btcAccount, setBtcAccount, ethAccount, setEthAccount], - ) - - return ( - - {children} - - ) -} diff --git a/dapp/src/providers/LedgerWalletAPIProvider.tsx b/dapp/src/contexts/LedgerWalletAPIProvider.tsx similarity index 93% rename from dapp/src/providers/LedgerWalletAPIProvider.tsx rename to dapp/src/contexts/LedgerWalletAPIProvider.tsx index 3581cad6f..492f4ee45 100644 --- a/dapp/src/providers/LedgerWalletAPIProvider.tsx +++ b/dapp/src/contexts/LedgerWalletAPIProvider.tsx @@ -19,7 +19,7 @@ type LedgerWalletAPIProviderProps = { children: React.ReactElement } -export default function LedgerWalletAPIProvider({ +export function LedgerWalletAPIProvider({ children, }: LedgerWalletAPIProviderProps): JSX.Element { const transport = getWalletAPITransport() diff --git a/dapp/src/contexts/WalletContext.tsx b/dapp/src/contexts/WalletContext.tsx new file mode 100644 index 000000000..f327ce0bd --- /dev/null +++ b/dapp/src/contexts/WalletContext.tsx @@ -0,0 +1,38 @@ +import { Account } from "@ledgerhq/wallet-api-client" +import React, { createContext, useMemo, useState } from "react" + +type WalletContextValue = { + btcAccount: Account | undefined + setBtcAccount: React.Dispatch> + ethAccount: Account | undefined + setEthAccount: React.Dispatch> +} + +export const WalletContext = createContext( + undefined, +) + +export function WalletContextProvider({ + children, +}: { + children: React.ReactNode +}): React.ReactElement { + const [btcAccount, setBtcAccount] = useState(undefined) + const [ethAccount, setEthAccount] = useState(undefined) + + const contextValue: WalletContextValue = useMemo( + () => ({ + btcAccount, + setBtcAccount, + ethAccount, + setEthAccount, + }), + [btcAccount, setBtcAccount, ethAccount, setEthAccount], + ) + + return ( + + {children} + + ) +} diff --git a/dapp/src/contexts/index.tsx b/dapp/src/contexts/index.tsx new file mode 100644 index 000000000..3ea3058ed --- /dev/null +++ b/dapp/src/contexts/index.tsx @@ -0,0 +1,2 @@ +export * from "./WalletContext" +export * from "./LedgerWalletAPIProvider" diff --git a/dapp/src/hooks/index.ts b/dapp/src/hooks/index.ts index f07366688..038388476 100644 --- a/dapp/src/hooks/index.ts +++ b/dapp/src/hooks/index.ts @@ -1,3 +1,4 @@ export * from "./useDetectThemeMode" export * from "./useRequestBitcoinAccount" export * from "./useRequestEthereumAccount" +export * from "./useWalletContext" diff --git a/dapp/src/hooks/useRequestBitcoinAccount.ts b/dapp/src/hooks/useRequestBitcoinAccount.ts index 3f7a0d120..971a3f9ef 100644 --- a/dapp/src/hooks/useRequestBitcoinAccount.ts +++ b/dapp/src/hooks/useRequestBitcoinAccount.ts @@ -1,21 +1,20 @@ import { useRequestAccount } from "@ledgerhq/wallet-api-client-react" import { useCallback, useContext, useEffect } from "react" -import { LedgerLiveAppContext } from "../contexts/LedgerLiveAppContext" import { CURRENCY_ID_BITCOIN } from "../constants" import { UseRequestAccountReturn } from "../types" +import { WalletContext } from "../contexts" export function useRequestBitcoinAccount(): UseRequestAccountReturn { - const { setBtcAccount } = useContext(LedgerLiveAppContext) - const requestAccountResponse = useRequestAccount() - const { account, requestAccount } = requestAccountResponse + const walletContext = useContext(WalletContext) + const { account, requestAccount } = useRequestAccount() useEffect(() => { - setBtcAccount(account || undefined) - }, [account, setBtcAccount]) + walletContext?.setBtcAccount(account || undefined) + }, [account, walletContext]) const requestBitcoinAccount = useCallback(async () => { await requestAccount({ currencyIds: [CURRENCY_ID_BITCOIN] }) }, [requestAccount]) - return { ...requestAccountResponse, requestAccount: requestBitcoinAccount } + return { requestAccount: requestBitcoinAccount } } diff --git a/dapp/src/hooks/useRequestEthereumAccount.ts b/dapp/src/hooks/useRequestEthereumAccount.ts index b3c674d4c..ebeb1f268 100644 --- a/dapp/src/hooks/useRequestEthereumAccount.ts +++ b/dapp/src/hooks/useRequestEthereumAccount.ts @@ -1,21 +1,20 @@ import { useRequestAccount } from "@ledgerhq/wallet-api-client-react" import { useCallback, useContext, useEffect } from "react" -import { LedgerLiveAppContext } from "../contexts/LedgerLiveAppContext" import { CURRENCY_ID_ETHEREUM } from "../constants" import { UseRequestAccountReturn } from "../types" +import { WalletContext } from "../contexts" export function useRequestEthereumAccount(): UseRequestAccountReturn { - const { setEthAccount } = useContext(LedgerLiveAppContext) - const requestAccountResponse = useRequestAccount() - const { account, requestAccount } = requestAccountResponse + const walletContext = useContext(WalletContext) + const { account, requestAccount } = useRequestAccount() useEffect(() => { - setEthAccount(account || undefined) - }, [account, setEthAccount]) + walletContext?.setEthAccount(account || undefined) + }, [account, walletContext]) const requestEthereumAccount = useCallback(async () => { await requestAccount({ currencyIds: [CURRENCY_ID_ETHEREUM] }) }, [requestAccount]) - return { ...requestAccountResponse, requestAccount: requestEthereumAccount } + return { requestAccount: requestEthereumAccount } } diff --git a/dapp/src/hooks/useWalletContext.ts b/dapp/src/hooks/useWalletContext.ts new file mode 100644 index 000000000..0da19204b --- /dev/null +++ b/dapp/src/hooks/useWalletContext.ts @@ -0,0 +1,12 @@ +import { useContext } from "react" +import { WalletContext } from "../contexts" + +export function useWalletContext() { + const context = useContext(WalletContext) + + if (!context) { + throw new Error("WalletContext used outside of WalletContext component") + } + + return context +} diff --git a/dapp/src/providers/index.ts b/dapp/src/providers/index.ts deleted file mode 100644 index c8f56e2c2..000000000 --- a/dapp/src/providers/index.ts +++ /dev/null @@ -1 +0,0 @@ -export { default as LedgerWalletAPIProvider } from "./LedgerWalletAPIProvider" diff --git a/dapp/src/types/ledger-live-app.ts b/dapp/src/types/ledger-live-app.ts index 84dc9fec8..c63368d3e 100644 --- a/dapp/src/types/ledger-live-app.ts +++ b/dapp/src/types/ledger-live-app.ts @@ -1,13 +1,7 @@ -import { Account, WalletAPIClient } from "@ledgerhq/wallet-api-client" - -type UseRequestAccount = { - pending: boolean - account: Account | null - error: unknown -} +import { WalletAPIClient } from "@ledgerhq/wallet-api-client" type RequestAccountParams = Parameters export type UseRequestAccountReturn = { requestAccount: (...params: RequestAccountParams) => Promise -} & UseRequestAccount +}