-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
c1d1bb8
commit 5a7a29c
Showing
12 changed files
with
84 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./WalletContext" | ||
export * from "./LedgerWalletAPIProvider" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |