// source: https://github.com/nostr-dev-kit/ndk-react/ import NDK from '@nostr-dev-kit/ndk'; import { NostrFetcher } from 'nostr-fetch'; import { PropsWithChildren, createContext, useContext } from 'react'; import { NDKInstance } from '@libs/ndk/instance'; interface NDKContext { ndk: undefined | NDK; relayUrls: string[]; fetcher: NostrFetcher; } const NDKContext = createContext({ ndk: undefined, relayUrls: [], fetcher: undefined, }); const NDKProvider = ({ children }: PropsWithChildren) => { const { ndk, relayUrls, fetcher } = NDKInstance(); return ( {children} ); }; const useNDK = () => { const context = useContext(NDKContext); if (context === undefined) { throw new Error('import NDKProvider to use useNDK'); } return context; }; export { NDKProvider, useNDK };