-
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.
We should be able to have global access to the account data. Let's add to this context.
- Loading branch information
1 parent
788b92b
commit 98e4412
Showing
5 changed files
with
87 additions
and
7 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 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,35 @@ | ||
import { Account } from "@ledgerhq/wallet-api-client" | ||
import React, { createContext, useMemo, useState } from "react" | ||
|
||
type LedgerLiveAppContextValue = { | ||
btcAccount: Account | undefined | ||
setBtcAccount: React.Dispatch<React.SetStateAction<Account | undefined>> | ||
} | ||
|
||
export const LedgerLiveAppContext = createContext<LedgerLiveAppContextValue>({ | ||
btcAccount: undefined, | ||
setBtcAccount: () => {}, | ||
}) | ||
|
||
export function LedgerLiveAppProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}): React.ReactElement { | ||
const [btcAccount, setBtcAccount] = useState<Account | undefined>(undefined) | ||
|
||
const contextValue: LedgerLiveAppContextValue = | ||
useMemo<LedgerLiveAppContextValue>( | ||
() => ({ | ||
btcAccount, | ||
setBtcAccount, | ||
}), | ||
[btcAccount, setBtcAccount], | ||
) | ||
|
||
return ( | ||
<LedgerLiveAppContext.Provider value={contextValue}> | ||
{children} | ||
</LedgerLiveAppContext.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,34 @@ | ||
import { Account, WalletAPIClient } from "@ledgerhq/wallet-api-client" | ||
import { useRequestAccount } from "@ledgerhq/wallet-api-client-react" | ||
import { useCallback, useContext, useEffect } from "react" | ||
import { LedgerLiveAppContext } from "../contexts/LedgerLiveAppProvider" | ||
import { CURRENCY_ID_BITCOIN } from "../constants" | ||
|
||
type UseRequestAccount = { | ||
pending: boolean | ||
account: Account | null | ||
error: unknown | ||
} | ||
|
||
type RequestAccountParams = Parameters<WalletAPIClient["account"]["request"]> | ||
|
||
type UseRequestAccountReturn = { | ||
requestAccount: (...params: RequestAccountParams) => Promise<void> | ||
} & UseRequestAccount | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export function useRequestBitcoinAccount(): UseRequestAccountReturn { | ||
const { setBtcAccount } = useContext(LedgerLiveAppContext) | ||
const requestAccountResponse = useRequestAccount() | ||
const { account, requestAccount } = requestAccountResponse | ||
|
||
useEffect(() => { | ||
setBtcAccount(account || undefined) | ||
}, [account, setBtcAccount]) | ||
|
||
const requestBitcoinAccount = useCallback(async () => { | ||
await requestAccount({ currencyIds: [CURRENCY_ID_BITCOIN] }) | ||
}, [requestAccount]) | ||
|
||
return { ...requestAccountResponse, 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,3 +1,4 @@ | ||
export * from "./dom-hooks" | ||
export * from "./feature-hooks" | ||
export * from "./helpers-hooks" | ||
export * from "./account-hooks" |