Skip to content

Commit

Permalink
Update an approach to the contexts
Browse files Browse the repository at this point in the history
- Remove a dir for providers and move it to contexts dir.
- Create a wrapper for context.
- Use a more abstract context name - `WalletContext`
- Small improvements
  • Loading branch information
kkosiorowska committed Nov 28, 2023
1 parent c1d1bb8 commit 5a7a29c
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 86 deletions.
16 changes: 6 additions & 10 deletions dapp/src/DApp.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<Box>
Expand All @@ -27,11 +23,11 @@ function DApp() {
function DAppProviders() {
return (
<LedgerWalletAPIProvider>
<LedgerLiveAppProvider>
<WalletContextProvider>
<ChakraProvider theme={theme}>
<DApp />
</ChakraProvider>
</LedgerLiveAppProvider>
</WalletContextProvider>
</LedgerWalletAPIProvider>
)
}
Expand Down
20 changes: 10 additions & 10 deletions dapp/src/components/Navbar/ConnectWallet.tsx
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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<void>
}

Expand All @@ -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 (
<Box display="flex" alignItems="center" gap="4">
Expand All @@ -59,17 +59,17 @@ export default function ConnectWallet() {
<ConnectButton
leftIcon={BitcoinIcon}
rightIcon={InfoIcon}
account={requestBitcoinAccount.account}
account={btcAccount}
requestAccount={async () => {
await requestBitcoinAccount.requestAccount()
await requestBitcoinAccount()
}}
/>
<ConnectButton
leftIcon={EthereumIcon}
rightIcon={InfoIcon}
account={requestEthereumAccount.account}
account={ethAccount}
requestAccount={async () => {
await requestEthereumAccount.requestAccount()
await requestEthereumAccount()
}}
/>
</Box>
Expand Down
42 changes: 0 additions & 42 deletions dapp/src/contexts/LedgerLiveAppContext.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type LedgerWalletAPIProviderProps = {
children: React.ReactElement
}

export default function LedgerWalletAPIProvider({
export function LedgerWalletAPIProvider({
children,
}: LedgerWalletAPIProviderProps): JSX.Element {
const transport = getWalletAPITransport()
Expand Down
38 changes: 38 additions & 0 deletions dapp/src/contexts/WalletContext.tsx
Original file line number Diff line number Diff line change
@@ -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<React.SetStateAction<Account | undefined>>
ethAccount: Account | undefined
setEthAccount: React.Dispatch<React.SetStateAction<Account | undefined>>
}

export const WalletContext = createContext<WalletContextValue | undefined>(
undefined,
)

export function WalletContextProvider({
children,
}: {
children: React.ReactNode
}): React.ReactElement {
const [btcAccount, setBtcAccount] = useState<Account | undefined>(undefined)
const [ethAccount, setEthAccount] = useState<Account | undefined>(undefined)

const contextValue: WalletContextValue = useMemo<WalletContextValue>(
() => ({
btcAccount,
setBtcAccount,
ethAccount,
setEthAccount,
}),
[btcAccount, setBtcAccount, ethAccount, setEthAccount],
)

return (
<WalletContext.Provider value={contextValue}>
{children}
</WalletContext.Provider>
)
}
2 changes: 2 additions & 0 deletions dapp/src/contexts/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./WalletContext"
export * from "./LedgerWalletAPIProvider"
1 change: 1 addition & 0 deletions dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./useDetectThemeMode"
export * from "./useRequestBitcoinAccount"
export * from "./useRequestEthereumAccount"
export * from "./useWalletContext"
13 changes: 6 additions & 7 deletions dapp/src/hooks/useRequestBitcoinAccount.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
13 changes: 6 additions & 7 deletions dapp/src/hooks/useRequestEthereumAccount.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
12 changes: 12 additions & 0 deletions dapp/src/hooks/useWalletContext.ts
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 0 additions & 1 deletion dapp/src/providers/index.ts

This file was deleted.

10 changes: 2 additions & 8 deletions dapp/src/types/ledger-live-app.ts
Original file line number Diff line number Diff line change
@@ -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<WalletAPIClient["account"]["request"]>

export type UseRequestAccountReturn = {
requestAccount: (...params: RequestAccountParams) => Promise<void>
} & UseRequestAccount
}

0 comments on commit 5a7a29c

Please sign in to comment.