-
-
Notifications
You must be signed in to change notification settings - Fork 129
Use bitcoin-connect for self-custodial zaps #715
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8fb4957
cb4a40a
238d582
375d98b
ba05fbb
d9a79af
fcdb585
6d28b42
f1bf5dd
e06e862
59008b7
6ae3382
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { createContext, useContext, useEffect, useState } from 'react' | ||
|
||
const WebLNContext = createContext({}) | ||
|
||
export function WebLNProvider ({ children }) { | ||
const [provider, setProvider] = useState(null) | ||
const [info, setInfo] = useState(null) | ||
// NOTE these functions are undefined initially - can this be a problem? | ||
const [launchModal, setLaunchModal] = useState() | ||
const [launchPaymentModal, setLaunchPaymentModal] = useState() | ||
const [closeModal, setCloseModal] = useState() | ||
const [isConnected, setIsConnected] = useState() | ||
|
||
useEffect(() => { | ||
const unsub = [] | ||
async function effect () { | ||
const [isConnected, onConnected, onDisconnected, requestProvider, launchModal, launchPaymentModal, closeModal] = await import('@getalby/bitcoin-connect-react').then( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could import |
||
(mod) => [mod.isConnected, mod.onConnected, mod.onDisconnected, mod.requestProvider, mod.launchModal, mod.launchPaymentModal, mod.closeModal] | ||
) | ||
|
||
// if you want to store a function, you need to wrap it with another function because of updater functions | ||
// see https://react.dev/reference/react/useState#updating-state-based-on-the-previous-state | ||
setLaunchModal(() => launchModal) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions are imported and then passed as state, could they just be imported where they are actually used? actually I don't think they are even used yet, are they? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I am importing all of them here so I can simply use what I need where I need it by calling pages/wallet.js: const { provider, info, launchModal } = useWebLN()
You're right, |
||
setLaunchPaymentModal(() => launchPaymentModal) | ||
setCloseModal(() => closeModal) | ||
setIsConnected(() => isConnected) | ||
|
||
if (isConnected()) { | ||
// requestProvider will not launch a modal because a provider is already available. | ||
// TODO but it might for wallets that must be unlocked? | ||
const provider = await requestProvider() | ||
setProvider(provider) | ||
} | ||
unsub.push(onConnected(async (provider) => { | ||
setProvider(provider) | ||
const info = await provider.getInfo() | ||
setInfo(info) | ||
})) | ||
unsub.push(onDisconnected(() => { | ||
setProvider(null) | ||
setInfo(null) | ||
})) | ||
} | ||
effect() | ||
|
||
return () => unsub.forEach(fn => fn()) | ||
}, [setProvider]) | ||
|
||
const value = { provider, info, launchModal, launchPaymentModal, closeModal, isConnected } | ||
return ( | ||
<WebLNContext.Provider value={value}> | ||
{children} | ||
</WebLNContext.Provider> | ||
) | ||
} | ||
|
||
export function useWebLN () { | ||
return useContext(WebLNContext) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.